MainMenu

Home Core Java Overview Maven Tutorials

Sunday, 15 January 2017

Encapsulation in Java with example

Encapsulation in Java with example:


For Video : CLICK HERE



Encapsulation is also known as Data hiding.
If a class have private member & it is required to access these private member from outside of class then we have to create public getter & setter method.


This way data can only be accessed by public methods thus making the private fields and their implementation hidden for outside classes.

Encapsulation Example


class chandan
{
private string Name;
private Int Age;
public string getName()
{
return Name;
}
public void setName(String Name)
{
this.Name = Name;
}
public int getAge()
{
return Age;
}
public void setAge(int Age)
{
this.Age = Age;
}
}
public class Chandan1
{
public static void main(String args[])
{
chandan obj = new chandan();
obj.setName("Chandan");
obj.setAge(25);
System.out.println("Your name is :" +obj.getName);
System.out.println("Your Age is :" +obj.getAge);
}
}
Output :
Your name is :Chandan
Your Age is :25

Tags :What is Encapsulation?,Encapsulation Example in Java

No comments:

Post a Comment