Explain Codes LogoExplain Codes Logo

Can we instantiate an abstract class?

java
anonymous-inner-classes
instantiation
abstract-classes
Anton ShumikhinbyAnton Shumikhin·Oct 12, 2024
TLDR

No, direct instantiation of an abstract class is not possible in Java. Instead, concrete subclass implementations are needed for instantiation. Use a subclass to instantiate and invoke all abstract class methods:

abstract class Vehicle { abstract void move(); // The vehicles that can't move haven't been invented yet! } class Car extends Vehicle { void move() { System.out.println("Car moves"); // Vroom Vroom! } } // Car object, Vehicle reference. // Because who doesn't want a car that behaves like a vehicle? Vehicle vehicle = new Car(); vehicle.move(); // Output: Car moves. Of course, it does!

Here, the Car of instantiates the Vehicle class - it's like having the cake and eating it too!

Under the hood: Instantiation of abstract classes

While an abstract class cannot be instantiated head-on, we do have some tricks up our sleeve: extending an abstract class with a concrete subclass and clever use of anonymous inner classes. Here we deep dive into the machinations behind the scenes.

Instantiate using Anonymous inner classes

In java, anonymous inner classes are the trojan horses that allow you to declare and instantiate a class at once. Have an abstract class AbstractEvent you need to implement? - no worry, anonymous inner class to the rescue!

abstract class AbstractEvent { abstract void perform(); // Just do it! } // ... AbstractEvent event = new AbstractEvent() { void perform() { System.out.println("Event performed"); // And the crowd goes wild! } }; event.perform(); // Output: Event performed

Here, we define an anonymous class that extends AbstractEvent and instantiates it at the same time. The class name is generally assigned after compilation following the pattern OuterClass$1.class.

Calling Superclass constructor

The constructor of the superclass (AbstractEvent in this case) is invoked when a new instance of an anonymous inner class is created. This initializes the object before the reference is returned - basically, like ensuring the guests are comfortable before the party starts!

Memory allocation during instantiation

During the instantiation process, memory is allocated for instance variables of the anonymous subclass and its superclasses. Now we know where all the extra memory goes!

Knowledge of abstract class instantiation

Cracking a technical interview can be as tricky as instantiating an abstract class, and even the slightest knowledge about abstract class instantiation can give you a leg up!

Brushing up on JLS

Want to deep dive into the rules of the game? Look no further than the Java Language Specification (JLS). Section 15.9.1 will definitely add a feather to your Java cap.

Code examples

Nothing brings more joy to us developers than a piece of well-commented code - add AbstractEvent to your coding repertoire and watch the magic unfold!

Peeking into class files

Once you compile the code, you can see the anonymous classes as separate class files named OuterClass$1.class, OuterClass$2.class, etc - It's like revealing the secret identity!

Order of creation

The quest for order in the chaos ends with the anonymous class names - numerical order in the names indicates the sequence of creation. Talk about an orderly compilation!

The transformation

Instantiating an anonymous subclass is like turning the unconstructable blueprint into a house that we can live in, effectively bringing the code to life!

Memory allocation detail

The subclass instance that is created also becomes an instance of its super classes and interfaces - It carries its entire ancestral lineage in its memory footprint!

Clarifying the misconception

Instantiating an abstract class directly is as impossible as fitting an elephant into a fridge. However, providing an anonymous implementation is like training a mouse to play fetch - it's doable but isn't the same as fitting an elephant in a fridge!