CodeGym/Courses/Java Multithreading/Interview questions - Level 4

Interview questions - Level 4

Available

"Hi, Amigo! Here are some interview questions for you:"

Interview questions
1 What do anonymous inner classes become after being compiled?
2 Can inner classes be inherited?
3 Can anonymous inner classes be inherited?
4 Can you override inner classes?
5 What limitations do local classes have?
6 Can an anonymous inner class contain static methods?
7 Can you create an instance of an inner class if the outer class only has a private constructor?
8 Can you declare inner classes as private?
9 Can you declare anonymous inner classes as private?
10 How many inner classes can a class have?

"But, Professor, I haven't been told the answers to these questions!"

"Amigo, do you think employers prepare interview questions based on my lessons?"

"Uhhhh... No."

"This isn't college. If you need answers, google them."

"And say thanks for the questions."

"Thanks."

Comments (14)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
manny9876
Level 36 , Israel
15 December 2024, 08:19
From ChatGPT I havent verfied it so feel free to comment if there is a mistake. 1. What do anonymous inner classes become after being compiled? After being compiled, anonymous inner classes become separate class files named using the enclosing class name followed by a $ and a number, e.g., OuterClass$1.class. 2. Can inner classes be inherited? Yes, inner classes can be inherited, but only if they are not private. 3. Can anonymous inner classes be inherited? No, anonymous inner classes cannot be inherited because they do not have a name to refer to. 4. Can you override inner classes? No, you cannot override inner classes, but you can shadow them by declaring another class with the same name in a subclass. 5. What limitations do local classes have? Local classes cannot have static declarations and can only access final or effectively final variables from their enclosing method or block. 6. Can an anonymous inner class contain static methods? No, an anonymous inner class cannot contain static methods because it is implicitly tied to an instance of its enclosing class. 7. Can you create an instance of an inner class if the outer class only has a private constructor? Yes, you can create an instance of an inner class if the outer class only has a private constructor, but it must be done from within the outer class or through a method provided by the outer class. 8. Can you declare inner classes as private? Yes, you can declare inner classes as private. 9. Can you declare anonymous inner classes as private? No, you cannot explicitly declare an anonymous inner class as private because its scope is already limited to the enclosing block or expression. 10. How many inner classes can a class have? A class can have any number of inner classes; there is no predefined limit.
cute potato
Level 46 , Poland, Poland
12 December 2022, 13:19
Hi! Can someone me help with 10th question? How many inner classes can a class have?
Uszek
Level 41 , Wodzisław Śląski, United States
10 March 2023, 18:05
A class can have any number of inner classes, including anonymous inner classes, local classes, member classes, and nested classes. However, having too many inner classes can make the code harder to read and maintain. Inner classes are typically used to group related functionality together or to encapsulate implementation details, and they are most useful when they are used sparingly and thoughtfully. It's also worth noting that each inner class has its own copy of the instance variables of the outer class, which can potentially consume a lot of memory if there are many instances of the inner classes. Therefore, it's generally a good idea to use inner classes only when they are necessary for the structure and functionality of the program.
Lisa L
Level 47 , Nuremberg, Germany
5 May 2022, 21:58
6. In Java 8: no, in newer versions (from 16 on): yes https://www.oracle.com/java/technologies/javase/16-relnote-issues.html#JDK-8246771
Jaime Padilla
Level 41 , Chandler, United States
Expert
30 September 2023, 17:12
While JEP 395 may allow static members within inner classes, this generally refers to named inner classes and nested static members rather than anonymous inner classes. The ability to declare static methods within an anonymous inner class might still be restricted. -ChatGpt I also tried building it to test and it didn't work for me, but I may have done something else wrong.
TomL
Level 30 , Prague, Czech Republic
16 June 2021, 20:09
I'm confused with the 4th question - if I inherit inner class, think there's no problem to override any non-final method of this inner class. But what is meant by "overriding inner class"?
Lisa L
Level 47 , Nuremberg, Germany
5 May 2022, 21:57
They are asking you if polymorphism is working with inner classes. The answer is no. Polymorphism works only for methods. Instance variables and inner classes are different.
public class Polymorphism {

    public static class A {
        public class InnerA {
            public void print() {
                System.out.println("A.InnerA");
            }
        }

        public int someVar = 1;
    }

    public static class B extends A {
        public class InnerA {
            public void print() {
                System.out.println("B.InnerA");
            }
        }

        public int someVar = 321; // variable shadowing

    }

    public static void main(String[] args) {
        A b1 = new B();
        B b2 = new B();

        // fields: no polymorphism, shadowing
        System.out.println(b1.someVar);
        System.out.println(b1.someVar);

        // no polymorphism but also no shadowing
        b1.new InnerA().print();
        b2.new InnerA().print();

        // for methods this should be clear. If it's overridden methods then b1.method() and b2.method() always would
        // invoke the overridden version.
    }
}
Andrei
Level 41
12 April 2021, 19:08
Can someone help me with these please? I haven't found anything on the internet. 7. Can you create an instance of an inner class if the outer class only has a private constructor? 9. Can you declare anonymous inner classes as private?
John Squirrels Website Admin at CodeGym
13 April 2021, 14:10
Please check your DM.
Szymon
Level 41 , Warsaw, Poland
14 April 2021, 19:19
did u get the answer?
Andrei
Level 41
14 April 2021, 19:28
Hi Szymon, yes, here are the answers: 7. Yes. The outer class (which contains it) has access to the private constructor.
public class Solution {
    public static class Test {
        private Test() {
        }
        public void testMethod() {
        }
    }
    public static void main(String[] args) {
        new Test();
    }
}
9. You cannot make the anonymous class private, since it does not use access modifiers at all. There is a good recommendation in the Oracle documentation: "Use anonymous classes when you need a local class for one-time use."
Andrei
Level 41
15 April 2021, 15:06
You are welcome. 😒
Szymon
Level 41 , Warsaw, Poland
3 May 2021, 20:38
Hey man thanks don't be mad.
matemate123
Level 50 , Kraków, Poland
18 April 2023, 15:36
7. Can you create an instance of an inner class if the outer class only has a private constructor?
public class Solution {
    private Solution() {

    }

    public static class Test {
        public void testMethod() {
        }
    }

    public class TestNonStatic {
        public void testMethod2() {
        }
    }
    public static void main(String[] args) {
        new Test();
        new Solution().new TestNonStatic();
    }
}
Still YES