OOPs concepts in Java
Java is an object-oriented programming (OOP) language that follows the key principles of OOP such as encapsulation, inheritance, and polymorphism. In this blog , we’ll take a look at four fundamental concepts and see how they are implemented in Java.
- Abstraction
Abstraction is a key OOP concept that involves hiding the implementation details and showing only the necessary information to the user. In Java, abstraction is achieved through the use of abstract classes and interfaces. An abstract class is a class that cannot be instantiated, but it can be extended by other classes to add new features and behaviors. An interface is a blueprint of a class that defines the methods that a class must implement.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a Circle");
}
}
class Rectangle extends Shape {
void draw() {
System.out.println("Drawing a Rectangle");
}
}
class Square extends Shape {
void draw() {
System.out.println("Drawing a Square");
}
}
class AbstractDemo {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Rectangle();
Shape shape3 = new Square()
shape1.draw();
shape2.draw();
shape3.draw();