• Post Reply Bookmark Topic Watch Topic
  • New Topic

a question about sub\super class  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Super {
private int s = 100;

Super() {
abc();
}

void abc() {
System.out.println("s " + s);
}
}
class Sub extends Super {
private int s1 = 200;

void abc() {
System.out.println("s1 " + s1);
}


public static void main(String[] args) {
new Sub();
}

}

why the output is "s1 0"
who knows?
thanks in advance...
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us explain what is happening here.

When you call new Sub(), the parent class's (Super) constructor gets invoked. why because, there is no constructor for Sub. So the call propagates in the chain and goes to parents constructor.

Now, when you call abc() in the super class's constructor, the abc() method in Sub is called. Why because remember you invoked new Sub(). But when that call gets executed from the Super's constructor, the variable s1 is not visible.

The result is s1 0

Hope this helps!

Sincerely,
your friends at JavaAdvice.com
http://www.JavaAdvice.com - The one stop resource for all your Java questions and answers
[ January 23, 2008: Message edited by: Lukasz Bajzel ]
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lukasz Bajzel:
... When you call new Sub(), the parent class's (Super) constructor gets invoked. why because, there is no constructor for Sub. So the call propagates in the chain and goes to parents constructor...

There is a constructor for Sub. Because no constructors are explicitly defined for Sub, the compiler inserts a default no-args constructor, which simply calls the no-args version of the superclass constructor.
 
qd wangqun
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lukasz Bajzel:
Let us explain what is happening here.

When you call new Sub(), the parent class's (Super) constructor gets invoked. why because, there is no constructor for Sub. So the call propagates in the chain and goes to parents constructor.

Now, when you call abc() in the super class's constructor, the abc() method in Sub is called. Why because remember you invoked new Sub(). But when that call gets executed from the Super's constructor, the variable s1 is not visible.

The result is s1 0

Hope this helps!

Sincerely,
your friends at JavaAdvice.com
http://www.JavaAdvice.com - The one stop resource for all your Java questions and answers

[ January 23, 2008: Message edited by: Lukasz Bajzel ]


Lukasz Bajzel,thank you so much!!!
 
qd wangqun
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

There is a constructor for Sub. Because no constructors are explicitly defined for Sub, the compiler inserts a default no-args constructor, which simply calls the no-args version of the superclass constructor.


and thank you too!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!