Abstract Class in Java: An abstract class in Java is a class that cannot be instantiated directly. It serves as a blueprint for other classes, providing a common structure and methods that can be inherited.
- An abstract class in Java is declared using the abstract keyword.
- It can have both abstract methods (without a body) and non-abstract methods (with a body).
- An abstract class cannot be instantiated directly.
- Subclasses must extend an abstract class and provide implementations for all abstract methods, or the subclass itself must be declared abstract.
Understanding Abstract Class in Java
- Abstraction is the concept of hiding the internal details and showing only essential information to the user.
- This simplifies complex systems by reducing the focus to the most important elements.
Example of Abstraction:
- When sending an SMS, you only type the message and send it, but you don’t know or need to know the internal workings of how the message is delivered.
Ways to Achieve Abstraction
- Abstract class: Provides partial abstraction (0-100%).
- Interface: Provides full abstraction (100%).
Read also: 50 Best MCQs on Increment and Decrement Operators in Java
Abstract Class in Detail
Key Points to Remember
- It must be declared with the abstract keyword.
- It can have both abstract and non-abstract methods.
- Cannot be instantiated.
- Can have constructors, static methods, and final methods.
- Final methods in an abstract class prevent subclasses from modifying the method’s body.
Example:
abstract class Animal {
// Abstract method (does not have a body)
abstract void sound();
// Non-abstract method (has a body)
void sleep() {
System.out.println("Sleeping...");
}
}
Read also: Difference between HashSet vs TreeSet in Java, Best Tutorial of HashSet vs TreeSet 2024
Abstract Method in Java
- An abstract method in Java is a method declared without an implementation (no method body).
- Subclasses that extend an abstract class must implement all abstract methods unless they are also abstract.
Example:
abstract class Vehicle {
abstract void start(); // Abstract method
}
class Car extends Vehicle {
void start() {
System.out.println("Car is starting");
}
}
Example: Abstract Class with Abstract Method
In this example, the Bike class is abstract, and its method run() is implemented by a subclass Honda.
abstract class Bike {
abstract void run(); // Abstract method
}
class Honda extends Bike {
void run() {
System.out.println("Honda is running safely");
}
public static void main(String[] args) {
Bike myBike = new Honda();
myBike.run();
}
}
Output:
Honda is running safely
Real-World Example: Abstract Class with Factory Method
In this scenario, Shape is the abstract class, and Rectangle and Circle provide specific implementations of the draw() method.
// Abstract Class with Factory Method
abstract class Shape {
abstract void draw();
}
class Rectangle extends Shape {
void draw() {
System.out.println("Drawing rectangle");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing circle");
}
}
class TestAbstraction {
public static void main(String[] args) {
Shape s = new Circle();
s.draw(); // Output: Drawing circle
}
}
Another Example: Bank Abstract Class
Here, the Bank abstract class contains the getRateOfInterest() method and subclasses SBI and PNB provide specific implementations.
abstract class Bank {
abstract int getRateOfInterest();
}
class SBI extends Bank {
int getRateOfInterest() {
return 7;
}
}
class PNB extends Bank {
int getRateOfInterest() {
return 8;
}
}
class TestBank {
public static void main(String[] args) {
Bank b;
b = new SBI();
System.out.println("SBI Rate of Interest: " + b.getRateOfInterest() + "%");
b = new PNB();
System.out.println("PNB Rate of Interest: " + b.getRateOfInterest() + "%");
}
}
Output:
SBI Rate of Interest: 7%
PNB Rate of Interest: 8%
Abstract Class with Constructor, Data Members, and Methods
An abstract class can have a constructor, data members, and both abstract and non-abstract methods. Below is an example:
// Abstract Class with Constructor
abstract class Bike {
Bike() {
System.out.println("Bike is created");
}
abstract void run();
void changeGear() {
System.out.println("Gear changed");
}
}
class Honda extends Bike {
void run() {
System.out.println("Running safely...");
}
public static void main(String[] args) {
Bike bike = new Honda();
bike.run();
bike.changeGear();
}
}
Output:
Bike is created
Running safely…
Gear changed
Rules and Best Practices
- If a class contains an abstract method, the class must be declared as abstract.
abstract class Bike {
abstract void run();
}
- If a subclass does not provide an implementation for an abstract method, the subclass must also be declared as abstract.
Real-World Example: Abstract Class Implementing an Interface
An abstract class can implement an interface without being forced to implement all its methods. This allows subclasses to focus on the methods they need to implement.
interface A {
void a();
void b();
void c();
void d();
}
abstract class B implements A {
public void c() {
System.out.println("I am C");
}
}
class M extends B {
public void a() {
System.out.println("I am A");
}
public void b() {
System.out.println("I am B");
}
public void d() {
System.out.println("I am D");
}
}
class TestInterface {
public static void main(String[] args) {
A obj = new M();
obj.a();
obj.b();
obj.c();
obj.d();
}
}
Output:
I am A
I am B
I am C
I am D
Abstract Class in Java and Abstract Method in Java Example
// Abstract class
abstract class Animal {
// Abstract method (no body)
public abstract void sound();
// Non-abstract method
public void sleep() {
System.out.println("This animal is sleeping.");
}
}
// Dog class inherits from Animal and provides implementation for the abstract method
class Dog extends Animal {
public void sound() {
System.out.println("The dog says: Bark");
}
}
// Cat class inherits from Animal and provides implementation for the abstract method
class Cat extends Animal {
public void sound() {
System.out.println("The cat says: Meow");
}
}
// Main class to test the abstract class and its implementation
public class AbstractClassExample {
public static void main(String[] args) {
// Creating instances of the subclasses
Animal myDog = new Dog();
Animal myCat = new Cat();
// Calling the abstract method implemented by subclasses
myDog.sound(); // Output: The dog says: Bark
myCat.sound(); // Output: The cat says: Meow
// Calling the non-abstract method from the abstract class
myDog.sleep(); // Output: This animal is sleeping.
myCat.sleep(); // Output: This animal is sleeping.
}
}
Summarize the Abstract Class and Method
Feature | Description |
Abstract Class | A class that cannot be instantiated directly. It serves as a blueprint for other classes. |
Abstract Methods | Methods declared with the abstract keyword and have no implementation. Subclasses must provide concrete implementations. |
Non-Abstract Methods | Methods with a body that can be used directly in the abstract class or inherited by subclasses. |
Inheritance | Subclasses can inherit both abstract and non-abstract methods from the abstract class. |
Polymorphism | Objects of different subclasses can be treated as objects of the same abstract class type. |
Code Reusability | Abstract classes provide a way to define common functionality that can be inherited by multiple subclasses. |
Example | The Animal class is abstract, while Dog and Cat are concrete subclasses that implement the makeSound() method. |