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!
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!
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).