• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Lorraine Batol
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic