Step 16:Oops- 4th Concept:Encapsulation: 11th hour + code

Definition-

Encapsulation_in_Java[1]

Encapsulation is a concept of binding data and code together in single unit.

-In java, data means data members, and code means methods.

– In java, in encapsulation, data members are private and access methods (setters and getters) are public.

1- Creation of encapsulation class-


package ncapsulsn;

public class ExEncapsulated {

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

2- Usege of encapsulation class-

package ncapsulsn;

public class UseEncapsulated {

public static void main(String[] args) {
ExEncapsulated e1=new ExEncapsulated();
e1.setName("xyz");
e1.setAge(5);
ExEncapsulated e2=new ExEncapsulated();
e2.setAge(17);
System.out.println(e2.getAge());
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.