09.04. Inner Classes

Inner Classes

  • Eclipse: Oxygen
  • Java: 1.8

Here in Java Inner classes is the concept where we want to add more encapsulation to the given code. When we declare a class inside a class then the inside class can access all the private members in addition to the inner class can be hidden from the outside world.

Also, it is logical to group two classes that are related together.

Types of inner class in java.

  1. Nested Inner class
  2. Method Local Inner class
  3. Anonymous Inner class
  4. Static Inner class

In this tutorial, we will discuss non-static inner classes.

In the following example, the lock is an inner class. Within the lock class, we have initialized the Boolean variable. Then we create a constructor method. Within the method, we create the object of the lock class, and then invoke the getLock() method. setLock() in the reference variable like shopStatus().

Output

Contributed by: Poonam Tomar

 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments