• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Must subclass call constructor of super class  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Must subclass call constructor of super class always and initialize its variable? What is the exception?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 61774
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to specify a call to super when your superclass has no default constructor:

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!