Inner Classes

Download Report

Transcript Inner Classes

Inner or Nested Classes
Y. Daniel Liang Introduction to
Java Programming
Objectives
•Use inner classes
•Use anonymous classes
•Programs
TestCircle
Test
OuterClass
TestCircleAnonymous.java and
TestCircle
CloseDemo
Y. Daniel Liang Introduction to
Java Programming
Inner or Nested Classes
An inner class, or nested class, is a class declared inside another
class. Inner classes are usually used for tactical classes that
should not be visible elsewhere in a program
An inner class is only for supporting the work of its containing
outer class. And it is recommended that the inner class not be
used by other classes.
A simple use of inner classes is to combine dependent classes
into a primary class.
Y. Daniel Liang Introduction to
Java Programming
Inner or Nested Classes
Advantages:
•
In some applications, you can use an inner class to make programs
simple, concise and easy to understand.
• Many Java development tools use inner classes to generate adapters
for handling events.
Later, you will use inner classes when we learn AWT Event-driven
classes
AWT -Abstract Windowing ToolKit
Y. Daniel Liang Introduction to
Java Programming
#1.Two separate classes in
#2 Inner class
one file syntax:
syntax:
public classTest{
public class Test{
…
...
}// end class Test
//Inner class
public class A {
class A {
...
...
} // end class A
} // end class A
} // end class Test
See program #1 Example TestCircle.java
See program #2 Example: Test.java
Y. Daniel Liang Introduction to
Java Programming
Inner Class Syntax
Example of an inner Class declared in the method of the outer
class:
public class DriverOuterClassName {
public static void main (String [ ] arg) {
class InnerClassName
{
methods
fields
} // end InnerClass
} // end main method
...
}// end DriverOuterClassName
Y. Daniel Liang Introduction to
Java Programming
Inner Class Syntax
Example of an inner class declared inside an outer class;
public class OuterClassName {
private int data = 0; // data member
/** a method in the outer class */
public void aMethod() {
// Do Something
InnerClass instance = new InnerClass();
instance.mi();
}
// an inner class
public class InnerClass
{
// a method in the Inner class
public void mi() {
data++;
//Directly reference data and method defined
aMethod(); // in the outer class method
} // end method mi()
} // end InnerClass
}// end OuterClassName
Y. Daniel Liang Introduction to
Java Programming
Inner or Nested Classes
Features of an inner (or nested) class:
• An inner class can reference the data and methods
declared in the outer class in which it is nested.
•
You do not need to pass the reference of an object of the
outer class to the constructor of the inner class.
•
An inner class can be declared public, protected, or
private based on the visibility rules applied to a member
of the class.
•
An inner class can be declared static. A static inner class
can be accessed using the outer class name. A static
inner class cannot access non-static members of the
outer class
Y. Daniel Liang Introduction to
Java Programming
Inner or Nested Classes
Features of an inner (or nested) class contd.:
• Objects of an inner class are usually created in the outer class
But you can also create an object of an inner class from
another class. For a non-static inner class, you must first
create an instance of the outer class, then instantiate an object
for the inner class.
See Program OuterClass.java
for an example of how a non-static inner method, for a non-static
inner class, is invoked by a static method.
• For a static inner class use the following syntax to instantiate an object for it:
Outerclass.InnerClass innerObject = new outerObject. InnerClass();
Y. Daniel Liang Introduction to
Java Programming
OUTER Class Compilation
• You must name the file the same name as the outer class.
• When you compile the outer class, the compiler will generate two
.class files- one for the outer class and one for the inner class
• Compile the outer class.
javac ShowInnerClass.java
• Two object classes generated:
ShowInnerClass.class
ShowInnerClass$InnerClass.class
See program ShowInnerClass
Y. Daniel Liang Introduction to
Java Programming
Anonymous Classes
• An anonymous class is a special case of an inner
(nested) class.
• A class is anonymous if it does not have a name. In a
program, something that is only used once doesn’t
usually need a name. For example, you can replace
Coin aCoin = new Coin(0.1, “dime”);
data.add(aCoin);
with
data.add(new Coin(0.1, “dime”));
Y. Daniel Liang Introduction to
Java Programming
Anonymous Object
Often you create an object and assign it to a variable. Later
you can use the variable to reference the object.
Sometimes an object does not need to be reference later.
Therefore, you can create an object without explicitly
assigning it to a variable. For example:
new Circle();
Or
System.out.println (“Area is “ + new Circle().findArea() );
Run programs: TestCircleAnonymous.java and TestCircle
Y. Daniel Liang Introduction to
Java Programming
Other Inner and Anonymous Classes Examples:
See CloseDemo
CloseDemo
MyCodeToHandleWinClose
System.out.println()
System.exit(0)
+main(String[]args):void
setSize(400,100)
setVisible(true)
addWindowListener()
<<interface>>
WindowListener
(Many methods)
Y. Daniel Liang Introduction to
Java Programming
Other Inner and Anonymous Classes Examples: See CloseDemo2
A rewrite of CloseDemo
CloseDemo2
+main(String[] args):void
setSize(400,100)
setVisible(true)
addWindowListener(new WindowListener)
+windowClosing(WindowEvent):void
+windowOpened(WindowEvent):void
+windowClosed(WindowEvent):void
+windowDeiconified(WindowEvent):void
+windowIconified(WindowEvent):void
+windowActivated(WindowEvent):void
+windowDeactivated(WindowEvent):void
Y. Daniel Liang Introduction to
Java Programming
Anonymous Classes – Two different opinions
“An anonymous class can be useful where the
class definition is short and simple. This
concept should be used sparingly as it tend to
make the code very difficult to understand.”
(Richard C. Lee and William. Tepfenhart)
“Anonymous Classes have become extremely
popular with professional Java programmers. You will
see them quite often when looking at
professional Java code.” ( Cay Horstmann)
Y. Daniel Liang Introduction to
Java Programming