• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Methods overridden/hidden/inherited

 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Override the method local in the child class and check....
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
rajan singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so 'everytime' we call an inherited method, an implicit this reference of the 'subclass' (the caller) is passed?
 
rajan singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whenever we call non static method implicit this will be passed and this
will point to actual object created.
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes in my view you are right.
 
reply
    Bookmark Topic Watch Topic
  • New Topic