Explain Codes LogoExplain Codes Logo

Abstract class in Java

java
abstract-classes
oop
polymorphism
Alex KataevbyAlex Kataev·Dec 21, 2024
TLDR
// Abstract class with one abstract method public abstract class Shape { // Pure abstract method, no implementation, only sadness :( public abstract double area(); } // Concrete subclass implementing the abstract method public class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } // Implement abstract method to calculate area of a circle @Override public double area() { // Now that's a lot of area! return Math.PI * radius * radius; } } // Usage public class Main { public static void main(String[] args) { // A wild circle appeared! Shape circle = new Circle(5); // It's super effective! System.out.println("The area of the Circle is: " + circle.area()); } }

Key Takeaways:

  • An abstract class cannot be directly brought to life (instantiated).
  • An abstract method is a promise that must be kept by any brave subclass.
  • To leverage the abstract class's wisdom, you must instantiate a subclass (like Circle).
  • Override the abstract methods to flesh out your subclass.

Method distribution: Abstract vs. concrete methods

Abstract classes in Java provide the ingenious functionality to fuse together both cube-shaped (abstract) and meaty (concrete) methods. Provide a detailed implementation for methods that will be universal across all subclasses, and leave the rest as abstract!

About final in abstract classes

If you want to dictate the methods that children classes should definitely inherit without any changes, just prefix them with the final keyword. Prohibiting the override option gives parents total power!

Polymorphism: The shape-shifting magic

In Java, polymorphism allows us to think as if every object of a subclass is an object of its superclass too. It opens the routes to code flexibility, enabling you to write methods that don't need to change when new subclasses are introduced.

public void printShapeArea(Shape shape) { System.out.println("The area is: " + shape.area()); }

Now, no matter what type of shape you pass, Java will dynamically identify the right area() method to call.

Interfaces vs. Abstract classes: Who will win?

When an abstract class has only abstract methods and no concrete implementations, consider replacing it with an interface. Compare it to an assembly line where you provide the mechanism, and the classes do the assembling (i.e., implementation).

Risks with Abstract classes

While abstract classes sound like the superhero of OOP, they have their weaknesses and misuses that we should be aware of:

  • Rigidity: Java, unfortunately, only supports single inheritance. So use them sparingly!
  • Over Interface: Can be overly used in places where an interface would have been a better option.
  • Inflation: They can bloat your design if you have too many tiny abstract classes hanging around.

Now, let's visualize it!

Key Differences between Interfaces and Abstract Classes

While choosing between interfaces and abstract classes, it's critical to the design of your system. Here are a few starters:

Use an Abstract Class When:

  • You want to avoid code redundancy.
  • To establish a common trial for a group of related classes.
  • To create a skeleton version of a service.

Use an Interface When:

  • Multiple inheritances are required.
  • To establish contract compliance for unrelated classes.
  • To create a plug-n-play system of components.

Advantages and Pitfalls of Abstract Classes

Advantages:

  • Promotes code reuse and encapsulation.
  • Enables runtime polymorphism.
  • Abstracts commonly used methods.

Pitfalls:

  • Can create complexity and increase coupling.
  • You can extend only one class, even if it doesn't fully describe your object.