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 which 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

Shop is closed, please visit later.
Welcome, we are open.

Contributed by: Poonam Tomar

 


VIEW COURSES

Buy me a coffee!

 

PayTM QR (INDIA)

  • Get video tutorials
  • Project files set
  • Get┬áCertified

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu