1 / 17

Chapter 13 Inner Classes

Chapter 13 Inner Classes. Inner Classes. Inner (or nested) classes are classes defined within other classes: The class that includes the inner class is called the outer class There are four categories of inner classes in Java: Inner ( member ) classes (non-static). Static inner classes.

yamin
Télécharger la présentation

Chapter 13 Inner 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. Chapter 13 Inner Classes University Of Hail

  2. Inner Classes • Inner (or nested) classes are classes defined within other classes: • The class that includes the inner class is called the outer class • There are four categories of inner classes in Java: • Inner (member ) classes (non-static). • Static inner classes. • Local classes (defined inside a block of Java code). • Anonymous classes (defined inside a block of Java code). Used with GUIs

  3. Simple Use of Inner Classes • An inner class definition is a member of the outer class in the same way that the instance variables and methods of the outer class are members. 3

  4. Definition of InnerClass • public class OuterClass • { • private class InnerClass • { • Declarations_of_InnerClass_Instance_Variables • Definitions_of_InnerClass_Methods • } • Declarations_of_OuterClass_Instance_Variables • Definitions_of_OuterClass_Methods • } University Of Hail

  5. Introductory Example Inner class inside Person

  6. Member classes • A member class is an “ordinary” inner class • class Outer {int n; class Inner {int ten = 10; void setNToTen ( ) { n = 10; } } void setN ( ) { new Inner ( ).setNToTen ( ); }}

  7. Simple Use of Inner Classes • Within the definition of a method of an inner class: • It is legal to reference a private instance variable of the outer class • It is legal to invoke a private method of the outer class • Within the definition of a method of the outer class • It is legal to reference a private instance variable of the inner class on an object of the inner class • It is legal to invoke a (nonstatic) method of the inner class as long as an object of the inner class is used as a calling object 7

  8. The .class File for an Inner Class • Compiling any class in Java produces a .class file named ClassName.class • Compiling a class with one (or more) inner classes causes both (or more) classes to be compiled, and produces two (or more) .class files : • ClassName.class • ClassName$InnerClassName.class

  9. Example(Use of Inner Classes) public class University{ private class Student { private String ID ; private int Age ; private String Grade ; public Student (string I, int A, String G) { this.ID = I ; this.Age = A ; this.Grade = G ; } public String getGrade( ) { return Grade ; } public String getStudentInfo( ) { return ID +” ” +Age; } } // end of inner class private Student S ; public String getInfo( ) { return S.Grade + “ “ + S.getStudentInfo() ; } } // end of outerclass ……… 9

  10. Example: Write a program that illustrates how to use inner classes. • Declare an outer class named Vehicle that contains: • An integer variable called yearmodel ; • An integer variable called range; • A constructor to initialize the different fields of the Vehicle class; • A method display() that returns a string containing the yearmodel and the range of a given vehicle. University Of Hail

  11. class Vehicle { intyearmodel ; int range ; public Vehicle (int y , int n){ this.yearmodel= y ; this.range = n ; } public String display(){ return yearmodel +" " +range ; } University Of Hail

  12. Example: • 2. Declare an inner class called Car that contains: • A Vehicle variable called vec; • An integer variable called nbwheels ; • An integer variable called nbpassengers ; • A constructor to initialize the field of the Car class; • A toString() method that returns a description of the Car. • Hint: invoke the display() method of the Vehicle class. University Of Hail

  13. class Car { Vehicle vec ; intnbpassengers ; intnbwheels ; Car (Vehicle v1 , int n , intnb){ this.vec= v1 ; this.nbpassengers= n ; this.nbwheels= nb ; } public String toString(){ return display() +" " +nbpassengers +" " +nbwheels; } } University Of Hail

  14. Example: 3. Declare an inner class called MotorCyclethat contains: • A Vehicle variable called vec; • An integer variable called nbpassengers; • A constructor to initialize the field of the MotorCycle class; • A toString() method that returns a description of the MotorCycle. Hint: invoke the display() method of the Vehicle class. University Of Hail

  15. class MotorCycle { Vehicle vec ; intnbpassengers ; intnbwheels ; MotorCycle(Vehicle v1 , int n , intnb){ this.vec= v1 ; this.nbpassengers = n ; this.nbwheels = nb ; } public String toString(){ return display() +" " +nbpassengers +" " +nbwheels; } } } // outer class University Of Hail

  16. Example: Instantiate the class Car and invoke the tostring() method. Instantiate the class MotorCycle and invoke the toString() method. Note: to instantiate an inner class, use the following syntax: Outerclass obj1 = new outerclass( ) ; Outerclass.innerclass obj2 = obj1.new innerclass( ) ; University Of Hail

  17. class test{ public static void main(String[] ar){ Vehicle V = new Vehicle(2008 , 40) ; Vehicle.Car C = V.new Car(V , 5, 4); // an inner class System.out.println(C.toString()) ; Vehicle V1 = new Vehicle(2006 , 34) ; Vehicle.MotorCycle M = V1.new MotorCycle(V1 , 2 , 3); // an inner class System.out.println(M.toString()) ; } } University Of Hail

More Related