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 tech() { System.out.println("anonymous tech"); } }; public void dothis() { a.tech(); } public static void main(String... args){ Atech atech = new Atech(); atech.dothis(); }

  1. anonymous tech

  2. Compile Error

  3. Tech

  4. anonymous tech Tech


Correct Option: A

AI Explanation

To determine the output of the code, let's go through the code step by step:

First, we have the class Tech with a method tech() that prints "Tech".

Next, we have the class Atech which creates an object a of type Tech using an anonymous inner class. This anonymous inner class overrides the tech() method and prints "anonymous tech".

In the dothis() method of class Atech, the tech() method of the object a is called.

Finally, in the main method, an object atech of type Atech is created, and the dothis() method is called on it.

Therefore, the output of the code will be:

"anonymous tech"

The correct answer is A.

Find more quizzes: