Welcome Guest | Sign in | Register
Inheritance - Discussion Page For Q.2821 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Inheritance > Questions and Answers

Q: What is the output for the below code ?

public class A {
int i = 10;
public void printValue() {
System.out.println("Value-A");
};
}
public class B extends A{
int i = 12;
public void printValue() {
System.out.print("Value-B");
}
}
public class Test{
public static void main(String argv[]){
A a = new B();
a.printValue();
System.out.println(a.i);
}
}
A. Value-B 11 B. Value-B 10
C. Value-A 10 D. Value-A 11

Answer and Explanation

Answer:Value-B 10

Explanation:
If you create object of subclass with reference of super class like ( A a = new B();) then subclass method and super class variable will be executed.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.