Inner Class And Anonymous Inner Class that Implements Runnable in Java


In Java, the Inner class and Anonymous inner class are two types of Nested classes. Here, nested class means a class within another class. The inner class is a nested class defined without the static keyword, i.e., the inner class is a non-static nested class. The type of nested inner class that has no name is called as anonymous class.

The Runnable Interface is a way of creating threads in Java multithreading. Java provides the functionality of multithreading to perform multiple operations simultaneously. In it, the operation gets divided into multiple smaller parts called a thread. Let's explore how to use Inner class and Anonymous inner class that implements Runnable.

Inner Class and Anonymous Inner Class that Implements Runnable

In this section, we will learn the Inner class and Anonymous inner class that implements Runnable Interface.

Java Program to Show Inner Class that Implements Runnable

Let's start with introducing the Inner class.

Inner class

Non-static or Inner class can access all the static and non-static methods and member variables of its outer class. It can also access the members that are private but, an outer class cannot access the private members of inner class. Inner class is the most used type of nested class.

Syntax

class Class_otr {
   // Outer class body
   class Class_inn { 
	  // Inner class body
   }
}

To create class we have used 'class' keyword. 'Class_otr' is the name of outer class and 'Class_inn' is the name of inner class.

Example 1

The following example demonstrates the practical implementation of Inner class that implements the Runnable Interface.

Approach

  • First, define a outer class and inside it, define its inner class that implements Runnable interface.

  • Inside this inner class, create its constructor and override the 'run()' method.

  • Moving further, define another method and within this method, create two instances of inner class. Using these instances, create two threads to perform the operation. Then, use the built-in method 'start()' to start the process of threads.

  • In the main() method, create an instance of Outer class and call the 'executeThread()' method to start the operation.

class OtrClass {
   // Creating an Inner class that implements Runnable
   class InrClass implements Runnable {
      private String greet;
      // Constructor
      public InrClass(String greet) {
         this.greet = greet;
      }
      // Overriding the run() method
      public void run() {
         System.out.println(greet);
      }
   }
   // defining method to execute threads using inner classes
   public void executeThread() {
      // instances of the inner class
      InrClass ic1 = new InrClass("Hello and Welcome!!");
      InrClass ic2 = new InrClass("Tutorials Point");
      // Creating threads with instances of inner class 
      Thread thr1 = new Thread(ic1);
      Thread thr2 = new Thread(ic2);
      // Start the threads
      thr1.start();
      thr2.start();
   }
}
public class RunableExp1 {
   public static void main(String[] args) {
      OtrClass oc = new OtrClass(); // instance of Outer class
      oc. executeThread(); // calling method to start threads
   }
}

Output

Hello and Welcome!!
Tutorials Point

Java Program to Show Anonymous Inner Class that Implements Runnable

Before jumping into the program, let's discuss the Anonymous inner class.

Anonymous Inner Class

As discussed earlier, it is the type of inner class that has no name. Its declaration and initialization happen at the same time. It is mainly used for overriding methods.

We can use an abstract keyword to declare the anonymous class.

Syntax

Class_name object_name = new Class_name() {
    return_type method_name() {
	// code to be executed
    }
};

Example 2

The following example illustrates the use of Anonymous Inner class that implements Runnable Interface.

Approach

  • Create an abstract class that implements Runnable Interface.

  • Then, inside the main() method create an anonymous inner class that overrides the previous abstract class.

  • Inside this anonymous inner class, override the built-in method 'run()'.

  • Create a thread and start it using 'start()' method.

// abstract class that implements Runnable
abstract class Anonymous implements Runnable { } 
public class RunableExp2 {
   public static void main(String args[]) {
      // creating anonymous inner class
      Anonymous thr = new Anonymous() { 
         // overriding run() method
         public void run() { 
            System.out.println("Hello! from anonymous inner class");
         }
      };
      Thread thr1 = new Thread(thr); // defining a thread
      thr1.start(); // starting the thread	
   }
}

Output

Hello! from anonymous inner class

Conclusion

We started this article by defining Inner class and Anonymous inner class and in the next section, we discussed them in detail. Also, we discovered the Runnable Interface that is used to perform multithreading operations in Java. We have seen two different examples of Inner class and Anonymous inner class that implements Runnable Interface.

Updated on: 20-Jul-2023

259 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements