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

4 comments:

  1. I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective. Techno-based information has been fetched in each of your topics. Sure it will enhance and fill the queries of the public needs. Feeling so glad about your article. Thanks…!
    selenium training in chennai
    selenium online courses best selenium online training
    selenium testing training
    selenium classes

    ReplyDelete