• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Explain ...

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x=20
What Is Variable Hiding and Shadowing?
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In simple, instance method looks for object not for reference type.
 
reply
    Bookmark Topic Watch Topic
  • New Topic