confused with super

HiI never clearly understood the concept of "super()"I have gone through java docs of sun many times...but still it is unclear to me. Will anyone please kindly elucidate me ?Thanks a lot
[214 byte] By [Sangfroida] at [2007-11-27 0:21:18]
# 1

super() calls the superclass's default constructor.

class SuperClass

{

public SuperClass()

{

System.out.println("In super constructor");

}

public void method()

{

System.out.println("In super method");

}

}

class SubClass

{

public SubClass()

{

super(); //prints out "In super constructor"

super.method(); //prints out "In super method"

}

public static void main(String[] args)

{

new SubClass();

}

}

CaptainMorgan08a at 2007-7-11 22:15:05 > top of Java-index,Java Essentials,Java Programming...
# 2

> Hi

> I never clearly understood the concept of

>"super()"

Depends what you mean.

The "concept" of super in general is that it's a way to refer to member in the partent class.

"super()" as you provided is how you invoke the default no-arg constructor in the superclass from within a subclass constructor.

jverda at 2007-7-11 22:15:05 > top of Java-index,Java Essentials,Java Programming...
# 3

class SubClass extends SuperClass // *Correction-Needs to extend a class

{

public SubClass()

{

super(); //prints out "In super constructor"

super.method(); //prints out "In super method"

}

public static void main(String[] args)

{

new SubClass();

}

}

If you have a class for instance a class called Shape.

Then let's say you have a class called Circle that extends Shape

Now any public or protected method that was in the super class(Shape) can now be called inside of the child class (Circle)

So when you call super(); This calls the super class'es constructor that has no arguments.

If you called super(234, 24.94, "Hello World");

You would be calling the constructor of the super class that requires 3 arguments.

You can also call the public/protected methods and variables like this.

For instance, if you had a method inside of "Shape" defined as

//inside of Shape Class

public double getLength() { return length };

//Inside of Circle Class

public double getLength() { super.getLength() };

It can get a bit tricky if you have many methods inside of methods calling eachother.

lethalwirea at 2007-7-11 22:15:05 > top of Java-index,Java Essentials,Java Programming...
# 4

> class SubClass extends SuperClass //*Correction-Needs to extend a class

D'oh! Thanks for the correction.

//inside of Shape Class

public double getLength()

{

return length;

}

//Inside of Circle Class

public double getLength()

{

return super.getLength(); //<-- Correction :)

}

Also note that overriding the method is not needed in this case because it is already inherited. You could do:

//In Circle class

public double getCircumference()

{

return (Math.PI * super.getLength());

}

CaptainMorgan08a at 2007-7-11 22:15:05 > top of Java-index,Java Essentials,Java Programming...
# 5
And to add, a call to the super class constructor using super() should be placed as the first statement in the derived class constructor.
qUesT_foR_knOwLeDgea at 2007-7-11 22:15:05 > top of Java-index,Java Essentials,Java Programming...
# 6

> And to add, a call to the super class constructor

> using super() should be placed as the first statement

> in the derived class constructor.

Correction: must be first, eles it won't compile.

Constructor rules:

1) Every class has at least one ctor.

1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().

1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...}

if you want one.

1.3) Constructors are not inherited.

2) The first statement in the body of any ctor is either a call to a superclass ctor super(...)

or a call to another ctor of this class this(...)

2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super()

as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.

2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.

jverda at 2007-7-11 22:15:05 > top of Java-index,Java Essentials,Java Programming...
# 7
What's the difference between should and must now? Both virtually convey the same thing. *Must* sure does supply more momentum to the phrase, but there is no much discrepancy between what either of those two suggest.
qUesT_foR_knOwLeDgea at 2007-7-11 22:15:05 > top of Java-index,Java Essentials,Java Programming...
# 8

> What's the difference between should and must

"Must" is used of necessary or essential things. It is used where there is no

choice - like the placement of a constructor invocation.

"Should" is advisory. It is used when there is a choice and you are giving advice

as to which option is preferable.

But let's not get started or I'd have to explain "virtually", "momentum", "discrepancy"

and "either". If in doubt, consult a dictionary.

pbrockway2a at 2007-7-11 22:15:05 > top of Java-index,Java Essentials,Java Programming...