JavaRush /Java Blog /Random EN /Level 23. Answers to interview questions on the level top...
zor07
Level 31
Санкт-Петербург

Level 23. Answers to interview questions on the level topic

Published in the Random EN group
Level 23. Answers to interview questions on the topic of level - 1
  1. What are the inner classes?

    • Nested classes are divided into two types: static and non-static.
    • Nested classes declared as static are called static nested classes.
    • Nested non-static classes are called inner classes.
  2. What does an anonymous inner class compile to?

    To an inner non-static class

  3. Why use keyword finalwhen creating anonymous classes?

    If an anonymous inner class is defined and it needs to use objects defined outside of this inner class, the compiler requires that the references passed to them be declared immutable (final). Without such a declaration, you will receive an error when compiling the program.

  4. How to properly create an inner class object?

    Inner (non-static) classes, like variables and methods, are associated with an object of the outer class. Inner classes also have direct access to the fields of the outer class. Such classes cannot contain static methods and fields. Inner classes cannot exist without an instance of the outer one. To create an object:

    Outer outer = new Outer();
    Innter inner = outer.new Inner();
  5. How to properly create an object of a nested class?

    The syntax for creating a nested class object is:

    OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
  6. Is it possible to create static methods/variables in an inner class?

    Static methods/variables cannot be declared in an internal class (not a nested one).

    Inner (non-static) classes, like variables and methods, are associated with an object of the outer class. Such classes cannot contain static methods and fields.

  7. Name any three inner classes?

    1. private static class Holder—nested class HashMapfrom java.util.
    2. The interface Maphas interface Entry<K,V>, which is again in HashMapand is implemented in another nested class static class Entry<K,V> implements Map.Entry<K,V>.
    3. private static class IntegerCachein class Integer.
  8. How do inner classes solve the problem of multiple inheritance in Java?

    Because Multiple inheritance of classes is prohibited in Java, this problem is solved with the help of internal classes: in the class we need, we declare an internal class and inherit it from the required class. Example:

    class Tiger extends Cat
    {
    
     public void tigerRun()
     {
      .....
     }
    
    public void startTiger()
     {
      TigerThread thread = new TigerThread();
      thread.start();
     }
    
     class TigerThread extends Thread
     {
      public void run()
      {
       tigerRun();
      }
     }
    }
  9. What is the difference between interface-based and class-based anonymous classes?

    An anonymous class according to JLS 15.9.5 is an expression in which the declaration of a new class and its initialization are combined:

    • When declaring a class, a new class will be created that derives from the specified class when used as the base of another class, or implements the interface when used as the base of an interface.
    • During initialization, a new object will be created and a reference will be returned to it: the anonymous class is concrete.

    Thus, the only difference in interface-based and class-based anonymous classes is the number of abstract methods that need to be implemented.

    Initializing an anonymous class based on an interface will require the implementation of each method, while initializing an anonymous class based on an abstract/concrete class will allow behavior to change through method overloading and will require implementation of abstract methods.

  10. Is it possible to create an anonymous static nested class?

    No, only a variable becomes static, not a class.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION