Abstract: existing in thought or as an idea but not having a physical or concrete existence.
Abstraction lets you focus on what the object does instead of how it does it.
Ways to achieve Abstraction
There are two ways to achieve abstraction in java
- Abstract class (0 to 100%)
- Interface (100%)
Abstract Class is a special type of class which is a kind of restricted class that cannot be used to create objects. They are meant to be subclasses providing a common base for the child classes. It also enforce a contract for subclasses through abstract methods. It includes abstract methods
(methods without a body) and concrete methods
(methods with implementation).
Abstract class is defined using keyword abstract
abstract class AbstractClass {
// Abstract method (does not have a body)
abstract void abstractMethod();
// Concrete method
void concreteMethod() {
System.out.println("This is a concrete method.");
}
// Constructor
AbstractClass() {
System.out.println("Abstract class constructor called.");
}
}
Abstract Class Constructor
Abstract classes can have constructors, but you cannot instantiate an abstract class directly. However, the constructor of an abstract class is called when an instance of a subclass is created.
Abstract classes can have fields and constants.
abstract class Animal {
Animal() {
System.out.println("Animal abstract class constructor");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog constructor");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog(); // Output:
// Animal abstract class constructor
// Dog constructor
}
}
Abstract Methods and Polymorphism
Abstract methods define a contract that subclasses must fulfill. This allows for polymorphism, where a reference to the abstract class can point to an object of any subclass.
Abstract class can extend another abstract class. This allows for a hierarchy of abstract classes where more specific abstract classes build upon the general ones. This can be useful for creating a layered architecture where each layer provides additional specificity and functionality.
If a subclass (abstract or concrete) does not provide implementations for all abstract methods from its superclass, it must be declared abstract.
abstract class Shape {
// Abstract method
abstract void draw();
// Concrete method
void display() {
System.out.println("Displaying shape");
}
}
abstract class Polygon extends Shape {
// Abstract method
abstract int getNumberOfSides();
// Overriding concrete method
@Override
void display() {
System.out.println("Displaying polygon");
}
}
class Triangle extends Polygon {
@Override
void draw() {
System.out.println("Drawing a Triangle");
}
@Override
int getNumberOfSides() {
return 3;
}
}
public class Main {
public static void main(String[] args) {
Triangle triangle = new Triangle();
triangle.draw(); // Drawing a Triangle
triangle.display(); // Displaying polygon
System.out.println("Sides: " + triangle.getNumberOfSides()); // Sides: 3
}
}
How Abstract Class Denotes Abstraction
- Hiding Implementation Details:
- An abstract class can have abstract methods, which are method signatures without implementation. This means the abstract class defines what needs to be done without specifying how it should be done.
- Subclasses provide the implementation details of these abstract methods, effectively hiding the implementation details from the users of the abstract class.
- Providing a Common Interface:
- Abstract classes provide a common interface for all subclasses. They can define concrete methods that are common to all subclasses, and abstract methods that must be implemented by each subclass.
- This ensures a consistent API while allowing flexibility in the implementation.
- Partial Implementation:
- Abstract classes can contain both abstract and concrete methods. This allows for partial implementation, where some functionality is shared across all subclasses, and some functionality is specific to each subclass.
- This is useful for code reuse and maintaining a clean and understandable code structure.