CodeGym /Java Course /Java Multithreading /Interview prep | Level 3

Interview prep | Level 3

Java Multithreading
Level 3 , Lesson 12
Available

"Hi, Amigo!"

  Interview questions
1 What types of nested classes are there?
2 What does an anonymous class become when it is compiled?
3 Why use the keyword final when creating anonymous classes?
4 What is the right way to create an instance of a nested inner class?
5 What is the right way to create an instance of a static nested class?
6 Can you declare static methods/variables in a nested inner class?
7 Name any three of static nested classes.
8 How do nested classes solve the problem of multiple inheritance in Java?
9 What is the difference between anonymous classes based on an interface and those based on a class?
10 Can you create an anonymous static nested class?

 

Comments (12)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Kubańczyk92 Level 35, Wroclaw, Poland
18 September 2022
1. What types of nested classes are there? There are four kinds of nested class in Java. In brief, they are: • static class: declared as a static member of another class • inner class: declared as an instance member of another class • local inner class: declared inside an instance method of another class • anonymous inner class: like a local inner class, but written as an expression which returns a one-off object 2. What does an anonymous class become when it is compiled? Byte codes for anonymous classes are created at compile time. Anonymous classes are named after their enclosing class, postponed with the $ sign and an increasing number - Foo$1.class 3. Why use the keyword final when creating anonymous classes? The reason why the access has been restricted only to the local final variables is that if all the local variables would be made accessible then they would first required to be copied to a separate section where inner classes can have access to them and maintaining multiple copies of mutable local variables may lead to inconsistent data. Whereas final variables are immutable and hence any number of copies to them will not have any impact on the consistency of data. 4. What is the right way to create an instance of a nested inner class? Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes: class OuterClass { ... class InnerClass { ... } } An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass() OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Kubańczyk92 Level 35, Wroclaw, Poland
18 September 2022
5. What is the right way to create an instance of a static nested class? Static nested classes are accessed using the enclosing class name: OuterClass.StaticNestedClass For example, to create an object for the static nested class, use this syntax: OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); 6. Can you declare static methods/variables in a nested inner class? If an inner class has at least one static component then the class itself must also be static. This does not apply to fields marked as static final. 7. Name any three of static nested classes. Node class nested in LinkedList class, Entry class nested in HashMap class, Entry class in LinkedList class 8. How do nested classes solve the problem of multiple inheritance in Java? Inner classes exist since Java doesn't support Multiple Inheritance. This (multiple inheritances) can be done within an Inner class which it is that the Outer class can have multiple inner classes, and each of them can inherit from different classes. So in this way, The multiple inheritances can be implemented.
Kubańczyk92 Level 35, Wroclaw, Poland
18 September 2022
9. What is the difference between anonymous classes based on an interface and those based on a class? For interfaces, the anonymous class has to implement all methods. For classes, you don't have to, but you can override methods. If you're not overriding any methods you might as well just create a regular instance. It's exactly like implementing an interface in the first case and extend a class in the second. 10. Can you create an anonymous static nested class? As soon as the anonymous class is created, the compiler creates inner classes for all anonymous classes. If we write something like: static Thread thread = new Thread() { public void run() { tigerRun(); } }; then without anonymous class it would look like: static TigerThread thread = new TigerThread(); private class TigerThread extends Thread { public void run() { tigerRun(); } } so it means that only the variable would be static, not a class
TomL Level 30, Prague, Czech Republic
12 June 2021
Anyone knows answer for the question no.7?
Andrei Level 41
7 April 2021
Some of these questions don't make sense, or at least I can't understand them. For example: 3. Why use the keyword final when creating anonymous classes? - nowhere in this article https://codegym.cc/groups/posts/261-anonymous-classes does it talk about this. And nowhere on the internet have I found mentions of using the final keyword when creating anonymous classes ? 5. What is the right way to create an instance of a nested class? - nested classes can be either static or non-static no? Which type of a nested class are they referring to here? Isn't this question the same as number 4?
Romain Level 26, Paris, France
22 April 2021
for the 3th question I have no answer, for the 5th, you miss a word : What is the right way to create an instance of a static nested class? so I'm sur you have the answer, just in case :

NameOutterClass.NameNestedClass myClass = new NameOutterClass.NameNestedClass();
Have you the answer for the 7th question ? To me there is an error, it should be : "Name any three of static nested classes." and the and the answer : the inner class, method-local inner class, anonymous inner class Thx
Andrei Level 41
26 April 2021
Hi Rom, I got in touch with CodeGym people after posting these questions to tell me the difference between the 2 and they updated question 5, adding the static keyword.
Andrei Level 41
26 April 2021
For 7, I don't have an answer, you might be right.
Michał Level 41, Krakow, Poland
20 July 2021
For question 7: how about classes like Map.Entry? It's a nested class of Map class and it's static.
Banak Level 29, Saint-Gratien, France
30 March 2021
10-A static nested class can only access the static fields of an outer class and can be independently created without creating the outterClass object first.(an anonymous class cannot contain static variables and methods because it inheret from it's ancestor (inner class)!) so false
Banak Level 29, Saint-Gratien, France
30 March 2021
I think this is only possible if the anonymous class only accesses the static members of the class it is nested in!
Romain Level 26, Paris, France
28 April 2021
Correct answer : (https://codegym.cc/fr/quests/lectures/questmultithreading.level03.lecture07) And I think you have wrong when you say "(an anonymous class cannot contain static variables and methods because it inheret from it's ancestor (inner class)!) " For me, the anonymous class doesn't inherit from the outer calss, but she still can't have statics varaibles and methods cause she is an inner class. By the way if you have an answer for the third question I take it, thx