This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explain ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explain ..." Watch "Explain ..." New topic
Author

Explain ...

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
public class Test extends Super {

int x = 20; //1

public void test() {
System.out.println("x = " + x);
}

public static void main(String[] arg) {
Super s = new Test(); //2
s.test(); //3
}
}

class Super {
int x = 10; //4

public void test() {
System.out.println("x = " + x);
}
}
A : x = 10
B : x = 20
C : The program cannot be compiled due to line //1.
D : The program cannot be compiled due to line //2.
E : The program cannot be compiled due to line //3.
F : The program cannot be compiled due to line //4.

O/P B...Why....


$Hi/\bEr
Peter MacMillan
Ranch Hand

Joined: Jun 23, 2006
Posts: 34
Even though the type of s is Super, the type of the object that it references is of type Test. This is possible because Test is a Super by virtue of inheritance. The test() method overrides the test() method of the Super class, and when you call s.test() you call that method (the one in Test) because of what the object is.

Hope that helps.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
x=20
What Is Variable Hiding and Shadowing?
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
In simple, instance method looks for object not for reference type.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain ...
 
Similar Threads
RHE P182
varargs - method resolution principle???
overriding
assigning boolean to integer.
Pls help me out..