This compiles and fine giving o/p: Executing Base Callgo Executing Base Here the method go ()is private and final,Then howc ome in DerivedBase class it is overidden to be public and nonfinal??Its not overidiing and also not overloading.. Please help!!
The method go() is private in Base and it cannot be inherited by the DerivedBase class, but the method callgo() is inherited! and it can access the methods of class Base!!. Hence the result.
Its neither overriding or overloading. Its a new method by itself in the sub-class
Kayal
Ramu MS
Greenhorn
Joined: Jun 02, 2005
Posts: 4
posted
0
Hi Smitha,
Just wanted to add few more words!. The method go() cannot be overridden because, none of the private methods can be inherited(and overriding happens only if any method is inherited).
The method go() is not overloaded, because overloading happens in the same class and there is no other method declaration in DerivedBase.
So the declaration is a new declaration in DerivedBase.
System.out.println(this) in callgo() and go() and it prints DerivedBase@..
Now how come go in Base call gets called
Ramu MS
Greenhorn
Joined: Jun 02, 2005
Posts: 4
posted
0
Hi Srikanth,
Since an object of DerivedBase is calling the function callgo(), the current object reference available to the function is that of DerivedBase object.
Let me explain with an example...
class Base{ public void callgo(){ this.hi(); } public void hi(){ System.out.println("HI in Base"); } } public class DerivedBase extends Base{ public void hi(){ System.out.println("HI in Derived"); } public static void main(String[]args){ new DerivedBase().callgo();//1 new Base().callgo();//2 } }
OUTPUT ~~~~~~
HI in Derived HI in Base
In the above example
When //1 is executed this reference is pointing to an anonymous DerivedBase object hence when this.hi() is executed, it calls hi() of DerivedBase
When //2 is executed this reference is pointing to an anonymous Base object hence when this.hi() is executed, it calls hi() of Base
hi Ramu, iam still confused.when i execute this code class Base{ public void callgo(){ System.out.println("Executing Base Callgo"); System.out.println(this); go(); } private void go(){ System.out.println("Executing Base"); }} class DerivedBase extends Base{ public void go(){ System.out.println("Executing Derived"); } public static void main(String[]args){ new DerivedBase().callgo(); } } it prints Executing Base Callgo DerivedBase@3 Executing Base
whereas class Base{ public void callgo(){ System.out.println("Executing Base Callgo"); System.out.println(this); go(); } public void go(){ System.out.println("Executing Base"); }} class DerivedBase extends Base{ public void go(){ System.out.println("Executing Derived"); } public static void main(String[]args){ new DerivedBase().callgo(); } } then it prints Executing Base Callgo DerivedBase@3 Executing Derived
my doubt is in the call go method we have the this reference to Derived object only then how come its printing base class go method when i declare it private and when i change it to public its fine ..can nyone tell me please.
However my doubt is to how it resolves which go() to call. How does making go private change the resolving process.?
And I just wanted to add my thoughts on this question..
Why would one want to have a private method as final ?
Does it have any effect.
Even in case of an Inner class which has access to private methods of the enclosing class - The Inner class can still extend from the Enclosing class and provide a declaration for the method go() without any error, eventhough its private final in the enclosing class. Since private final methods are never inherited. I think this cant be called as overriding. I think the Inner class is actually "shadowing" the go() of the outer. The Inner class now has a separate method go. (Actually though its getting very confusing !!! )
class Outer { private final void go(){}
class Inner extends Outer{ void go(){ super.go();// No Error !! (not supposed to be inherited right) Outer.this.go(); } } } // This code compiled !! [ August 26, 2005: Message edited by: sankar velamuri ]
SCJP 1.4
Ramu MS
Greenhorn
Joined: Jun 02, 2005
Posts: 4
posted
0
Hi Srikanth,
Please note this point...
The compiler always tries to resolve the function calls at compile time, if not possible then it leaves it for the JVM to decide which function to call.
Let me explain ,
What happens when the method go() is private. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In this case, go() cannot be inherited by any of the derived classes extending Base, hence the resolution happens at compile time. Whenevr the method call is encountered it calls the already resolved fucntion i.e go() in Base class.
What happens when the method go() is public. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In this case since the method go() is inherited and overridden by DerivedBase class. The method resolution is not possible during compile time. Hence the call happens depending on the type of object that is calling the method callgo().