Write a Java Program to implement the concept of Encapsulation

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




Comments



Search

For any inquiry, Contact us at atechdailyweb@gmail.com




Facing Programming Errors. Get Answers within 24 hours:


Recommendation Based on your Interest




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