Tech With Tim Logo
Go back

Inner Classes

Inner Class

An inner class is very similar to a regular class. The main difference is that it is defined inside another class! Typically when we have an object we only need to use from inside a class we will define an inner class to represent that object. We can define this inner class from inside the class body or inside a method. We can also have cascading inner classes, or in other words inner classes inside other inner classes.

We will start by creating an inner class and then working with it. Typically when we create an inner class we declare it as private. This means that we can only create an instance of the inner class from inside the outer class.

Here's a small example:

public class OuterClass{
    private class InnerClass{
        public void display(){
            System.out.println("This is an inner class");
        }
    }

    public void display(){
        System.out.println("This is an outer class");
    }
}

For some more detailed examples refer to the video.

Design & Development by Ibezio Logo