• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Inheritance doubt

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends, in this program given below , the print() method is inherited by the subclass. Then why does it print the value of variable s of the class Base ?
Thanx a lot
Vidya
class Base{
String s = "Base";
void print(){
System.out.println(s);
}
}
class Derived extends Base{
String s = "Derived";
}
public class Test {
public static void main(String[] args){
Derived b = new Derived();
b.print();
System.out.println(b.s);
}
}
[ April 29, 2003: Message edited by: Vidya Venkatesh ]
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Derived class inherits the print() method of base class. It does not define its own implementation of print() overriding the base class method.
So when u invoke the print() method, the base class method is called and the string in base class is printed.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At compile time, class Base knows nothing about class Derived. What the compiler does is to statically bind print method to String s in Base. So, when the inherited print method is called, it's the value of String s at compile time ("Base") which is printed out. String s in Derived is not used at runtime, it is only there to test you.
[ April 29, 2003: Message edited by: Roger Chung-Wee ]
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vidya
Let me do the trace help you to go through this programme

Firstly,print out (7)
Base
Then,print out (8) because it use the string itself
Derived
If it is not clear,please post it again
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method in a subclass may override a method in a superclass that has the same name and parameter list. However, fields in a subclass do not override fields in a superclass. Instead, a field in a subclass will "hide" a field with the same name in a superclass. The behavior of overridden methods is different from the behavior of hidden fields.
Section 8.3.3.2 of the Java Language Specification covers "Hiding of Instance Variables".
 
reply
    Bookmark Topic Watch Topic
  • New Topic