| Author |
why run super class methods for twice ?
|
ben oliver
Ranch Hand
Joined: Mar 28, 2006
Posts: 369
|
|
public class A { protected act() { System.out.println("hello"); } public class B extends A { } public class C extends B { } public class Test { public static void main(String[] args) { C c = new C(); c.act(); } } I saw "hello" being printed for twice. Why ?? I thought the process is C calls B , and B calls A and prints "hello" for once. Why twice ??
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
Can you post the exact and complete program? The code you posted above does not compile (the method definition of act() in class A is missing a return type and there is a missing closing curly brace). It should not print "hello" twice, so something must be going wrong. Please use code tags when you post code.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
ben oliver
Ranch Hand
Joined: Mar 28, 2006
Posts: 369
|
|
|
sorry for the confusion. I found out the problem. It only prints once. thanks.
|
 |
 |
|
|
subject: why run super class methods for twice ?
|
|
|