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

method overriding

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i can't understand why the out put of b.i is 99 in the below mention question please help me.
Amit
class Base {
int i=99;
public void amethod(){
System.out.println("Base.amethod()");
}
}
public class RType extends Base{
int i=-1;
public static void main(String argv[]){
Base b = new RType();//<= Note the type
System.out.println(b.i);
b.amethod();
}
public void amethod(){
System.out.println("RType.amethod()");
}
}
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Base {
int i=99;
public void amethod(){
System.out.println("Base.amethod()");
}
}
public class RType extends Base{
int i=-1;
public static void main(String argv[]){
Base b = new RType();//<= Note the type
System.out.println(b.i);
b.amethod();
}
public void amethod(){
System.out.println("RType.amethod()");
}
}
o/p will be
99
RType.amethod()
whenever we assign child reference to base reference..
1.if we try to access the variables which are shadowed
we will always get the value of the variable from the base
class and not the child class since variables are identified
by there type of reference and not to which object they are
pointing.
2.if we try to access
a)Overrriden methods child method will be called
b)if method is not overriden then base method will get a call.
hope this could solve ur problem

 
Amit Madan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for solving my problem.
Amit
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rember this...
Member methods are accessed based on the refering object class type at run time. i.e subclass overriden methods are accessed. And member vaiables are accessed based on the class type,i.e base class member varialbes.
So in this case, Base b = new RType(), at runtime b refers to the subclass RType instance and methods are accessed on this and varialbes are accessed on Base class itself.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for this discussion. I did not know that the variables are taken from the referencing class. I did know that a method from the object type is used if it exists but I assumed that this is also the case for initialized values.
 
reply
    Bookmark Topic Watch Topic
  • New Topic