This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Super () Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Super ()" Watch "Super ()" New topic
Author

Super ()

RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
Hello,

How this code is compiled, the MySuper class without call to super(). I ready the book it says any one of the constructors in a class should call the super ().
Pls correct me if I am wrong.

1. class MySuper {
2. public MySuper(int i) {
3. System.out.println("super " + i);
4. }
5. }
6.
7. public class MySub extends MySuper {
8. public MySub() {
9. super(2);
10. System.out.println("sub");
11. }
12.
13. public static void main(String [] args) {
14. MySuper sup = new MySub();
15. }
16. }


Thanks, Raghu.K
sirisha sirisha
Ranch Hand

Joined: Apr 17, 2006
Posts: 39
i think if you donot put the call to super() , the compiler will insrt it for you.check the s&b book that shows what happens for the different types of constructor.here the compiler will insert that super() call for you.since you did not pride for one.
s&b page 318 table.
please correct me if i am wrong.
Rupak Khurana
Ranch Hand

Joined: Mar 01, 2005
Posts: 89
If you dont put super() or this() in a constructor, the compiler will put a call to super() as the first statement automatically


SCJP 1.5<br />SCWCD 1.4
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

It is not mandatory that you call super(). The compiler will do it for you. However if you do call super() or this(), then you need to call it on the first line. If you plan to use variables in the call then you can only use static ones.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
It complied because you wrote explict constructor for MySub with no arguments and created object of the class by calling constructor with no arguments that in turn called super class constructor with one int argument, everything is fine so it compiled.

if it were MySuper sup=new MySuper();
then compile time error might have occured.


Kiran Kumar.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

The very minute you add this - > public class MySub extends MySuper

MySuper must have a no arg constructor or it wont compile. try it out.
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
it is not necessary to call super(), if you dont write any constructor in subclass(same with any class) then compiler automatically inserts one default constructor and its first line would be super(),

but in your example there is no constructor in superclass with zero arguments(compiler will not depend on default implicit construtor if you wrote one explicit constructor),so it is necessary to define explicit constructor in sub class and write super(int) in it otherwise whom would super() call.

I hope it is clear somewhat to you.
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
First Rule:- If we provide an over loaded constructor, default constructor is not provided. So in case of MySuper, 'MySuper()' will not be provided as we had an explictly defined 'MySuper(int i)'.

Second Rule:- Whenever call to child constructor is generated, first its parent construtor is called. So In this case if you say like this

It will give error as compiler will try to call MySuper() implicitly but for MySuper Class default no argument constructor doesn't exist.

As you are explicitly calling super(2) to fullfill second rule there for your aren't receiving any error.

Hope it Helps


"Know where to find the solution and how to use it - that's the secret of success."
sky su
Greenhorn

Joined: Apr 19, 2006
Posts: 6
Hi!
I deem you must read that book more than once!
If you don't put super() or this() in a constructor, the compiler will put a call to super() as the first statement automatically.iif you
don't put super(),you will get complie error!
Jegon Jeganathan Renganathan
Greenhorn

Joined: Apr 21, 2006
Posts: 6
Originally posted by RAGU KANNAN:
Hello,

How this code is compiled, the MySuper class without call to super(). I ready the book it says any one of the constructors in a class should call the super ().
Pls correct me if I am wrong.

1. class MySuper {
2. public MySuper(int i) {
3. System.out.println("super " + i);
4. }
5. }
6.
7. public class MySub extends MySuper {
8. public MySub() {
9. super(2);
10. System.out.println("sub");
11. }
12.
13. public static void main(String [] args) {
14. MySuper sup = new MySub();
15. }
16. }


Thanks, Raghu.K


When Going gets Tougher, the Tough gets Going
Jegon Jeganathan Renganathan
Greenhorn

Joined: Apr 21, 2006
Posts: 6
To my knowledge if you don't have any constructor in Super(Parent) class then, the JVM will add a "Default Constructor" which is nothing but a empty constructor like the one given below
MySuper(){}
So even if you create any Constructor in child class it will not lead to compilation issue. But if you added an explicit constructor say
MySuper()
{
System.out.println("MySuper");
}
Then in the child class you need to add the super() explicitly or else it will leads to a Compilation issue.
MySub()
{
super();
System.out.println("MySub");
}
Hope you are clear now.

By the way the output for your code is

It will execute the Parent(Super) constructor first (Because the Object reference is of MySuper type) and then only the MySub(Child) will be executed.

so the output is

super2
sub
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Super ()
 
Similar Threads
a beginner wanna knows something about this( )
super(x,y) and this() in one constructor
Overriding and Overloading
try/catch with exception casting
Overriding and Overloading.