Java Inner Classes
Java Inner Classes
Inner Classes
Introduction
• In Java, it is also possible to nest classes (a class within a class).
The purpose of nested classes is to group classes that belong
together, which makes your code more readable and
maintainable.
• To access the inner class, create an object of the outer class, and
then create an object of the inner class:
Sample Code for Inner and Outer Classes
class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
}
}
class OuterClass {
int x = 10;
private class InnerClass {
int y = 5; Main.java:13: error: OuterClass.InnerClass has private access in OuterClass
OuterClass.InnerClass myInner = myOuter.new InnerClass();
} }
^
public class Main {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
Static Inner Class
class OuterClass {
• An inner class can also be static, which means int x = 10;
that you can access it without creating an
object of the outer class: static class InnerClass {
int y = 5;
}
}
// Outputs 5
Garbage Collection in Java
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Multilevel Inheritance Example
• When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the
example given below, BabyDog class inherits the Dog class which again inherits the Animal class,
so there is a multilevel inheritance.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Hierarchical Inheritance Example
• When two or more classes inherits a single class, it is known as hierarchical inheritance. In the
example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical
inheritance.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Sample Exercise:
• To write a Java program which implements multiple classes related with inheritance. The program should have a
main class Product, subclasses Book and CD. Also create a sub-class for Book called Scientific. All the Subclass
should inherit the properties of the main class and it can have its own properties also. Observe the inheritance
hierarchy output and display.
Develop a Java program to define a class called Account which contains two private data elements, an integer
account number and a floating point account balance and three methods:
A constructor that allows the user to set initial values for account number and account balance and a default constructor that prompts for
the input of the values for the above data members.
A method which reads a character value for transaction type (D for deposit and W for withdrawal), and a floating point value for transaction
amount, and updates account balance.
A method, which prints on the screen the account number and account balance.