Welcome Guest | Sign in | Register
Inheritance - Discussion Page For Q.2833 | 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 C {
}
public class D extends C{
}
public class A {
public C getOBJ(){
System.out.println("class A - return C");
return new C();
}
}
public class B extends A{
public D getOBJ(){
System.out.println("class B - return D");
return new D();
}
}
public class Test {
public static void main(String... args) {
A a = new B();
a.getOBJ();
}
}
A. class A - return C B. class B - return D

C. Compilation fails D. Compilation succeed but no output

Answer and Explanation

Answer:class B - return D

Explanation:
From J2SE 5.0 onwards. return type in the overriding method can be same or subtype of the declared return type of the overridden (superclass) method.

Comment on this Question

Name:

Loading...




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