120 likes | 147 Views
Object Oriented Programming in Java. Habib Rostami Lecture 10. Inner class. An inner class is a class declared inside another class The class that encloses it is called the outer or top-level class. A “normal class” is a direct member of a package; inner classes are not.
E N D
Object Oriented ProgramminginJava Habib Rostami Lecture 10
Inner class • An inner class is a class declared inside another class • The class that encloses it is called the outer or top-level class. • A “normal class” is a direct member of a package; inner classes are not.
Types of inner classes • Static member classes • Member classes • Local classes • Anonymous classes
Types of inner classes • Static member classes • Is static, has access to all static methods of the outer class • Member classes • Is instance-specific and has access to any and all methods and members, even the parent's this reference.
Types of inner classes • Local classes • declared within a block of code and are visible only within that block, just as any other method variable. • Anonymous classes • A class with no name • More on this later…
Inner classes • When inner classes are compiled, the compiler generates classes of this format: • ContainingClass$InnerClass.class • If you want to distinguish between the inner class's methods/fields and that of the containing class, use "ContainingClass.this" • e.g., in PersonMoverApplet6's MoverButton inner class, we say • PersonMoverApplet6.this.repaint()
Anonymous classes • A class with no name, which is why it’s called an “anonymous class” • Combines the following into one step: • class declaration • creation of an instance of the class • Anonymous objects cannot be instantiated from outside the class in which the anonymous class is defined • it can only be instantiated from within the same scope in which it is defined
Why use an anonymous class? • It lessens the amount of “.java” files necessary to define the application. • anonymous classes can access the static and instance variables of the enclosing outer class. • Can be time-savers • Useful when implementing listeners in GUI programs • See sample code in access folder of slide7.zip
Anonymous classes • When you compile classes that contain anonymous classes, the compiler will create these class files: • ClassName$SomeNumber • SomeNumber is the sequence number for the anonymous class • So, if you have a class named MyAnonTest.java that contains two anonymous classes, the following class files will be generated by the compiler: • MyAnonTest.class, MyAnonTest$1.class and MyAnonTest$2.class
Example button.addActionListener( new ActionListener() { //anonymous inner class for WindowAdapter public void actionPerformed(ActionEvent ae) { System.out.println("Clicked!"); } }); • Usual way: add a listener for each component and define the action inside the actionPerformed() method • Would have a very long if-else chain if many buttons used • With anonymous classes, you can directly add and define the action on the component
Rules for Anonymous classes • An anonymous class must always extend a super class or implement an interface but it cannot have an explicit extends or implements clause • An anonymous class must implement all the abstract methods in the super class or the interface. • An anonymous class always uses the default constructor from the super class to create an instance
When to use Inner Classes • In general • Use when you do NOT need to reuse or extend the class outside the containing class. • e.g., for handling events that are specific to that applet • Non-Anonymous Inner Classes • Use when you create several different instances of the same inner class within the same class • Anonymous Inner Classes • Use when you only need one instance of that class and never need to reuse it.