Java Class 8
Java Class 8
In the example below, the Car class (subclass) inherits the attributes
and methods from the Vehicle class (superclass):
class Vehicle {
protected String brand = "Ford";
public void honk() {
System.out.println("Tuut, tuut!");
}
}
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class InnerClass {
int y = 5;
}
}
class OuterClass {
int x = 10;
Note: just like static attributes and methods, a static inner class does not
have access to members of the outer class.
Access Outer Class From Inner Class
One advantage of inner classes, is that they can access
attributes and methods of the outer class:
class OuterClass {
int x = 10;
class InnerClass {
public int myInnerMethod() {
return x;
}
}
}