JAVA Programming :: Basic Concepts - Discussion
Home > JAVA Programming > Basic Concepts > MCQs Questions Discussion
38 / 64
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();
printValue();
System.out.println(i);
}
}
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();
printValue();
System.out.println(i);
}
}
AValue-B 11
BValue-B 10
CValue-A 10
DValue-A 11
Answer: Option (Login/Signup)
Show Explanation
Asked In ::
STEP-BY-STEP
In line no 15-
A a=new B();
Here, we are creating an object of class B, with reference of class A i.e, creating subclass object with superclass reference. In such a case, the subclass method gets executed, and superclass variable is executed.
Hence, output will be Value-B 10.
Moreover, there is a small but major error. printValue() method is a non-static method and should be invoked by the object of the class, somewhat like a.printValue();
Read Full Answer
Report Error
Please Login First Click Here