What is inner class and sub class in Java?

Paula 142 Published: 09/24/2024

What is inner class and sub class in Java?

I'd be happy to explain the concept of inner classes and subclasses in Java!

Subclass

In Java, a subclass (also known as a derived class) is a class that inherits properties and behavior from another class, called the superclass or parent class. The subclass inherits all the fields and methods of the superclass and can also add new fields and methods or override the ones inherited from the superclass.

A subclass is denoted by using the keyword extends followed by the name of its superclass. For example:

public class Animal {

public void sound() {

System.out.println("The animal makes a sound.");

}

}

public class Dog extends Animal {

public void sound() {

System.out.println("The dog barks.");

}

}

In this example, Dog is a subclass of Animal. The Dog class inherits the sound() method from Animal and overrides it with its own implementation.

Inner Class

An inner class (also known as a nested class) is a class that is defined inside another class. An inner class has access to all the members of its outer class, including private variables and methods. This provides a way to organize code and keep related classes together.

There are two types of inner classes in Java:

Static Inner Class: A static inner class is not bound to an instance of its outer class. It can be instantiated independently without referencing an instance of the outer class. Non-Static Inner Class (or Inner Class with a Reference to the Outer Instance): A non-static inner class is bound to an instance of its outer class and cannot be instantiated independently.

Here's an example of a non-static inner class:

public class OuterClass {

private int x;

public class InnerClass {

public void printX() {

System.out.println(x); // Accesses the private variable x from the outer class

}

}

}

In this example, InnerClass is a non-static inner class that has access to the private variable x of its outer class OuterClass.

Key differences between Subclass and Inner Class

A subclass inherits properties and behavior from another class (superclass), whereas an inner class is defined inside another class. A subclass can be instantiated independently, whereas a non-static inner class requires an instance of the outer class to be instantiated.

In summary, subclasses allow you to create new classes that inherit properties and behavior from existing classes, while inner classes provide a way to organize code by defining classes within other classes. Both concepts are essential in object-oriented programming and are widely used in Java development.

What are the different types of inner classes in Java?

I'm happy to respond in English!

In Java, inner classes (also known as nested classes) are classes defined inside another class. They are essentially "friends" of the enclosing class and can access its members directly. Inner classes have several types based on their access modifiers:

Static Inner Classes: These inner classes do not have a reference to the outer class instance. They can be accessed only through the outer class. This means that they cannot access any non-static member variables or methods of the enclosing class.

Example:

public class Outer {

public static void main(String[] args) {

new InnerClass().doSomething();

}

private static class InnerClass {

public void doSomething() {

System.out.println("Doing something...");

}

}

}

Non-Static Inner Classes: These inner classes have a reference to the outer class instance. They can access both static and non-static member variables and methods of the enclosing class.

Example:

public class Outer {

public void doSomething() {

System.out.println("Outer is doing something...");

}

private class InnerClass {

public void doSomethingElse() {

outer.doSomething(); // accessing the outer class instance

}

}

}

Local Inner Classes: These inner classes are defined within a method and can access the local variables and methods of that method.

Example:

public class Outer {

public void doSomething(int x) {

int y = 10;

class LocalInnerClass {

public void doSomethingElse() {

System.out.println(x + ", " + y);

}

}

new LocalInnerClass().doSomethingElse();

}

}

Anonymous Inner Classes: These inner classes are defined without a name, often used for implementing interfaces or creating threads.

Example:

public class Outer {

public void doSomething() {

Runnable r = new Runnable() {

public void run() {

System.out.println("Running...");

}

};

Thread t = new Thread(r);

t.start();

}

}

In summary, inner classes in Java are classified into static inner classes, non-static inner classes, local inner classes, and anonymous inner classes based on their access modifiers and usage. Each type has its unique features and applications.

Please let me know if you'd like me to elaborate further or address any specific questions!