Level 31

Level 23

Published in the Random EN group
Level 23
  1. What are 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 the keyword finalwhen creating anonymous classes?

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

  4. What is the correct way to create an object of the inner class?

    Inner (non-static) classes, like variables and methods, are associated with the 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. To create an object:

    Outer outer = new Outer();
    Innter inner = outer.new Inner();
  5. How to correctly 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 inner class (not nested).

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

  7. Can you name any three inner classes?

    1. private static class Holder— nested class HashMapfrom java.util.
    2. The interface Maphas interface Entry<K,V>, which again is in HashMapand 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 in Java is prohibited, this problem is solved with the help of inner classes: in the class we need, we declare an inner class and inherit it from the required class. Example:

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

    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:

    • Declaring a class will create a new class 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.
    • Upon 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 anonymous classes derived from an interface and a class is the number of abstract methods that need to be implemented.

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

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

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

  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet