Write a Java Program to implement the concept of Encapsulation

[1989 views]




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(); } }
                 



Clear Any Java Interview by Reading our Ebook Once.


Can you clear Java Interview?



Comments










Search
Have Technical Doubts????


Hot Deals ends in













Technical Quiz:

Search Tags

    Encapsulation implementation in Java

    Simple Java Program demonstrating Encapsulation