Professional Documents
Culture Documents
Encapsulation and Constructor
Encapsulation and Constructor
Encapsulation and Constructor
A Java object is a combination of data and procedures working on the available data.
An object has a state and behavior.
The state of an object is stored in fields (variables), while methods (functions) display the
object's behavior.
Objects are created from templates known as classes.
In Java, an object is created using the keyword "new".
When a Java object is declared, a name is associated with that object. The object is instantiated
so that memory space can be allocated. Initialization is the process of assigning a proper initial
value to this allocated space. The properties of Java objects include:
One can only interact with the object through its methods. Hence, internal details are
hidden.
When coding, an existing object may be reused.
When a program's operation is hindered by a particular object, that object can be easily
removed and replaced.
A new object t from the class "Tree" is created using the following syntax:
System.out.println(t.c);
}
PROGRAM: Object creation done with different class.
int a=10;
int b=20;
int c =a+b;
PROGRAM:
System.out.println(c);
}
PROGRAM: Done with different class.
ENCAPSULATION
PROGRAM:
}
}
So, we go for concept is called “ENCAPSULATION”.
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance,
polymorphism, and abstraction.
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting
on the data (methods) together as a single unit.
In encapsulation, the variables of a class will be hidden from other classes, and can be
accessed only through the methods of their current class.
Provide public setter and getter methods to modify and view the variables values.
PROGRAM:
System.out.println(t.getApple());
System.out.println(t.getOrange());
System.out.println(t.getMango());
System.out.println(t1.getApple());
System.out.println(t1.getOrange());
System.out.println(t1.getMango());
System.out.println(t2.getApple());
System.out.println(t2.getOrange());
System.out.println(t2.getMango());
System.out.println(t3.getApple());
System.out.println(t3.getOrange());
System.out.println(t3.getMango());
System.out.println(t4.getApple());
System.out.println(t4.getOrange());
System.out.println(t4.getMango());
}
Array cons:
We must know in advance that how many elements are to be stored in array.
The memory which is allocated to array can not be increased or reduced.
Since array is of fixed size, if we allocate more memory than requirement then the
memory space will be wasted.
}
public class Branch {
a[0]=t;
a[1]=t1;
a[2]=t2;
a[3]=t3;
a[4]=t4;
System.out.println(a[i].getApple()+"\n"+a[i].getOrange()+"\n"+a[i].getMango());
}
}
}
PROGRAM: Using ArrayList and for loop
import java.util.ArrayList;
a.add(t);
a.add(t1);
a.add(t2);
a.add(t3);
a.add(t4);
for(int i=0;i<a.size();i++){
System.out.println(a.get(i).getApple()+"\n"+a.get(i).getOrange()+"\n"+a.get(i).getMango());
}
}
}
PROGRAM: Using ArrayList, toString and Enhanced for
@Override
public String toString() {
return getApple()+" "+getOrange()+" "+getMango();
}
import java.util.ArrayList;
a.add(t);
Tree t1 = new Tree();
t1.setApple("4");
t1.setOrange("5");
t1.setMango("6");
a.add(t1);
a.add(t2);
for(Tree s:a){
System.out.println(s);
}
}
CONSTRUCTOR
PROGRAM:
Fruit(){
System.out.println("apple is created without any parameter");
}
PARAMETERIZED CONSTRUCTOR:
PROGRAM:
int number;
String name;
f.show();
f1.show();
f2.show();
}
}
int number;
String name;
System.out.println(f);
System.out.println(f1);
System.out.println(f2);
}
}
@Override
public String toString() {
return getApple()+" "+getOrange()+" "+getMango();
}
import java.util.ArrayList;
for(Tree s:a){
System.out.println(s);
}
}