• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance: use of super  RSS feed

 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, good evening

I have a question about the inheritance, and in particular the use of super for constructor and for access to members of Superclass:
Constructor, the constructor of subclass use keyword super , passing the specific agrument.
Assuming following code of superclass:


subclass:


I read that variable superclass can refer to object derived from superclass, in this case Box can refer to object BoxWeight, but no understand the reason.. Then, how happens this thing at compile?!

If someone manages to fully understand this concept in mind, I am grateful
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructors for both Box and BoxWeight are copy constructors. Your BoxWeight constructor takes another BoxWeight object as an argument and will make a copy of it which is another BoxWeight object. Now this is fine for making duplicates of the weight value but because it is derived from a superclass you also need a copy constructor there which will make duplicates of width, height, and depth. The superclass can access the ob passed to the BoxWeight class because you are passing ob to the superclass's copy constructor.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, clear!

Instead, regarding use of super for access to members or method... example:
method of superclass


method of subclass


why call the superclass method, then if I have to rewrite part of the method is the same?

Maybe, because those methods / variables belong to the superclass?
i.g. getNome() in subclass not written, but it inherits.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The subclass does not need to call the superclass's getInfo() but the superclass still needs getInfo because it is a class in its own right.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch! Those lines are horribly long and I have broken them so you can see how to do it.

Only use \n if you have been told to supply the LF character. The correct thing to write is shown in the Java® Tutorials (search for “\n”).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!