File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritence" Watch "Inheritence" New topic


Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
What is the output of the following program ?
class Question
String s="Outer";

public static void main(String args[])
S2 s2 = new S2();
class S1
String s="s1";
void display()
class S2 extends S1
String s="s2";
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.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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.

So much trouble in the world -- Bob Marley
I agree. Here's the link:
subject: Inheritence
It's not a secret anymore!