1

I'm a beginner in java, and I was wondering why java actually allows an abstract class to have a constructor if It is not allowed to have an object? Where as in interface it is not allowed.

Thanks in advance!

Faisal
  • 35
  • 3
  • 4
    An abstract class can have data members, and you can extend an abstract class, so it makes sense that a derived class might want to call the abstract class constructor to initialize the abstract class members – Remy Lebeau Mar 31 '18 at 21:05
  • Duplicate of this https://stackoverflow.com/q/2170500/6253321 – cpp beginner Mar 31 '18 at 21:18
  • Sub-classes of abstract classes can be created and all the fields need to be initialised. – Peter Lawrey Apr 01 '18 at 01:03

1 Answers1

0

It is important to have a constructor for constructor chaining to happen. If Abstract class were not supposed to have constructors then chaining from subclass to Object class would not happen. The design behind not allowing Abstract class to have objects is that It serves as a base which itself is not interacting but its child classes very well are,

A very basic analogy,

 abstract class AquaticAnimal{

 }

 class Duck extends AquaticAnimal{

 }

 class Fish extends AquaticAnimal{

 }

If Duck has to eat(read interact) it will eat(interact with) Fish(Both are Sea Animals, but real life interaction is happening between Duck and Fish).

  • see this bout answering to questions marked as duplicated: https://meta.stackoverflow.com/a/312320/833336 – emecas Mar 31 '18 at 22:17