LEC 13 (Inner Classes)
LEC 13 (Inner Classes)
Inner/Outer Classes
public class Outer { private class Inner { // inner class instance variables // inner class methods
Using an inner class as a helping class is one of the most useful applications of inner classes
If used as a helping class, an inner class should be marked private
Inner and Outer Classes Have A ccess to Each Other's Private Members
Within the definition of a method of an inner class:
It is legal to reference a private instance variable of the outer class It is legal to invoke a private method of the outer class Essentially, the inner class has a hidden reference to the outer class
Within the definition of the inner or outer classes, the modifiers public and private are equivalent
There are certain situations, however , when an inner class must be static
If an object of the inner class is created within a static m ethod of the outer class If the inner class must have s tatic members
T o invoke a static method or t o name a static variable of a static inner class within the outer class, preface each with the name of the inner class and a dot
An outer class can be a derived class An inner class can be a derived class also
Anonymous Classes
If an object is to be created, but there is no need to name the object's class, then an anonymous class definition can be used The class definition is embedded inside the expression with the new operator An anonymous class is an abbre viated notation for creating a simple local object "in-line" within any expression, s imply by wrapping the desired code in a "new" expression. Anonymous classes are sometimes used when they are to be assigned to a variable of another type The other type must be such that an object of the anonymous class is also an object of the other type The other type is usually a Java interface Not every inner class should be anonymous, but very simple "one-shot" local objects are such a common case that they merit some syntactic sugar .
Anonymous Classes
Anonymous Classes
Anonymous Classes
THANK YOU