Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.

Similar presentations


Presentation on theme: "Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes."— Presentation transcript:

1 Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes

2 Programming With Java ICS201 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: 1.Inner ( member ) classes (non-static). 2.Static inner classes. 3.Local classes (defined inside a block of Java code). 4.Anonymous classes (defined inside a block of Java code). Used with GUIs

3 Programming With Java ICS201 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 Programming With Java ICS201 University Of Hail4 public class OuterClass { private class InnerClass { Declarations_of_InnerClass_Instance_Variables Definitions_of_InnerClass_Methods } Declarations_of_OuterClass_Instance_Variables Definitions_of_OuterClass_Methods } Definition of InnerClass

5 Programming With Java ICS201 Introductory Example Inner class inside Person

6 Programming With Java ICS201 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 Programming With Java ICS201 7 Simple Use of Inner Classes o 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 o 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 Programming With Java ICS201 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 Programming With Java ICS201 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 Programming With Java ICS201 University Of Hail10 Write a program that illustrates how to use inner classes. 1. Declare an outer class named Vehicle that contains: a)An integer variable called yearmodel ; b)An integer variable called range; c)A constructor to initialize the different fields of the Vehicle class; d)A method display() that returns a string containing the yearmodel and the range of a given vehicle. Example:

11 Programming With Java ICS201 University Of Hail11 class Vehicle { int yearmodel ; int range ; public Vehicle (int y, int n){ this.yearmodel= y ; this.range = n ; } public String display(){ return yearmodel +" " +range ; }

12 Programming With Java ICS201 University Of Hail12 2. Declare an inner class called Car that contains: a)A Vehicle variable called vec; b)An integer variable called nbwheels ; c)An integer variable called nbpassengers ; d) A constructor to initialize the field of the Car class; e)A toString() method that returns a description of the Car. Hint: invoke the display() method of the Vehicle class. Example:

13 Programming With Java ICS201 University Of Hail13 class Car { Vehicle vec ; int nbpassengers ; int nbwheels ; Car (Vehicle v1, int n, int nb){ this.vec= v1 ; this.nbpassengers = n ; this.nbwheels = nb ; } public String toString(){ return display() +" " +nbpassengers +" " +nbwheels; }

14 Programming With Java ICS Declare an inner class called MotorCycle that 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 Hail14 Example:

15 Programming With Java ICS201 class MotorCycle { Vehicle vec ; int nbpassengers ; int nbwheels ; MotorCycle (Vehicle v1, int n, int nb){ this.vec= v1 ; this.nbpassengers = n ; this.nbwheels = nb ; } public String toString(){ return display() +" " +nbpassengers +" " +nbwheels; } } // outer class University Of Hail15

16 Programming With Java ICS201 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 Hail16 Example:

17 Programming With Java ICS201 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 Hail17


Download ppt "Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes."

Similar presentations


Ads by Google