• Post Reply Bookmark Topic Watch Topic
  • New Topic

private methods and inheritance  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. Here is the code:



Output: Hello!!!

For my opinion there must be runtime error because runtime JVM cannot find method abc() in class B (method is not visible in B: B class says: "I cannot see any inherited method abc() in my scope"), but code runs successfully. Where is mistake in my discussion?

 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zaur Tregulov wrote:
For my opinion there must be runtime error because runtime JVM cannot find method abc() in class B (method is not visible in B: B class says: "I cannot see any inherited method abc() in my scope"), but code runs successfully. Where is mistake in my discussion?

The "private" modifier means private to the class. And you are running the code from the same location where the method is declared (ie. the same class).

The compiler knows that B has no such method, and knows that the private version is accessible, so why isn't it allowed? Trying moving the main() method to a different class instead.

[EDIT: To elaborate some more. You are using an A reference to call the method. At compile time, the compiler doesn't care about the object type. It only knows that you are using an A reference, and since private methods do not support polymorphism -- in this example, the private method is called, even if the B class has a version of the method.]

Henry
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

First of all, whether a method is accessible or not is determined statically, and not dynamically. That means that this is determined at compile time and not at runtime. The compiler checks if you are allowed to call a method, when you are compiling the program, not the JVM when you run the program. So it is incorrect to expect a runtime error.

You can call the private method in line 8 because the main method is in the same class A as the private method abc.

If you move the main method to another class (for example, class B, or any other class) then you will get a compiler error when you try to compile the code.
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The private method is not inherited by subclasses, and you would not expect a runtime error. If you get an error because of access problems, the code would fail to compile.

You are working in a special situation because you are inside the same class; if you move the main method to a class of its own (where it should ideally be), that class will fail to compile.
 
Zaur Tregulov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot to all of you, guys!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!