This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Using the Keyword super  RSS feed

 
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i read the following in the sun java tutorials:


source: http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html
You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

source: http://java.sun.com/docs/books/tutorial/java/IandI/super.html
Using the Keyword super
Accessing Superclass Members
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged).

Invocation of a superclass constructor must be the first line in the subclass constructor.
The syntax for calling a superclass constructor is

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

With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.

--------------------------------------------------------------------------------
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.
--------------------------------------------------------------------------------

If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.


still, i have some doubts:
1) "(...)write a subclass constructor that invokes the constructor of the superclass, either implicitly(...)" means not explicitly calling super() inside my constructor?
2) "It is called constructor chaining, and you need to be aware of it when there is a long line of class descent." why should i be aware?
3) please take a look at the following code: as i dont need to override anything from superclass i dont need to call super() and anyway the compiler does it for me; am i right?
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still, i have some doubts:
1) "(...)write a subclass constructor that invokes the constructor of the superclass, either implicitly(...)" means not explicitly calling super() inside my constructor?
2) "It is called constructor chaining, and you need to be aware of it when there is a long line of class descent." why should i be aware?
3) please take a look at the following code: as i dont need to override anything from superclass i dont need to call super() and anyway the compiler does it for me; am i right?


1) Yes. If you do not include a call to super();, the compiler will automatically put one in for you.
2) Because the SuperClass constructor might do something that you need to be aware of. It could set a value to some variable, or perhaps create another object you need. Think about it like this: would you call a random method in your code without knowing what it does?
3) You wouldn't need to explicitly call super(); However, overriding and the use of super is different from constructor chaining and super();. The keyword super is used to access something from a class's superclass, whereas super() calls the superclass's no-arg constructor.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!