Blog Java

Understanding Abstract Class and Anonymous Class in Java

Java, as an object-oriented programming language, provides several powerful features, two of which are abstract classes and anonymous classes. In this blog post, we will delve into these concepts and understand how they work together.

Abstract Classes

In Java, an abstract class is a class that cannot be instantiated directly. It often contains one or more abstract methods, which are methods declared without any implementation. These classes represent a concept which is not fully defined and hence, cannot be instantiated directly.

Here’s an example of an abstract class:

Java
abstract class AbstractClass {
    abstract void abstractMethod();
}

In the above code, AbstractClass is an abstract class with an abstract method abstractMethod(). This class cannot be instantiated directly because it’s not fully defined.

Anonymous Classes

An anonymous class in Java is a class that is defined and instantiated in a single statement without a name. They are often used when you need to create an instance of an object with certain “extras” such as overriding methods from its superclass or implementing methods from an interface.

You can create instance of an anonymous class that extends an abstract class but but not the instance of abstract class:

Java
public class AbstractClassDemo {
    public static void main(String[] args) {
        AbstractClass abstractClassObject = new AbstractClass() {
            @Override
            void abstractMethod() {
                System.out.println("Implementing the Abstract class Method.");
            }
        };
        abstractClassObject.abstractMethod();
    }
}

In the above code, new AbstractClass() {...} is defining an anonymous class that’s a subclass of AbstractClass, and it’s providing an implementation for the abstract method abstractMethod(). Then it’s creating an instance of this anonymous class. The reference to this object is of type AbstractClass, so it can be used to call any methods defined or inherited by AbstractClass.

Conclusion

In conclusion, while you cannot create an instance of an abstract class directly in Java, you can create an instance of an anonymous class that extends the abstract class. This anonymous class acts like an inner class that extends the abstract class, and it belongs to your anonymous class. This is a powerful feature of Java that allows for greater flexibility and reusability in your code.

Avatar

Neelabh

About Author

As Neelabh Singh, I am a Senior Software Engineer with 6.6 years of experience, specializing in Java technologies, Microservices, AWS, Algorithms, and Data Structures. I am also a technology blogger and an active participant in several online coding communities.

You may also like

Blog Design Pattern

Understanding the Builder Design Pattern in Java | Creational Design Patterns | CodeTechSummit

Overview The Builder design pattern is a creational pattern used to construct a complex object step by step. It separates
Blog Tech Toolkit

Base64 Decode

Base64 encoding is a technique used to encode binary data into ASCII characters, making it easier to transmit data over