Inner classes Flashcards
What inner classes are there?
Inner class, local class, anonymous class, static nested class
What are local classes?
Those are ones created inside a method
What is relationship of local classes and upper class?
Inner class can read all instance variables and methods from upper class or method, regardless if they are final or effectively final. No initialization is needed
What are anonymous classes?
Implementation of a class or interface. They are implemented inline with a reference type. It is actually extending and implementation.
What are static nested classes?
Classes that are defined inside another class, and they are static.
What is relationship of static nested classes and upper class?
Members of a static nested classes cannot use members of upper class, but upper class can use members from static nested class -> A.B.something()
What are inner classes?
Inner class is a class defined inside another class - it is nested.
What are characteristics of inner classes?
They can have all access modifiers, they can extend and implement. They can have static members.
What is relationship of inner classes and upper class?
Inner most class can access all members from its upper classes. It can access generics that are passed to upper class, but need to watch out for overshadowing (must use different letters for each class)
How do we instantiate nested classes? Let’s say C is inside B, B is inside A.
Need to instantiate A -> A a = new A();
Need to instantiate B -> A.B b = a.new B();
Need to instantiate C -> A.B.C c = b.new C();
How can we access instance members of class A from its nested class C?
A.this.something()
How can we access instance members of class B from its nested class C?
B.this.something()