1 / 12

Object Oriented Programming in Java

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.

Télécharger la présentation

Object Oriented Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented ProgramminginJava 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 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

  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.

More Related