1 / 15

Inner or Nested Classes

Inner or Nested Classes. Objectives Use inner classes Use anonymous classes Programs TestCircle Test OuterClass TestCircleAnonymous.java and TestCircle CloseDemo. Inner or Nested Classes. An inner class, or nested class, is a class declared inside another

cowleyr
Download Presentation

Inner or Nested Classes

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. Inner or Nested Classes Y. Daniel Liang Introduction to Java Programming

  2. Objectives • Use inner classes • Use anonymous classes • Programs • TestCircle • Test • OuterClass • TestCircleAnonymous.java and TestCircle • CloseDemo Y. Daniel Liang Introduction to Java Programming

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

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

  5. #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 classA } // end class Test See program #1 Example TestCircle.java See program #2 Example: Test.java Y. Daniel Liang Introduction to Java Programming

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

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

  8. 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 See Program // ShowInnerClass.java: Y. Daniel Liang Introduction to Java Programming

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

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

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

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

  13. Other Inner and Anonymous Classes Examples: See CloseDemo Y. Daniel Liang Introduction to Java Programming

  14. Other Inner and Anonymous Classes Examples: See CloseDemo2A rewrite of CloseDemo Y. Daniel Liang Introduction to Java Programming

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

More Related