GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Hello ranchers


why to WRITE SUPER() explicitly. what it does ? is that for initializing STATE of a class or any other reason ?


thank you ranchers
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
what happens to the following code..

class A {

A (String name) {}
}

class B extends A {

B() {
super(); =======do we need to write super (String name)
}


Thanks rancher
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by chintan ramavat:
what happens to the following code... do we need to write super (String name)...

What happens when you compile this?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
actually just want to know about SUPER. ? what it does ? if you give an example that would be great.


thank you
Mamatha Preetham
Ranch Hand

Joined: Jan 23, 2007
Posts: 75
Hi Chintan,

As Marc said, you should compile the code and see what does the compiler do....

super() is a keyword used to call the base class's constructor. The rule is that super() must be the first line in every constructor.

If you dont provide it, the compiler will automatically provide it with no arguments.

For the code which you had posted, the solution will be,
class A {

A (String name) {}
}

class B extends A {

B() {
super("Hi"); //yes, we do need to write super (String name), since the super class constructor takes String as argument
}
}

Cheers,
Mamatha
SCJP(1.4)
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Thanks mamatha
 
GeeCON Prague 2014
 
subject: Constructor