Ticker

6/recent/ticker-posts

Abstraction in Object-Oriented Programming (OOPs)


Abstraction in Object-Oriented Programming (OOPs)

Introduction to Abstraction:
Abstraction is one of the four fundamental pillars of Object-Oriented Programming (OOPs), alongside Encapsulation, Inheritance, and Polymorphism. It is a key concept that allows developers to represent complex real-world entities in a simplified manner. Abstraction focuses on showing only the essential features of an object while hiding unnecessary details. Java provides abstraction through abstract classes and methods.

Java Abstract Class:
In Java, an abstract class is a class that cannot be instantiated directly, meaning you cannot create objects of an abstract class. Instead, it serves as a blueprint for other classes to inherit from. Abstract classes may contain both abstract (without implementation) and concrete (with implementation) methods, as well as regular instance variables.

Syntax for creating an abstract class:

java
public abstract class AbstractClass {
// Abstract method (no method body)
public abstract void abstractMethod();

// Concrete method with implementation
public void concreteMethod() {
// Code implementation
}
}

Explanation:

  • The abstract keyword is used to declare an abstract class. It indicates that the class contains abstract methods and cannot be instantiated.
  • An abstract method is declared without a method body, using the abstract keyword and followed by a semicolon. Subclasses inheriting from this abstract class must provide an implementation for these abstract methods.
  • The abstract class can also have concrete methods with implementations. Subclasses can inherit these methods without overriding them.

Java Abstract Method:
An abstract method is a method declared in an abstract class but does not contain any implementation in the abstract class itself. It is left abstract, and its implementation is deferred to its concrete subclasses.

Syntax for creating an abstract method:

java
public abstract void abstractMethod();

Explanation:

  • The abstract keyword is used to declare an abstract method in an abstract class.
  • Abstract methods are followed by a semicolon instead of a method body.
  • Concrete subclasses that extend the abstract class must provide an implementation for all the abstract methods.

Example:
Let's create a practical example using abstract classes and methods:

java
// Abstract class
public abstract class Shape {
// Abstract method to calculate area
public abstract double calculateArea();

// Concrete method with a default implementation
public void display() {
System.out.println("This is a shape.");
}
}

// Concrete subclass Circle
public class Circle extends Shape {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

// Implementing the abstract method
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}

// Concrete subclass Rectangle
public class Rectangle extends Shape {
private double length;
private double width;

public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

// Implementing the abstract method
@Override
public double calculateArea() {
return length * width;
}
}

Explanation:

  • We define an abstract class Shape with an abstract method calculateArea() and a concrete method display().
  • The Circle and Rectangle classes extend the Shape abstract class and provide implementations for the calculateArea() method specific to their shapes.
  • Since Shape is an abstract class, we cannot create objects of the Shape class, but we can create objects of Circle and Rectangle classes and utilize their respective implementations of calculateArea().

Conclusion:
Abstraction in Java, through abstract classes and methods, allows us to define a common structure for a group of related classes while hiding the implementation details. This helps in creating more maintainable and scalable code in object-oriented systems.

Post a Comment

0 Comments