This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes why run super class methods for twice ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "why run super class methods for twice ?" Watch "why run super class methods for twice ?" New topic
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
    
    3

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why run super class methods for twice ?
 
Similar Threads
Generics
Question on inheritance
Cast with Runnable interface.
Java Generics
Overloading & Overriding!