• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

mock exam problem

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, everyone:
I am taking a mock exam ,i got a problem with one of the mock exam questions.
Let's see the following code:
class Base {
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}
class Sub extends Base{
public static void main(String a[ ]){
Sub s = new Sub();
int iBase =0;
s.amethod(iBase);
}
public void amethod (int iSub){
System.out.println("Sub.amethod");
}
}
the answer is :Output of "Sub.amethod"
I disagree with the answer. My opinion is : the amethod in the superclass is a private method, it can't be seen in the subclass. maybe this code works fine. but the amethod in the subclass isn't a version of an overriding method.
I will try to run the code tonight. I just want to know if we can override a private method from superclass or not?
Hope someone can help me figure out that.
thanks a lot.
honggui
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Honggui, we cannot override method with private access modifier from super class. If the derived class happens to have a method with the same singiture with that private method, it is just a normal method in the derived, has nothing to do with overriding.
In fact, the code you provide should either be

or

The following will not compile:

since the amethod binding at compile time will find that amethod in Base has private access and fail it.
Hope it helps.
Guoqiao

Originally posted by Honggui Li:


 
Honggui Li
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,guoqiao:
Thank you for your kindly help. I learned a lot from your reply.
Acturally, I am confused with your last code. let's say
Base b = new Sub();
b.amethod(100);
In this case, in my opinion, it will use the amethod() in the Sub class, since the actural object type of b is Sub, the reference type of b is Base, so, i think java will decide which method will be used based upon the object type.
I haven't tried that, let's compile and see what will happen.
By the way, when i do the mock exam for programmer test, i always can't figure out the compile time error and runtime error. I know there is something wrong with the code, but sometimes it is very difficult to choose the compile time error or runtime error.
If u have any experience in this field, hope u can give me some hints.
Anyone who knows where i can find some useful resources about this topic can feel free to reply to me.
Thanks a lot.
Honggui
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic