package testsubclasssinheritance; public class One { private void myMethod(){ System.out.println ("This is my private from method One"); } }
// TWO.java package testsubclasssinheritance; public class Two extends One {
public void myMethod(){ System.out.println ("This is my private from method Two"); }
public static void main (String[] args){ Two t = new Two(); t.myMethod(); } }
// RESULT The result was the code compiled an printed out "This is my private from method Two"
// QUESTION Does that mean that the private method can be OVERRIDDEN ???
Anton Uwe
Ranch Hand
Joined: Jan 10, 2007
Posts: 122
posted
0
No. Class Two knows nothing about the private method of class One. And so it is no problem to define a new method with the same name (and return type). No overloading or overriding takes place, it's just a new method (unluckily) having the same name.
Pratap koritala
Ranch Hand
Joined: Sep 27, 2006
Posts: 251
posted
0
yes,A method can be Overridden with less Restrictive Access
As public,protected and default are less Restrictive than Private ,they can override the private method(of course,Private methods won't be inherited) [ January 13, 2007: Message edited by: ramya sri ]
Anton Uwe
Ranch Hand
Joined: Jan 10, 2007
Posts: 122
posted
0
Mhhh, let's take a look at this code:
If it is overriding, than we would get a compile error, because final methods can't be overridden. But there is no error at all.
Or look at this code:
If it would be overriding, we would have a compile error (overriding methots cannot throw "more" checked exceptions than the base method declared. But here is no compile error,too. Or look at this code:
If it would be overriding, we would get a compile error.
So, it's not overriding, we just have a new method which happen to have the same name.
Pratap koritala
Ranch Hand
Joined: Sep 27, 2006
Posts: 251
posted
0
Can we able to invoke Subclass method with Super class Reference on the Runtime Object of Subclass Super s=new Sub(); S.InvokeSubclassMethod();with the signature of Private method
Dustin Johnson
Greenhorn
Joined: Nov 14, 2006
Posts: 22
posted
0
Although you reference the variable as a Super class, you are initializing it as a subClass... therefore you should only be able to use the private methods of the subClass. If that was what you were asking...
As per the concept the private member of the class is visible to itself and not any other class or sub-class.
Hence the sub-class has no idea about the private method you defined in the SuperClass.
Since it doesn't know about the existence of same method in the Super Class, it can define the same method which can be treated as a normal method for the sub class.
So in your case waht the JVM sees is " a normal method of your sub-class" and executes successfully.
Is this information fine or if i miss anything please inform. [ January 13, 2007: Message edited by: siva prasaad ]
siva prasaad
Greenhorn
Joined: Jan 10, 2007
Posts: 23
posted
0
Missed out in my prevoius post,
Importantly , We should be aware that, Here we are not at all using the Overrding Concept.
The above 2 posts are for the 1st question....please make a note [ January 13, 2007: Message edited by: siva prasaad ]
Andrew Affolter
Greenhorn
Joined: Jan 07, 2007
Posts: 5
posted
0
I like to think about this in the sense that the sub class has no idea about the private member of the super class and can't overload something it doesn't know about. The subclass could create its own method that was the same but its still not really overloading it.
Mehul Mehta
Greenhorn
Joined: Aug 22, 2006
Posts: 7
posted
0
Thanks Anton Uwe for the clarification and some good clean examples.
Pratap koritala
Ranch Hand
Joined: Sep 27, 2006
Posts: 251
posted
0
yes Anton Uwe,as you said,this code giving compiler error instead of printing 4,4 .
[ January 14, 2007: Message edited by: ramya sri ]
Anton Uwe
Ranch Hand
Joined: Jan 10, 2007
Posts: 122
posted
0
You want to call a private method of the superclass. This method is not visible in the subclass. So you will get a compile error. You don't "need" to have in the subclass: You will get exactly the same error without it.