wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding" Watch "Overriding" New topic
Author

Overriding

Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99


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!!
Ramu MS
Greenhorn

Joined: Jun 02, 2005
Posts: 4
Hi Smitha,

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.

Thanks
Ramu
Kayalvizhi Umashankar
Greenhorn

Joined: Aug 04, 2005
Posts: 25
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
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.

Thanks
Ramu
Shrikant Pandhare
Greenhorn

Joined: Jul 29, 2005
Posts: 15
hey i am a bit confused..
now if we write

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

Hope that makes it clear!!

Regds
Ramu
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Try to change the private final method to public final method you 'll get an error.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
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.

thanks & regards

srikanth


Thanks & Regards<br /> <br />-Srikanth
Vidya Sankar
Greenhorn

Joined: Aug 17, 2005
Posts: 20
Nice Question !!

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

Hope this solves the doubt.

Regards
Ramu
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overriding