Java Homework Help | Help with Java Programming | Programming Homework Help | Java Programming Assignment Help | Help with Java Programming Assignment | Java Tutor | Programming Help | Java Online Help | Java Programming Online | Java Programming Projects

In object oriented programming , encapsulation is very important concept. Java programming is also high level object orinted programming language so encapsulation concept plays vital role in solving real time problems. We help with Java Programming assignment for srudents of java to understand the concept of encapsulation and its implimentation . Following is description with example :-

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If you make the fields as private, then outside of the class you can't access those variables. So Encapsulation is also called data hiding.

Example
class Person{
private float height, weight;
private String color, country;

public void setHeight(float h){
height = h;
}

public void setweight(float w){
weight = w;
}

public void setColor(String c){
color = c;
}

public void setCountry(String o){
country = o;
}

float getHeight(){
return height;
}

float getWeight(){
return weight;
}

String getColor(){
return color;
}

String getCountry(){
return country;
}
}

class PersonTest{
public static void main(String args[]){
Person personA = new Person();
Person personB = new Person();

/* Setting properties for personA object */
personA.setHeight(6);
personA.setweight(75);
personA.setColor("White");
personA.setCountry("India");

/* Setting properties for personB object */
personB.setHeight(5.5f);
personB.setweight(76);
personB.setColor("Black");
personB.setCountry("America");

/* Calling the behaviors and printing the details of personA */
System.out.println("PersonA Height is " + personA.getHeight());
System.out.println("PersonA Weight is " + personA.getWeight());
System.out.println("PersonA color is " + personA.getColor());
System.out.println("PersonA country is " + personA.getCountry());
System.out.println();

/* Calling the behaviors and printing the details of personB */
System.out.println("PersonB Height is " + personB.getHeight());
System.out.println("PersonB Weight is " + personB.getWeight());
System.out.println("PersonB color is " + personB.getColor());
System.out.println("PersonB country is " + personB.getCountry());
}
}

Output
PersonA Height is 6.0
PersonA Weight is 75.0
PersonA color is White
PersonA country is India

PersonB Height is 5.5
PersonB Weight is 76.0
PersonB color is Black
PersonB country is America

Some points to Remember

1. What are the uses of getter and setter methods ?
a. To Provide Encapsulation
b. By using setter methods we can restrict the value as we need.

Ex: There is a cat class, and height variable inside it.

without setter method
Cat c1 = new Cat();
c1.height = 0;

we can set the variable height to zero, which is unacceptable.

with setter method, we can avoid above kind of problems
class Cat{
int height;

void setHeight(int h){
if(h <= 0 ){
System.out.println(“Wrong Height, setting to default height”);
height = 2;
}
height = h;
}
}

With the above setHeight(), if you tries to set height to zero like

Cat c1 = new Cat();
c1.setHeight(0);

You will get a warning message saying the height you are trying to set is wrong, and it is set to default value.

Most instance variable values are coded with certain assumptions about the boundaries of the values. Like, think of all the things that would break if negative numbers were allowed. Amount of memory needed to run program(Definitely not a negative number), Employee minimum salary etc.,

Instance methods are associated with objects. Instance methods are used to provide behavior to an object. Objects usually communicate with their data using instance methods.

Syntax to access an instance method
objectName.methodName(arguments);

Our Team

We have 2500+ expert pool from all over the world. They are highly qualified and well experienced. All experts have experinced in teaching and good command over programming topics. Some of our experts are as follows:


John Doe

JAVA EXPERT(10+ Yrs Exp.)

Larry Doe

C EXPERT(12+ Yrs Exp.)

Ranith Kays

C++ EXPERT(8+ Yrs Exp.)

Joan Ray

OPEN SOURCE LANGUAGE EXPERT(8+ Yrs Exp.)