File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private method and inheritance... Exam Lab...

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's from Exam Lab. How is possible?


The Output : High

How is it possible? Since the method defined in base class High is private, it's not inherited to sub class Low. No Virtual Method Invocation... Thanks in Advanced!
 
Ankit Garg
Sheriff
Posts: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the reference is of type High, high has a private drop method. That method will be called. private methods are not inherited and overridden thus the private drop method of Low class doesn't come into picture...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still I couldn't get it... High class method won't be inherited down to Low class. Low class method won't be accessible from other class, since it's private. When it compiles, no problem, because it check with reference type, not with the actual type. So...?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:Still I couldn't get it... High class method won't be inherited down to Low class. Low class method won't be accessible from other class, since it's private. When it compiles, no problem, because it check with reference type, not with the actual type. So...?



the best explaination of thebabove query is that.

in case of static,private and overloaded methods..............reference type determines which method to call.................

and in case of overridden methods........object type determines which method to call...............
in above question the method is private so the refernce type of the object determines which method to call...................
so it call high class method.................
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are there anyone?
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"private" means private to the class, and not specific to an instance. The static main() method of the High class is allowed to access private methods of the High class.

And as already mentioned by phil, overridding doesn't apply to private methods, so the reference type is used to determine access to the particular method.

Henry
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. At Compile time, it checks with the reference type, so it's OK, but in the run time, the method will be invoked on the actual object basics, and since the actual object type method can't be accessed from other class. That's why I asked.

Now, as according to Henry, it(JVM) will invoked the reference type method, because it can't access the actual object type.

Thanks...
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:Thanks Henry. At Compile time, it checks with the reference type, so it's OK, but in the run time, the method will be invoked on the actual object basics, and since the actual object type method can't be accessed from other class. That's why I asked.

Now, as according to Henry, it(JVM) will invoked the reference type method, because it can't access the actual object type.

Thanks...




i donot know whether i am right or wrong.............but when it comes to reference type i think compiler decide which method to invoke..............and jvm invokes that method............

Henry,Please correct me if i am wrong
 
Pradeep Kr
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:

The Output : High


I think logically what Abimaran saying is correct. But ..

Lets assume, above code is not correct. There may be two error
1. Compile time Error
2. Runtime Exception (something like method not found)

Lets think about each for them one by one -
1. Compile time error, I don't think it make sense because it is perfectly valid to call private method of a class from same class's static method.
2. Runtime Exception, how would you explain this behavior, compiler said it is ok code, then suddenly at run time code starting to throw method not found exception.

So my guess is since there is not perfect solution for this problem, best approach would be what is current behavior of Java. I think that's why java designer went for this approach.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep- Kumar wrote:
2. Runtime Exception, how would you explain this behavior, compiler said it is ok code, then suddenly at run time code starting to throw method not found exception.

So my guess is since there is not perfect solution for this problem, best approach would be what is current behavior of Java. I think that's why java designer went for this approach.


Yea. It's interesting! Let's see, what others say!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic