Java - Inner Class vs Sub Class Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 ClassMethod Local Inner ClassAnonymous 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. Comment More infoAdvertise with us A aryan183choudhary Follow Improve Article Tags : Java Difference Between Practice Tags : Java 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 Anonymous Inner Class in Java Nested Classes in Java is prerequisite required before adhering forward to grasp about anonymous Inner class. It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain "extras" such as o 7 min read Classes and Objects in Java In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh 11 min read Types of Classes in Java A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-orien 8 min read Nested Classes in Java In Java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation and creates more readable and maintainable code. The scope of a nested cl 5 min read Static class in Java Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class 3 min read Like