Tuesday 7 March 2023

More About Inner Class in Java

Here's some additional information about inner classes in Java:

  1. Access modifiers: Inner classes can have the same access modifiers as any other member of the enclosing class. For example, you can make an inner class private, protected, or public.

  2. Inheritance: Inner classes can extend a class or implement an interface, just like any other class.

  3. Anonymous inner classes: As mentioned earlier, anonymous inner classes are typically used for creating a single object of an interface or an abstract class. Here's an example:

csharp
interface Greeting { public void greet(); } public class OuterClass { public void sayHello() { Greeting greeting = new Greeting() { public void greet() { System.out.println("Hello!"); } }; greeting.greet(); } }

In this example, an anonymous inner class is used to implement the Greeting interface. An object of this class is then assigned to the greeting variable and used to call the greet() method.

  1. Inner classes and static members: A non-static inner class can access both static and non-static members of the enclosing class. However, a static inner class can only access static members of the enclosing class.

  2. Local inner classes and closures: Local inner classes are often used for implementing closures in Java. A closure is a function that remembers the values of all the variables that were in scope when the function was created. Here's an example:

csharp
public class OuterClass { public void createClosure() { int x = 10; class LocalInnerClass { public void printX() { System.out.println(x); } } LocalInnerClass closure = new LocalInnerClass(); closure.printX(); // prints 10 } }

In this example, a local inner class is used to create a closure that remembers the value of the x variable. The printX() method can access the x variable even though it is defined outside of the class.


  1. . Here's an example:
kotlin
public class OuterClass<T> { private T value; public class InnerClass<S> { private S data; public InnerClass(S data) { this.data = data; } public T getValue() { return value; } public S getData() { return data; } } public void setValue(T value) { this.value = value; } }

In this example, both OuterClass and InnerClass are generic classes. InnerClass has its own type parameter S, in addition to the type parameter T of the enclosing class.

  1. Anonymous inner classes and lambdas: In Java 8 and later versions, anonymous inner classes can be replaced with lambda expressions. Here's an example:
csharp
interface Greeting { public void greet(); } public class OuterClass { public void sayHello() { Greeting greeting = () -> System.out.println("Hello!"); greeting.greet(); } }

In this example, the anonymous inner class that implements the Greeting interface is replaced with a lambda expression.

  1. Shadowing: If an inner class defines a member with the same name as a member of the enclosing class, the inner class member shadows the enclosing class member. Here's an example:
csharp
public class OuterClass { private int x = 10; public class InnerClass { private int x = 20; public void printX() { System.out.println(x); // prints 20 System.out.println(OuterClass.this.x); // prints 10 } } }

In this example, the InnerClass defines a member variable x that shadows the x variable of the enclosing class. To access the x variable of the enclosing class, you can use the syntax OuterClass.this.x.

No comments:

Post a Comment

Cryptography API

Java offers a comprehensive set of cryptographic functionalities through its `java.security` package. This package provides various classes ...