wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Methods overridden/hidden/inherited Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Methods overridden/hidden/inherited" Watch "Methods overridden/hidden/inherited" New topic
Author

Methods overridden/hidden/inherited

Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

If i am not wrong,The method which is overridden is not said to be inherited. Please check the code, here static init() is hidden

As we know static methods are resolved during compile time and a static method call is done according to type of reference, Here type of override is OverrideStatic, so on calling override.init(). the hidden static init() method of OverrideStatic class is called.This is clear to me.
Now when we call override.local();, the call is resolved by dynamic binding and the local() of class A is called, which will futher contain call to init() and hi(), Now i know that hi() of Override class is called, but why init() of Override is not called, as Type of original override reference is OverrideStatic and init() is also hidden in class Overriden.

In summary i need clerification on call of init() in side local() method of class A. Which init() will be call and Why ???

( tags added so that mere mortals can read it)
[ June 16, 2005: Message edited by: Barry Gaunt ]
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
Override the method local in the child class and check....


Thnx
rajan singh
Greenhorn

Joined: Jun 08, 2005
Posts: 17
hii pawan,

when local() is called, this(reference) is available to local().
and with the help of this(reference) only non static function will be called(remember this(reference) is pointing to object of OverrideStatic class)... since init() is static and superclass doesnot know about subclass so A's init willbe called.

if we provide overriiden local in OverrideStatic class then OverrideStatic's init() will be called..

if i am wrong plz correct me...
[ June 14, 2005: Message edited by: rajan singh ]
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

hi ppl,

i understand why the local() method calls init() in class A, coz the class will call its own static members, but why does it call hi() in the subclass when it is currently in the superclass?


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
rajan singh
Greenhorn

Joined: Jun 08, 2005
Posts: 17
hii marzo,

we have OverrideStatic override = new OverrideStatic();

and we call override.local();

local() having this reference which is pointing to instance of OverrideStatic. and this.hii()=> (instance of OverrideStatic).hii()

remember when we call superclass method through subclass object..this(reference) always point to subclass's object.
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

so 'everytime' we call an inherited method, an implicit this reference of the 'subclass' (the caller) is passed?
rajan singh
Greenhorn

Joined: Jun 08, 2005
Posts: 17
whenever we call non static method implicit this will be passed and this
will point to actual object created.
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Hi Rajan,
This is available in local(), so hi() will be called from subclass. As we know hi() is overridden method. Thanks for this.
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Please check the link and tell me why overridden method of Child is not being called (is it because the Parent class method method1() is private())


<a href="http://certification.about.com/library/quiz/java/blscjp14_q28.htm">
http://certification.about.com/library/quiz/java/blscjp14_q28.htm</a>
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Or please check this code and what ouput you expect

class Parent

{

private void method1()

{

System.out.println("Parent's method1()");

}


public void method2()

{

System.out.println("Parent's method2()");

method1();

}


}


class Child extends Parent

{

public void method1()

{

System.out.println("Child's method1()");

}


public static void main(String args[])

{

Parent p = new Child();

p.method2();

}

}
rajan singh
Greenhorn

Joined: Jun 08, 2005
Posts: 17
hii pawan,

you are right...using reference of base type we can invoke the method of subclass if it is overridden in sub class otherwise not.
and since method1() is private we are not overriding it....even if invoke method1() using base class reference(though it is pointing to base class) it will not complile because the using base class reference(pointing to subclass object) only overriden method is called.

PLEASE LET ME KNOW IF I AM WRONG......
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Yes in my view you are right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Methods overridden/hidden/inherited
 
Similar Threads
Field access (local/static)
Overriding both versions of init() method
Hiding Members (static methods /fields)
difference between overriding & hiding
can static method overridden?