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

Home > Java Programming > Inheritance > Questions and Answers

Q: public class A {
public void test1(){
System.out.println("test1");
}
}
public class B extends A{
public void test2(){
System.out.println("test2");
}
}
1. public class Test{
2. public static void main (String[] args){
3. A a = new A();
4. A b = new B();
5. B b1 = new B();
6. // insert code here
7. }
8. }

Which of the following , inserted at line 6, will compile and print
test2?
A. ((B)b).test2(); B. (B)b.test2();
C. b.test2(); D. a.test2();

Answer and Explanation

Answer:((B)b).test2();

Explanation:
((B)b).test2(); is proper cast. test2() method is in class B so need to cast b then only
test2() is accessible. (B)b.test2(); is not proper cast without the second set of parentheses,the compiler thinks it is an incomplete statement.

Comment on this Question

Name:

Loading...




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