aspose file tools*
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
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: 43

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: 18840
    
  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: 2851
    
  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: 43

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If abstract class cannot be instantiated, why is there a need for it to have a constructor?