File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Super" Watch "Super" New topic
Author

Super

Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
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

Joined: Jan 28, 2002
Posts: 107
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

Joined: Jan 16, 2002
Posts: 469
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

Joined: Jan 23, 2002
Posts: 144
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.


For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Erik Dark
Ranch Hand

Joined: Jan 28, 2002
Posts: 107
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

Joined: Jan 16, 2002
Posts: 469
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

Joined: Nov 06, 2000
Posts: 1510
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
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

Joined: Jan 16, 2002
Posts: 469
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

Joined: Jan 11, 2002
Posts: 203
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
 
jQuery in Action, 2nd edition
 
subject: Super
 
Similar Threads
Multiple inheritance in java
Confused about this output-Constructor doubt
Abstract classes - instantiation
Static Doubt
Problem with instance initializer