Chapter 13
This presentation is the property of its rightful owner.
Sponsored Links
1 / 17

Chapter 13 Inner Classes PowerPoint PPT Presentation


  • 138 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 13 inner classes

Chapter 13

Inner Classes

University Of Hail


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.

    • 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

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


Chapter 13 inner classes

  • 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


Chapter 13 inner classes

Introductory Example

Inner class inside Person


Member classes

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 classes1

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

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

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


Chapter 13 inner classes

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


Chapter 13 inner classes

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


Chapter 13 inner classes

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


Chapter 13 inner classes

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


Chapter 13 inner classes

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


Chapter 13 inner classes

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

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


Chapter 13 inner classes

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


  • Login