• Post Reply Bookmark Topic Watch Topic
  • New Topic

why run super class methods for twice ?  RSS feed

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the confusion. I found out the problem. It only prints once. thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!