• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

overriding a method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Tester {
public static void main(String[] args) {
System.out.println(new Sub().g());
}
private int f() {
return 2;
}
int g() {
return f();
}
}
class Sub extends Tester {
public int f() {
return 1;
}
}
I don't understand why the above code compiled and display 2. I thought it would be 1. anybody know why? thanks.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dear
In the main method you have created the object of the Sub & on this object you called the method g(),
Now since g() is not there in Sub class it will search for higher class from which this Sub class is extended in this case it is Tester.
In g() methods it again calls the f(),now the f() method is there in both the classes but this class(Tester-top class) only knows the method in there class & not from the derived class.
so it is calling the method from the top class there by printing 2.
love
Lokesh
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I change the access modify for method f() in Tester class to anything other than private (i.e. default, protected, public), it prints out 1 as I expected.
I don't remember seeing a rule says that you can't override private methods, but it does make sense. The method f() in the parent class is private, so the sub class inherites it, but can not access it, therefore can't overrides it.
Just found in Mughal's book in answer to exercise question 6.25 (which is very similar to your question), it did say private method cannot be overriden or overloaded.

[This message has been edited by huiying li (edited February 23, 2001).]
 
xwe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the replys.
So in the code given by you, System.out.println(new Sub().g()) will print out 1, the same as System.out.println(s.g()) and System.out.println(s.f()) because f() is friendly this time. Am I right?
If f() is private, it will not be overriden and dynamic binding will not take place. In this case, g() will only see the f() defined in its own class regardless of the calling object type. Is my understanding correct?
 
huiying li
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So in the code given by you, System.out.println(new Sub().g()) will print out 1, the same as System.out.println(s.g()) and System.out.println(s.f()) because f() is friendly this time. Am I right?
Yes.
If f() is private, it will not be overriden and dynamic binding will not take place. In this case, g() will only see the f() defined in its own class regardless of the calling object type. Is my understanding correct?
Yes.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i still don't unerstand.. it is fine that private methods can't be overridden.. but what prevents the subclass from searching in its own class for the method implementation instead of the super class implementation.. since it is not being referred by a superclass anyway.. pls explain..
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi.. what do you think will the output of this one.

It prints sss.f() : 2
but I think 1 should be printed.
Could anyone help me out.
thankyou,
subbu
[This message has been edited by Subramaniam Venkatesan (edited February 27, 2001).]
[This message has been edited by Subramaniam Venkatesan (edited February 27, 2001).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for raining the parade...
Your name "xwe" does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Thanks!
Ajith
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ajith,
I've changed my username, sorry about that...
Thank you.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

experts
we need your guidance here.things are becoming more & more confusing
help us out please.
thanx
shilpa
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You must understand in fact the private method can't be overriden .But they can be shadowed.That means the code in subclass can't see the private method in parent class.And the method in parent class will not expect to dynamic bind the methods which are declared as private method.
Your sample :
When compiling , call f() in g() , will just call its own g().Because the parent class know it will not need dynamic bind function since it's a private method which can't be overriden.
(sorry, my english isn't good enough to explain more clear)
Sun Certification Java 2 Programmer
petfishlf
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have been following this topic about overriding/shadowing private methods.
Is that what happens to static methods too?
So if I have got:
class A{
static int f(){return 1 ;}
}
class B extends A{
static void f(){return 2 ;}
}
this code will compile, but I am not really overriding the method f(), just shadowing it. Am I correct?
Thanks
 
Mafalda Alabort
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry,
in my last posting it should read
class B extends A {
static int f(){return 2;}
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case you created an instance of Sub and called a method of the super class. There are two methods here called f(). There is the f()(Tester) and f()(Sub). Because f()(Tester) is private it can not be overridden. The system knows this and therefore treats f()(Sub) as a separate and unrelated method. To the JVM these two methods have different internal names. When you invoked g() and it called f(), the JVM knew that f() in Tester could not be overridden so it did not go looking in the sub classes for identical methods (the dynamic binding stuff) it just used the method that was right there.
Static methods are a bit different from private methods. Since the static methods are class level methods, they are resolved at compile time and available at class load time (even with no instance of the class created) and do not participate in polymorphism. In order to have overriding and hidden methods etc. the method needs to be resolved at runtime.
PS: there is a little pad and pencil icon on each post that can be used to edit the post it if is your own. Good thing cuz I fat finger all the time.
 
The moustache of a titan! The ad of a flea:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic