Java - Inner Class vs Sub Class
Last Updated : 28 Apr, 2025
Inner Class
In Java, one can define a new class inside any other class. Such classes are known as Inner class. It is a non-static class, hence, it cannot define any static members in itself. Every instance has access to instance members of containing class. It is of three types:
- Nested Inner Class
- Method Local Inner Class
- Anonymous Inner Class
Generally, it makes code slightly complicated but, it is very useful with abstract window toolkit and AWT(GUI) event handling.
Example:
Java public class Outer { // outer class void showData() { // method inside outer class --- } public class Inner { // inner class inside outer class } }
Sub Class
In Java, a subclass is a class that derives/inherited from another class. A subclass inherits everything (like behavior, state of the class, etc. ) from its ancestor classes. For a better understanding of it, we just need to know about the superclass. The superclass is an immediate ancestor of the sub-class. As a subclass has the property of inheriting the properties of an ancestor, hence, it can modify or override the methods of the superclass. For creating a subclass from any other class, we need to use extends clause in the class declaration.
Example :
Java // super class public class Mahindra { public Mahindra() { System.out.println("I am super class"); } } // sub class public class Scorpio extends Mahindra { public Scorpio() { System.out.println("I am sub class"); } }
Difference Table
The difference between the Inner class and sub-class are listed below:
Inner Class | Sub Class |
---|
It is a class that is nested within another class. | It is a class that inherits from another class. |
It can be accessed with the reference of the outer class. | No reference is required. It can be accessed directly without any reference to any class. |
It is useful in performing encapsulation properties of OOPs and event handling in AWT(GUI). | It is beneficial in performing the inheritance property of object-oriented programming (OOPs). |
It is used when the "has-a" relationship with its outer class is defined. | It is used when the "is-a" relationship is defined with its parent class. |
It contains methods as per the requirement. | It must include the methods which are present in the parent class. Also, it can include any other methods too as per the need. |
It is always present in the same file where the outer class is present. | It may or may not be available in the same file/package as its parent class. |
It cannot define any static methods inside it. | It contains all types of methods either static or non-static. |
Similar Reads
Inner Class in Java In Java, inner class refers to the class that is declared inside class or interface which were mainly introduced, to sum up, same logically relatable classes as Java is object-oriented so bringing it closer to the real world. Now geeks you must be wondering why they were introduced? There are certai
11 min read
Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read
Java Class vs Interfaces In Java, the difference between a class and an interface is syntactically similar; both contain methods and variables, but they are different in many aspects. The main difference is, A class defines the state of behaviour of objects.An interface defines the methods that a class must implement.Class
5 min read
Local Inner Class in Java Prerequisites: Nested Classes in Java Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop or an if clause. Local Inner classes are not a member of any enclosing classes. They belong to the block the
5 min read
How to Access Inner Classes in Java? In Java, inner class refers to the class that is declared inside class or interface which were mainly introduced, to sum up, same logically relatable classes as Java is purely object-oriented so bringing it closer to the real world. It is suggested to have adequate knowledge access the inner class,
3 min read