• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Super

 
Mike Shn
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHen the keyword super is using? I saw a couple of times, for example
class X {
}
class y etends X {
super(x,y)
}
In other words, the super word use for Constructor.. Why is the prupose of that statement?
Thanks
 
Erik Dark
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use super when you refer to the parent constructors, methods or variables. When you use it to refer to the parent's class constructor it must be the first statement in the (subclass') constructor ...
Erik Dark
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not still clear on purpose of Super. If you can extends superclass, you can directly use it's methods and variable. Then why we have to use keyword "Super"???
Thanks
Angela
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the example you gave you passed some parameters to the super constructor. That was a very appropriate example (probably more so than you realized).
Imagine that the super class does some initialization using those variables to either an attribute or to a method that can be used by the derived class. So what you are doing is making sure that this proper initialization is being done by the super class before anything is done by your derived class.
That is also, by the way, the reason why the super() constructor must be the first thing in your constructor if you need to use the "super" keyword.
 
Erik Dark
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can extends superclass, you can directly use it's methods and variable

But what if you have overridden the superclass' methods or shadowed the variables...??
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine that the super class does some initialization using those variables to either an attribute or to a method that can be used by the derived class. So what you are doing is making sure that this proper initialization is being done by the super class before anything is done by your derived class.
But Why we want to intialize the variables of subclass into the superclass constructor. We can do it in subclass itself!!!
Angela
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela, you are not initializing the variables of the subclass. You are initializing the super class variables, so that they are in their initialized state if and when the subclass needs to access them. Since the subclass ia a type of the superclass, you want to make sure that the superclass is well constructed.
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela :
Think of the super constructor in terms of real world objects.
Imagine gasoline powered vehicle as a super class and all different kinds of vehicles such as a convertible, a sedan, a 4X4 and a pickup truck as descendant classes. For the sake of discussion, imagine that the subclasses all have the same engines, everything else is different in each subclass.

The superclass constructor can create the engine. The subclass constructors can add the breaks, and frame and windows and roofs etc. specific to each type of vehicle.
The code will be somewhat like this

Notice that the code to create the engine is only in one place. All the derived classes or subclasses simply call that code. If you came up with a better way of creating the engine, you change that code only in one place and not in the constructor of Sedan, Convertible, 4X4 and so on.
HTH
[ February 05, 2002: Message edited by: Shivaji Marathe ]
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all,
I got the purpose of Super keyword.
Shivaji,The last thing I want to ask you is that in Convertible, Sedan subclasses, when we used super(Capacity); Are we passing the value of capacity which will be the initialize value of capacity variable of Vehicle class.
Thanks
Angela
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela :
When any subclass is constructed, the super class is always constructed first.
If the constructor for the subclass does not explicitly call the superclass constructor then
the compiler implicitly calls the no-arguments constructor of the superclass. This topic is a very popular exam question in several mock exams.
The 'super(Capacity)' line is the explicit call to the super class constructor. The super refers to the immediate ancestor of the object. If there are multiple constructors in the ancestor, the compiler will choose the most appropriate one based on the type and order of arguments provided in the subclass.
HTH
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic