An inner class is a nested class, means a class within a class. Inner class is like other member variables of outer class and can access all the private, public and protected members of outer class. Inner class is used when the class is to be used only by the outer class and not need to be accessed by other classes. One example is a vehicle and wheel. A wheel is nothing without any vehicle, it has to be used only with the vehicle. Here is the example of Wheel class in Vehicle class -
public class Vehicle {
private String vehicleType;
private String vehicleColor;
private int noOfWheels = 0;
// Class Wheel Starts
class Wheel {
String wheelType;
int wheelSize;
Wheel(String type, int size) {
wheelType = type;
wheelSize = size;
noOfWheels++;
}
String getType() {
return this.wheelType;
}
void setType(String type) {
this.wheelType = type;
}
int getSize() {
return this.wheelSize;
}
void setSize(int size) {
this.wheelSize = size;
}
}
// Class Wheel Ends
Vehicle(String type, String color) {
vehicleType = type;
vehicleColor = color;
}
String getType() {
return this.vehicleType;
}
String getColor() {
return this.vehicleColor;
}
void setType(String type) {
this.vehicleType = type;
}
void setColor(String color) {
this.vehicleColor = color;
}
int getNoOfWheels() {
return this.noOfWheels;
}
}
Vehicle class has the fields - vehicle type, vehicle color, no of wheels and a wheel class member. (You can add more features - this is just for example) The Vehicle class constructor initializes vehicle type and vehicle color. There are getter and setter methods for vehicle type and vehicle color, and getter method for no of wheels.
Member class Wheel has the fields - wheel type and wheel size. The Wheel class constructor initializes wheel type and wheel color. And also accesses the field no of wheels from outer class Vehicle and increments it by 1. So, whenever a new wheel is added to the vehicle, it will increase the no of wheels.
Here is the class VehicleDemo, which creates an instance of class Vehicle and then creates 4 instances of Wheel class for the vehicle.
public class VehicleDemo {
public static void main(String args[]) {
Vehicle v = new Vehicle("Truck", "red");
System.out.println("Type of Vehicle is: " + v.getType());
System.out.println("Color of Vehicle is: " + v.getColor());
Vehicle.Wheel wh1 = v.new Wheel("thick", 28);
Vehicle.Wheel wh2 = v.new Wheel("thick", 28);
System.out.println("No Of Wheels: " + v.getNoOfWheels());
Vehicle.Wheel wh3 = v.new Wheel("thin", 15);
Vehicle.Wheel wh4 = v.new Wheel("thin", 15);
System.out.println("No Of Wheels: " + v.getNoOfWheels());
}
}
Here is the output:
Type of Vehicle is: Truck
Color of Vehicle is: red
No Of Wheels: 2
No Of Wheels: 4
Please pay close attention how the inner class object is created.
Vehicle v = new Vehicle("Truck", "red");
Vehicle.Wheel wh1 = v.new Wheel("thick", 28);
Note: Inner classes are very useful in cases when inner class is related to outer class, and there is no use of the inner class outside from the outer class.
Code can be accessed here: Github Link
No comments:
Post a Comment