aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private method and inheritance... Exam Lab... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private method and inheritance... Exam Lab..." Watch "private method and inheritance... Exam Lab..." New topic
Author

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

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Mar 17, 2010
Posts: 1051

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.................


SCJP6.0,My blog Ranchers from Delhi
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

are there anyone?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


"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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Mar 17, 2010
Posts: 1051

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

Joined: Feb 17, 2010
Posts: 22
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

Joined: Nov 04, 2009
Posts: 2066

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private method and inheritance... Exam Lab...