• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Super ()

 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you dont put super() or this() in a constructor, the compiler will put a call to super() as the first statement automatically
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sky su
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jegon Jeganathan Renganathan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic