Welcome Guest | Sign in | Register
Inheritance - Discussion Page For Q.2818 | 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 ?
package com;
class Animal {
public void printName(){
System.out.println("Animal");
}
}
package exam;
import com.Animal;
public class Cat extends Animal {
public void printName(){
System.out.println("Cat");
}
}
package exam;
import com.Animal;
public class Test {
public static void main(String[] args){
Animal a = new Cat();
a.printName();
}
}
A. Animal B. Cat
C. Animal Cat D. Compile Error

Answer and Explanation

Answer:Compile Error

Explanation:
Cat class won't compile because its superclass, Animal, has default access and is in a different package. Only public superclass can be accessible for different package.



Comment on this Question

Name:

Loading...




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