This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes If abstract class cannot be instantiated, why is there a need for it to have a constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "If abstract class cannot be instantiated, why is there a need for it to have a constructor?" Watch "If abstract class cannot be instantiated, why is there a need for it to have a constructor?" New topic
Author

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

Yin Stadfield
Ranch Hand

Joined: Oct 13, 2012
Posts: 35

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
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

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?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2833
    
  11

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

Joined: Oct 13, 2012
Posts: 35

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.
 
 
subject: If abstract class cannot be instantiated, why is there a need for it to have a constructor?
 
Similar Threads
Abstract class & constructor?
Abstract Class Constructor
abstract class
Abstract Class
Abstract class