This week's book giveaway is in the Agile/Processes forum.
We're giving away four copies of Building Green Software: A Sustainable Approach to Software Development and Operations and have Anne Currie, Sarah Hsu , Sara Bergman on-line!
See this thread for details.
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inheritence

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q10
What is the output of the following program ?
class Question
{
String s="Outer";

public static void main(String args[])
{
S2 s2 = new S2();
s2.display();
}
}
class S1
{
String s="s1";
void display()
{
System.out.println(s);
}
}
class S2 extends S1
{
String s="s2";
}
(a)S1
(b)S2
(c)null
(d)S1S2
The answer mentioned is (a),but should'nt it be (b) because we are using an object of class S2 to invoke a method which it has inherited.Pls give ur opinion.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Savio, that's because the display method defined in the superclass only knows about the S variable defined in it's class. You have to override the display method in the subclass, if you want it to print s2.
Bosun
 
reply
    Bookmark Topic Watch Topic
  • New Topic