Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming in Java Habib Rostami Lecture 10.

Similar presentations


Presentation on theme: "Object Oriented Programming in Java Habib Rostami Lecture 10."— Presentation transcript:

1 Object Oriented Programming in Java Habib Rostami Lecture 10

2 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.

3 Types of inner classes Static member classes Member classes Local classes Anonymous classes

4 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.

5 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…

6 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()

7 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

8 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

9 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

10 Example 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 button.addActionListener( new ActionListener() { //anonymous inner class for WindowAdapter public void actionPerformed(ActionEvent ae) { System.out.println("Clicked!"); } });

11 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

12 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.


Download ppt "Object Oriented Programming in Java Habib Rostami Lecture 10."

Similar presentations


Ads by Google