What is OOPs Concept in Java and Why?
OOPS stands for Object-Oriented Programming. It is a fundamental concept in Java.
There are 4 main concepts Abstraction, Encapsulation, Inheritance and Polymorphism.
It provides a clear and logical structure, making the code easier to understand, maintain, debug and easy to manage.
It is a programming approach that organizes code into classes and objects.
A class is a blueprint that defines properties and behaviors, while an object is a real-world entity.
What is Abstraction? Abstraction is a process of hiding the implementation details and showing only functionality to the user. For example filling the contact form. It will show only the particular details of the contact form not internal process details. It’s called Abstraction in Java.
Points To Remember :
It can have abstract and non-abstract methods.
It cannot be instantiated.
It is used for abstraction.
It provides 100% abstraction because it can have concrete methods.
An abstract class must be declared with an abstract keyword.
An Abstract methods can either be hidden or overridden, but non abstract methods can only be hidden.
What is an Abstract Class?
An abstract class is a class that is declared with abstract keyword.
Abstract classes cannot be instantiated, but they can be subclassed.
It can contain abstract methods (methods without a body) as well as concrete methods (methods with a body).
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.
However, if it does not, then the subclass must also be declared with abstract keyword.
Abstract Class Example:
abstract class Shape {
// Abstract method does not have a body.
public abstract double area();
// Concrete method has a body.
public void display() {
System.out.println("This is a shape...");
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Implementing abstract method
public double area() {
return length * width;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
// Implementing abstract method
public double area() {
return Math.PI * radius * radius;
}
}
// Derived Class
public class Main {
public static void main(String[] args) {
Shape rect = new Rectangle(5, 4);
Shape circle = new Circle(3);
rect.display();
System.out.println("Area of rectangle: " + rect.area());
circle.display();
System.out.println("Area of circle: " + circle.area());
}
}
Output:
This is a shape.
Area of rectangle: 20.0
This is a shape.
Area of circle: 28.274333882308138
What is Abstract method?
A method which is declared as abstract and does not have implementation is known as abstract method. Methods are declared without body within an abstract class is also called abstract method.
Abstract method Example:
abstract class Animal {
// Abstract method does not have a body.
public abstract void makeSound();
// Concrete method
public void sleep() {
System.out.println("Zzz");
}
}
class Dog extends Animal {
// Implementing abstract method
public void makeSound() {
System.out.println("Woof");
}
}
class Cat extends Animal {
// Implementing abstract method
public void makeSound() {
System.out.println("Meow");
}
}
// Derived Class
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
dog.makeSound();
cat.makeSound();
dog.sleep();
cat.sleep();
}
}
Output:
Woof
Meow
Zzz
Zzz