0

I'm learning OOP concepts. While reading about inheritance, i learnt that the super class has to be initialized before initializing the subclass i.e. constructor of all super-class must run before sub-class constructor. Also, we can create an instance of super class directly. For e.g.

SuperClass superClass = new SuperClass();

Now, I came across abstract classes. It seems we cannot instantiate an abstract class. To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.

My question is, while instantiating concrete subclass, constructor of abstract super class will be invoked before constructor of concrete subclass. If this is the case why can't i instantiate Abstract super class directly?

Shrirang
  • 198
  • 1
  • 10
  • You misunderstood. It's true that the super class must be initialized before the subclass, but Java will do that for you. You don't create a separate instance of the super class yourself. – Andreas Jun 13 '19 at 09:38
  • *"all the super class constructors must run"* Not true. Unless you specifically code a constructor in the super class to call another constructor by using `this(...)`, only one constructor of the super class will be run per object instance. – Andreas Jun 13 '19 at 09:38
  • 5
    *"why can't i instantiate Abstract super class directly?"* Because the class is declared abstract. --- This is usually because there is an abstract method, i.e. a method without a body, so you cannot create an instance. You need a subclass to implement the method. – Andreas Jun 13 '19 at 09:39
  • "why can't i instantiate Abstract super class directly?" because it is abstract, and thus may contain abstract methods. – Maurice Perry Jun 13 '19 at 09:41
  • @Andreas, I framed the sentence "all the super class constructors must run" incorrectly. Apologies. I meant to say, the constructor of all super-classes must run... – Shrirang Jun 13 '19 at 09:50
  • @Andreas, So JVM will internally invoke the constructor of abstract super class. But we cannot do that using "new keyword", Correct? – Shrirang Jun 13 '19 at 09:54
  • 1
    @Shrirang Correct, unless you do it explicitly using `super()`, which is required if the constructor has parameters. – Andreas Jun 13 '19 at 14:07

1 Answers1

3

An abstract class is like a bike, without wheels. It has spaces defined for wheels, but the bike frame seller leaves the type of wheels to the final seller. It just points out where the wheels should attach to the frame and which bolts to be used. The bike has a chain, leading to where the back wheel should go, so that when the peddles are turned, the back wheel will be propelled forward.

Bike without wheels.

You can try riding that bike, but it won't move without wheels attached. An error will occur when the peddles are turned because there is no wheel to turn.

You need the concrete seller to define the wheels will operate, as long as they are attached in the pre-defined way.

square wheel bike
Image by https://www.flickr.com/photos/vrogy/, licensed CC-BY 2.0

So trying to ride the bike without wheels is you trying instantiate the abstract class.
Even if you get it working it's useless and will throw errors when you peddle forwards due to lacking wheels.

When you instantiate the square wheel bike, it will work properly, and move you forward when you peddle forwards.

The instantiation of the superclass is purely for the chain to be attached, the steeringhweel being able to move, the height of the saddle being set, all the standard features that do not include the wheels being set. Wheels are not the abstract bike's concern. That's the concrete classes concern to set up.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133