Exploring Abstract Classes and Abstract Methods in Java

Understanding Partial Abstraction and Common Methodology

Nikita Chaurasia
Javarevisited
4 min readMay 6, 2024

--

Abstract classes and abstract methods are fundamental concepts in Java programming, offering a way to achieve partial abstraction and streamline code organization. In this article, we’ll delve into what abstract classes and methods are, their advantages, and how to use them effectively in your Java projects.

Abstract Classes:
Let’s start with abstract classes. An abstract class in Java serves as a blueprint for other classes. It can have both abstract (incomplete) methods and concrete (complete) methods. However, abstract classes cannot be instantiated on their own; they need to be extended by subclasses.

Abstract Methods:
An abstract method is a method declared without implementation. It provides a common interface for all subclasses but leaves the implementation details to be defined by each subclass. Abstract methods are declared using the `abstract` keyword and terminated with a semicolon (;). Any class that contains at least one abstract method must be declared as an abstract class.

Why Use Abstract Methods:
Abstract methods simplify coding by allowing us to define a common method name across multiple classes without needing to implement it in each one separately. This is particularly useful when dealing with classes that share common functionality but have different implementations.

Code Examples:
Let’s illustrate abstract classes and methods with some code examples.

abstract class Car 
{
protected int speed = 80;
public Car()
{
System.out.println("Car class Constructor!!");
}
public void getDetails()
{
System.out.println("It has 4 wheels and one Engine");
}
public abstract void run();
}

class Honda extends Car
{
@Override
public void run()
{
System.out.println("Honda Car is Running");
}
}

public class Main
{
public static void main(String[] args)
{
Car c = new Honda();
System.out.println("Speed is :" + c.speed);
c.getDetails();
c.run();
}
}

Code Explanation:
Here, we define an abstract class Car with a concrete method getDetails() and an abstract method run(). The Honda class extends Car and implements the run() method. In the main() method, we demonstrate accessing properties and methods of the abstract class through a subclass instance, showcasing abstraction and inheritance.

abstract class Alpha 
{
public abstract void show();
public abstract void demo();
}

abstract class Beta extends Alpha
{
@Override
public void show()
{
System.out.println("Show method implemented in Alpha class");
}
}

class Gamma extends Beta
{
@Override
public void demo()
{
System.out.println("Demo method implemented in Beta class");
}
}

public class AbstractDemo
{
public static void main(String[] args)
{
Gamma g = new Gamma(); g.show(); g.demo();
}
}

Code Explanation:
In this example, we have a hierarchy of abstract classes Alpha and Beta, where Beta extends Alpha. Alpha declares two abstract methods show() and demo(), with Beta providing an implementation for show(). Subclass Gamma extends Beta and implements demo(). In the main() method, we instantiate Gamma and demonstrate method invocation, highlighting the importance of method implementation in subclasses of abstract classes.

abstract class Bird 
{
public abstract void fly();
}

class Parrot extends Bird
{
@Override
public void fly()
{
System.out.println("Parrot can fly");
}
}

class Sparrow extends Bird
{
@Override
public void fly()
{
System.out.println("Sparrow can fly");
}
}

public class BirdDemo
{
public static void main(String[] args)
{
Bird b;
b = new Parrot();
b.fly();
b = new Sparrow();
b.fly();
}
}

Code Explanation:
In this code, we have an abstract class Bird with an abstract method fly(). Two concrete subclasses Parrot and Sparrow extend Bird and provide their implementations of the fly() method. In the main() method, we demonstrate polymorphism by creating instances of Parrot and Sparrow and invoking their fly() methods.

Advantages of Abstract Classes:
Certainly! Here’s a breakdown of both advantages and disadvantages of abstract classes:

1. Partial Abstraction: Abstract classes enable partial abstraction by defining common method signatures without implementation, promoting code reuse and modularity.

2. Enforcing Method Implementation: They ensure that essential methods are implemented by subclasses, promoting code consistency and reducing the risk of incomplete implementations.

3. Polymorphism: Abstract classes facilitate polymorphism, allowing objects of different subclasses to be treated interchangeably through a common abstract type, enhancing code flexibility and extensibility.

4. Template Design Pattern: Abstract classes are pivotal in implementing the template design pattern, providing a skeletal structure with placeholders for specific steps that subclasses can fill in, promoting code reusability and maintainability.

Disadvantages of Abstract Classes:

1. Limited Inheritance: Java does not support multiple inheritance, so if a class already extends an abstract class, it cannot extend another class, limiting flexibility in class design.

2. Complexity: Abstract classes can introduce complexity, especially in large projects with deep inheritance hierarchies, making code maintenance and debugging more challenging.

3. Tight Coupling: Subclasses are tightly coupled with the abstract class, as they must adhere to its structure and implementation, potentially reducing the flexibility to change or refactor the abstract class without affecting subclasses.

4. Runtime Overhead: Using abstract classes may introduce some runtime overhead due to dynamic method binding and polymorphism, although modern JVM optimizations help mitigate this impact to a certain extent.

Conclusion:
Abstract classes and methods are powerful tools in Java for achieving partial abstraction and code reusability. By understanding their usage and advantages, you can write more efficient and maintainable code in your Java projects. Incorporating abstract classes and methods into your programming toolkit opens up a world of possibilities for creating flexible and scalable software solutions.

Thanks for reading

👏 Clap for the story and follow me

📰 Read more content on my Medium

--

--