Multiple choice technology programming languages

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

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The code creates an anonymous inner class that extends Tech and overrides the tech() method. When a.tech() is called in dothis(), it invokes the overridden version which prints "anonymous tech". Option B is wrong - anonymous inner classes extending a class are valid syntax. Option C would only occur if the original Tech.tech() was called, but it's overridden. Option D is incorrect - only one method call happens, printing one line.

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.