wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Must subclass  call constructor of super class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Must subclass  call constructor of super class " Watch "Must subclass  call constructor of super class " New topic
Author

Must subclass call constructor of super class

kranthi kumar Vaddireddy
Greenhorn

Joined: Sep 30, 2005
Posts: 14
Must subclass call constructor of super class always and initialize its variable? What is the exception?


"SCJP 1.4 Certified"
Nico Van Brandt
Ranch Hand

Joined: Mar 31, 2011
Posts: 66

You have 2 options invoking super:

super();
--or--
super(parameter list);

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.


Oracle Java SE6 Certified Programmer
Oracle Java EE5 Certified Web Component Developer
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

You don't have to call a superclass constructor explicitly; it is not necessary to always have a super() or super(arguments) call in a subclass constructor. If you do not specify it, the compiler will automatically add a call to the no-arguments superclass constructor.

I don't like it when people add an explicit super() call (with no arguments), because it is superfluous:

If the superclass does not have a no-arguments constructor, then you must explicitly call super(arguments) in each subclass constructor.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
The details are in the Java™ Language Specification. But it is reluctant to open for me.

Simply: yes. You must initialise all the fields, so as to create your instance in a consistent state, ie fulfilling its class invariants. The only instance where you can get away without a super(...); call is if the superclass has an accessible no-arguments constructor. One must presume that constructor will put the superclass object into a consistent state.
I think the only state in which case it is good design not to initialise the fields in the superclass is when the superclass hasn't got any fields!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Must subclass call constructor of super class
 
Similar Threads
Is it true when super class constructor throws exception then sub class too should
inheritance question (I guess)
call constructor of super class?
Why I cannot extend a class from java.lang.Package
Clarifications/Suggestions in Java