• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If abstract class cannot be instantiated, why is there a need for it to have a constructor?

 
Yin Stadfield
Ranch Hand
Posts: 58
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just curious. Am I missing something?
If abstract classes cannot be instantiated, why is there a need for it to have a constructor?
The constructor is only invoked when we use the new keyword. But since it's an abstract class, why have a constructor then?
 
Henry Wong
author
Marshal
Pie
Posts: 20821
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yin Stadfield wrote:Just curious. Am I missing something?
If abstract classes cannot be instantiated, why is there a need for it to have a constructor?
The constructor is only invoked when we use the new keyword. But since it's an abstract class, why have a constructor then?


Hint: when you instantiate a class, does the constructor of that class' superclass ever gets called?

 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes cannot be directly instantiated, but one of its constructors will run when you instantiate one its subclasses. If the subclass's constructor doesn't specify which abstract superclass constrictor to use ... by using the super(...) call ... then the superclass's default, no argument constructor will be called automatically.
 
Yin Stadfield
Ranch Hand
Posts: 58
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Yin Stadfield wrote:Just curious. Am I missing something?
If abstract classes cannot be instantiated, why is there a need for it to have a constructor?
The constructor is only invoked when we use the new keyword. But since it's an abstract class, why have a constructor then?


Hint: when you instantiate a class, does the constructor of that class' superclass ever gets called?



Greg spoils the fun. lol. Thanks, makes sense now.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic