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, you can't access those
variables from outside of the class.
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;
return; } 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.,
No comments:
Post a Comment