• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

method,variable accessing

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello to all
Any one can explain both different program result.What is the main reason for that?

class Base extends Object

{
public Boolean isOK()
{

return new Boolean(true);
}
}



class Child extends Base

{
public Boolean isOK()
{


return new Boolean(false);


}
}

public class Qa1

{

public static void main( String args[] )

{

Child palm = new Child();

Basevap = palm;

System.out.println( vap.isOK() );

}

}

The answer is "false"
--------------------------------------------------------------------
class Base extends Object

{
public boolean ok = true;

}



class Child extends Base

{
public boolean ok = false
}

public class Qa1

{

public static void main( String args[] )

{

Child palm = new Child();

Base = palm;

System.out.println( vap.ok );

}

}

The answer is "true"
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a very simple explanation - In the case of storing a child class object in a base class reference, overridden method definition from child class shall be called as JVM deciphers in runtime that it is actually a child class object wrapped in a parent class reference. Whereas variables are something which are referred as per their compile time binding. That's the reason you see this behavior. Hope that helps you Rex (Amit)..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post code, so that the forum can display your code properly.

To answer your question: Member variables are not polymorphic - Java does not lookup the variable according to the runtime type of the object, it only looks at the type of the variable (which is Base in this case). Member variables cannot be overridden.
[ August 30, 2007: Message edited by: Jesper Young ]
 
I'm doing laundry! Look how clean this tiny ad is:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic