Showing posts with label setter method. Show all posts
Showing posts with label setter method. Show all posts

Thursday, 6 February 2014

Object Oriented Feature : Encapsulation

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.,



Instance Variables                                                 Instance Methods                                                 Home

Instance variables

Instance variables are those which are associated with object. Person object personA has instance variables height, weight, color, country.

Instance variables are also called member variables or fields.

Fields declaration syntax
modifier dataType variableName;

Field declaration composed of three components
     1. modifier is any one of public, private, protected, default.
    2. dataType is any primitive or reference type
    3. name of the variable
Example:
private int var;
    we will discuss about the access modifiers private and public in this section.

Access Modifiers (public, private)
private : the field is accessible only within its own class.
Example consider the class Person. I given private access to instance variables height, weight, color and country. So any method which is defined inside the class Person can able to access these variables, but you can't access outside the class.
class Person{
 private float height, weight;
 private String color, country;

 float getHeight(){
  return height;
 }

 float getWeight(){
  return weight;
 }

 String getColor(){
  return color;
 }

 String getCountry(){
  return country;
 }
}
What happen if I tried to access the private variables outside of my class ?
Good Question, You will get compile time error.

Trying to access the private variables from other class PersonTest.
class PersonTest{
 public static void main(String args[]){
  Person personA = new Person();
  personA.height = 6;
 }
}

When you try to compile the above code, you will get the compiler error like below
 PersonTest.java:6: error: height has private access in Person
           personA.height = 6;
           ^
           1 error
    

Then how to solve this problem
Update the Person class by providing setter methods, to set the private properties.
class Person{
 private float height, weight;
 private String color, country;

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

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

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

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

 float getHeight(){
  return height;
 }

 float getWeight(){
  return weight;
 }

 String getColor(){
  return color;
 }

 String getCountry(){
  return country;
 }
}
            
As you see in the above class Person, it is updated by adding 4 setter methods like setHeight, setWeight, setColor, setCountry to set the private properties height, weight, color, country respectively.

Update the PersonTest class also like below
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
  

public modifier
    the field is accessible from all classes.

Some points to remember

1. If you don't initialize an instance variable, by default it is initialized to default value.
Data Type Default Value
int 0
float 0
String Null
boolean FALSE
Reference type null

Example
class DefaultValue{
 byte byteVar;
 short shortVar;
 int intVar;
 long longVar;
 char charVar;
 float floatVar;
 double doubleVar;
 boolean boolVar;
 String stringVar;

 public static void main(String args[]){
  DefaultValue obj = new DefaultValue();
  
  System.out.println("byte variable is set to " + obj.byteVar);
  System.out.println("short variable is set to " + obj.shortVar);
  System.out.println("int variable is set to " + obj.intVar);
  System.out.println("long variable is set to " + obj.longVar);
  System.out.println("char variable is set to " + obj.charVar);
  System.out.println("float variable is set to " + obj.floatVar);
  System.out.println("double variable is set to " + obj.doubleVar);
  System.out.println("bool variable is set to " + obj.boolVar);
  System.out.println("string variable is set to " + obj.stringVar);
 }
}
     
Output
byte variable is set to 0
short variable is set to 0
int variable is set to 0
long variable is set to 0
char variable is set to
float variable is set to 0.0
double variable is set to 0.0
bool variable is set to false
string variable is set to null
     

Object Creation                                                 Encapsulation                                                 Home