Inner Class - faculty.uoh.edu.sa

Download Report

Transcript Inner Class - faculty.uoh.edu.sa

Programming With Java
ICS201
Chapter 13
Inner Classes
University Of Hail
1
Inner Classes
Programming With Java
ICS201
• 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).
3.Local classes (defined inside a block of Java code).
4.Anonymous classes (defined inside a block of Java
code).
Used with GUIs
2.Static inner classes.
Programming With Java
ICS201
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
Programming With Java
ICS201
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
4
Programming With Java
ICS201
Introductory Example
Inner class
inside
Person
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 ( );
}
}
Programming With Java
ICS201
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
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
Example(Use of Inner Classes)
Programming With Java
ICS201
public class University{
private class Student {
private String ID ;
private int Age ;
private String Grade ;
private Student S ;
public String getInfo( ) {
return S.Grade + “ “ + S.getStudentInfo() ;
}
} // end of outerclass
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
9
Programming With Java
ICS201
Example:
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.
University Of Hail
10
Programming With Java
ICS201
class Vehicle {
int yearmodel ;
int range ;
public Vehicle (int y , int n){
this.yearmodel= y ;
this.range = n ;
}
public String display(){
return yearmodel +" " +range ;
}
University Of Hail
11
Programming With Java
ICS201
Example:
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.
University Of Hail
12
Programming With Java
ICS201
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;
University Of Hail
13
Programming With Java
ICS201
Example:
3. 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 Hail
14
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 Hail
15
Programming With Java
ICS201
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
16
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 Hail
17