Write a Java Program to implement the concept of Encapsulation


What is Encapsulation?

Encapsulation simply means wrapping the data and code that works on the data together as a single unit. In java, data is nothing but variable and "code that works on data" is method. In encapsulation, the variables of given class will be hidden from any other classes, so that it can be accessed only through the methods of the current class. Therefore, it is also known as data hiding.

Encapsulation is one of the four fundamental OOPS Concept.

Now let's see how to implement encapsulation in Java.

class Emp { private int empid; private String name; public Emp(int x, String y) { empid = x; name = y; } public void show() { System.out.println("empid=" + empid); System.out.println("name=" + name); } } class Encap { public static void main(String arg[]) { int eid = Integer.parseInt(arg[0]); String ename = arg[1]; Emp e1 = new Emp(eid, ename); e1.show(); } }

Want to Clear Java Interview? Read Java Interview Notes prepared by Us

Are You Good enough in Java? Prove it!



Need Freelance Programming Writers, Contact us now at atechdailyweb@gmail.com

Having Programming Doubts or Facing any Issues?

Hurry Up! Exclusive Deals For You ends in

Quiz For You:
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Encapsulation implementation in Java

    Simple Java Program demonstrating Encapsulation