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

Home > Java Programming > Inner Classes > Questions and Answers

Q: What is the output for the below code ?
public class Tech {
public void tech() {
System.out.println("Tech");
}
}
public class Atech {
Tech a = new Tech() {
public void nontech () {
System.out.println("anonymous nontech tech");
}
public void tech() {
System.out.println("anonymous tech");
}
};
public void dothis() {
a.tech();
a.nontech();
}
public static void main(String... args){
Atech atech = new Atech();
atech.dothis();
}
}
A. anonymous nontech tech anonymous tech B. anonymous tech
C. Compilation fails D. Compilation succeed but Runtime Exception

Answer and Explanation

Answer:Compilation fails

Explanation:
anonymous subclass of the specified class type. Compile fail because Tech class does not have nontech() method. compiler will complain if you try to invoke any method on an anonymous inner class reference that is not in the superclass class definition.

Comment on this Question

Name:

Loading...




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