170 likes | 506 Views
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.
E N D
Chapter 13 Inner Classes University Of Hail
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
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
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
Introductory Example Inner class inside Person
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 ( ); }}
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
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
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
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
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
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
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
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
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
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
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