Inner or Nested Classes Y Daniel Liang Introduction

  • Slides: 15
Download presentation
Inner or Nested Classes Y. Daniel Liang Introduction to Java Programming

Inner or Nested Classes Y. Daniel Liang Introduction to Java Programming

Objectives • Use inner classes • Use anonymous classes • Programs Test. Circle Test

Objectives • Use inner classes • Use anonymous classes • Programs Test. Circle Test Outer. Class Test. Circle. Anonymous. java and Test. Circle Close. Demo Y. Daniel Liang Introduction to Java Programming

Inner or Nested Classes An inner class, or nested class, is a class declared

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

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 Tool. Kit Y. Daniel Liang Introduction to Java Programming

#1. Two separate classes in #2 Inner class one file syntax: public class. Test{

#1. Two separate classes in #2 Inner class one file syntax: public class. Test{ syntax: public class Test{ … . . . }// end class Test //Inner class public class A { . . . } // end class A } // end class Test See program #1 Example Test. Circle. 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

Inner Class Syntax Example of an inner Class declared in the method of the outer class: public class Driver. Outer. Class. Name { public static void main (String [ ] arg) { class Inner. Class. Name { methods fields } // end Inner. Class } // end main method. . . }// end Driver. Outer. Class. Name Y. Daniel Liang Introduction to Java Programming

Inner Class Syntax Example of an inner class declared inside an outer class; public

Inner Class Syntax Example of an inner class declared inside an outer class; public class Outer. Class. Name { private int data = 0; // data member /** a method in the outer class */ public void a. Method() { // Do Something Inner. Class instance = new Inner. Class(); instance. mi(); } // an inner class public class Inner. Class { // a method in the Inner class public void mi() { data++; //Directly reference data and method defined a. Method(); // in the outer class method } // end method mi() } // end Inner. Class }// end Outer. Class. Name Y. Daniel Liang Introduction to Java Programming

Inner or Nested Classes Features of an inner (or nested) class: • An inner

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. : •

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 Outer. Class. 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. Inner. Class inner. Object = new outer. Object. Inner. Class(); Y. Daniel Liang Introduction to Java Programming

OUTER Class Compilation • You must name the file the same name as the

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 Show. Inner. Class. java • Two object classes generated: Show. Inner. Class. class Show. Inner. Class$Inner. Class. class See program Show. Inner. Class Y. Daniel Liang Introduction to Java Programming

Anonymous Classes • An anonymous class is a special case of an inner (nested)

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 a. Coin = new Coin(0. 1, “dime”); data. add(a. Coin); 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

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(). find. Area() ); Run programs: Test. Circle. Anonymous. java and Test. Circle Y. Daniel Liang Introduction to Java Programming

Other Inner and Anonymous Classes Examples: See Close. Demo My. Code. To. Handle. Win.

Other Inner and Anonymous Classes Examples: See Close. Demo My. Code. To. Handle. Win. Close System. out. println() System. exit(0) +main(String[]args): void set. Size(400, 100) set. Visible(true) add. Window. Listener() <<interface>> Window. Listener (Many methods) Y. Daniel Liang Introduction to Java Programming

Other Inner and Anonymous Classes Examples: See Close. Demo 2 A rewrite of Close.

Other Inner and Anonymous Classes Examples: See Close. Demo 2 A rewrite of Close. Demo 2 +main(String[] args): void set. Size(400, 100) set. Visible(true) add. Window. Listener(new Window. Listener) +window. Closing(Window. Event): void +window. Opened(Window. Event): void +window. Closed(Window. Event): void +window. Deiconified(Window. Event): void +window. Iconified(Window. Event): void +window. Activated(Window. Event): void +window. Deactivated(Window. Event): void Y. Daniel Liang Introduction to Java Programming

Anonymous Classes – Two different opinions “An anonymous class can be useful where the

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