Encapsulation and Constructor

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

OBJECT IN JAVA:

 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".

HOW TO CREATE AN OBJECT:

There are three steps to creating a Java object:

 Declaration of the object.


 Instantiation of the object.
 Initialization of the object.

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:

Tree t = new Tree().

PROGRAM: Object creation done in same class.

public class Tree {


int a=10;
int b=20;
int c =a+b;

public static void main(String[] args) {

Tree t = new Tree();

System.out.println(t.c);

}
PROGRAM: Object creation done with different class.

public class Tree {

int a=10;
int b=20;
int c =a+b;

public class Branch {

public static void main(String[] args) {

Tree t = new Tree();


System.out.println("The value of a:"+t.a);
System.out.println("The value of b:"+t.b);
System.out.println("The addition value of:"+t.c);

WITHOUT OBJECT CREATION:

 Using static keyword, we don’t need to create an object for a class.


 We can directly call any variables or methods.

PROGRAM:

public class Tree {

static int a=10;


static int b=20;
static int c =a+b;

public static void main(String[] args) {

System.out.println(c);

}
PROGRAM: Done with different class.

public class Tree {

static int a=10;


static int b=20;
static int c = a+b;

public class Branch {

public static void main(String[] args) {

System.out.println("The value of a:"+Tree.a);


System.out.println("The value of b:"+Tree.b);
System.out.println("The addition value of:"+Tree.c);

ENCAPSULATION

VARIABLES DECLARED AS PUBLIC:

PROGRAM:

public class Tree {


public int a=10;
public int b=20;
public int c =a+b;

public class Branch {

public static void main(String[] args) {

Tree t = new Tree();


System.out.println("The value of a:"+t.a);
System.out.println("The value of b:"+t.b);
System.out.println("The addition value of:"+t.c);

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

 Therefore, it is also known as data hiding.

 Encapsulation is also called as Bean class.

To achieve encapsulation in Java:

 Declare the variables of a class as private.

 Provide public setter and getter methods to modify and view the variables values.

PROGRAM:

public class Tree {

private String apple;


private String orange;
private String mango ;

public String getApple() {


return apple;
}

public void setApple(String apple) {


this.apple = apple;
}

public String getOrange() {


return orange;
}

public void setOrange(String orange) {


this.orange = orange;
}

public String getMango() {


return mango;
}

public void setMango(String mango) {


this.mango = mango;
}

public class Branch {

public static void main(String[] args) {

Tree t = new Tree();


t.setApple("1");
t.setOrange("2");
t.setMango("3");

System.out.println(t.getApple());
System.out.println(t.getOrange());
System.out.println(t.getMango());

Tree t1 = new Tree();


t1.setApple("4");
t1.setOrange("5");
t1.setMango("6");

System.out.println(t1.getApple());
System.out.println(t1.getOrange());
System.out.println(t1.getMango());

Tree t2 = new Tree();


t2.setApple("7");
t2.setOrange("8");
t2.setMango("9");

System.out.println(t2.getApple());
System.out.println(t2.getOrange());
System.out.println(t2.getMango());

Tree t3 = new Tree();


t3.setApple("10");
t3.setOrange("11");
t3.setMango("12");

System.out.println(t3.getApple());
System.out.println(t3.getOrange());
System.out.println(t3.getMango());

Tree t4 = new Tree();


t4.setApple("13");
t4.setOrange("14");
t4.setMango("15");

System.out.println(t4.getApple());
System.out.println(t4.getOrange());
System.out.println(t4.getMango());
}

PROGRAM: Using Array and for loop

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 Tree {

private String apple;


private String orange;
private String mango ;

public String getApple() {


return apple;
}

public void setApple(String apple) {


this.apple = apple;
}

public String getOrange() {


return orange;
}

public void setOrange(String orange) {


this.orange = orange;
}

public String getMango() {


return mango;
}

public void setMango(String mango) {


this.mango = mango;
}

}
public class Branch {

public static void main(String[] args) {

Tree a[]= new Tree[5];

Tree t = new Tree();


t.setApple("1");
t.setOrange("2");
t.setMango("3");

a[0]=t;

Tree t1 = new Tree();


t1.setApple("4");
t1.setOrange("5");
t1.setMango("6");

a[1]=t1;

Tree t2 = new Tree();


t2.setApple("7");
t2.setOrange("8");
t2.setMango("9");

a[2]=t2;

Tree t3 = new Tree();


t3.setApple("10");
t3.setOrange("11");
t3.setMango("12");

a[3]=t3;

Tree t4 = new Tree();


t4.setApple("13");
t4.setOrange("14");
t4.setMango("15");

a[4]=t4;

for(int i=0; i<a.length;i++){

System.out.println(a[i].getApple()+"\n"+a[i].getOrange()+"\n"+a[i].getMango());

}
}

}
PROGRAM: Using ArrayList and for loop

Arraylist over Array:

 You can define ArrayList as re-sizable array.


 Size of the ArrayList is not fixed.
 ArrayList can grow and shrink dynamically

public class Tree {

private String apple;


private String orange;
private String mango ;

public String getApple() {


return apple;
}

public void setApple(String apple) {


this.apple = apple;
}

public String getOrange() {


return orange;
}

public void setOrange(String orange) {


this.orange = orange;
}

public String getMango() {


return mango;
}

public void setMango(String mango) {


this.mango = mango;
}

import java.util.ArrayList;

public class Branch {

public static void main(String[] args) {

ArrayList<Tree> a = new ArrayList<>();

Tree t = new Tree();


t.setApple("1");
t.setOrange("2");
t.setMango("3");

a.add(t);

Tree t1 = new Tree();


t1.setApple("4");
t1.setOrange("5");
t1.setMango("6");

a.add(t1);

Tree t2 = new Tree();


t2.setApple("7");
t2.setOrange("8");
t2.setMango("9");

a.add(t2);

Tree t3 = new Tree();


t3.setApple("10");
t3.setOrange("11");
t3.setMango("12");

a.add(t3);

Tree t4 = new Tree();


t4.setApple("13");
t4.setOrange("14");
t4.setMango("15");

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

public class Tree {

private String apple;


private String orange;
private String mango ;

public String getApple() {


return apple;
}

public void setApple(String apple) {


this.apple = apple;
}

public String getOrange() {


return orange;
}

public void setOrange(String orange) {


this.orange = orange;
}

public String getMango() {


return mango;
}

public void setMango(String mango) {


this.mango = mango;
}

@Override
public String toString() {
return getApple()+" "+getOrange()+" "+getMango();
}

import java.util.ArrayList;

public class Branch {

public static void main(String[] args) {

ArrayList<Tree> a = new ArrayList<>();

Tree t = new Tree();


t.setApple("1");
t.setOrange("2");
t.setMango("3");

a.add(t);
Tree t1 = new Tree();
t1.setApple("4");
t1.setOrange("5");
t1.setMango("6");

a.add(t1);

Tree t2 = new Tree();


t2.setApple("7");
t2.setOrange("8");
t2.setMango("9");

a.add(t2);

for(Tree s:a){

System.out.println(s);

}
}

CONSTRUCTOR

 Constructor is a block of codes similar to the method.


 It is called when an instance of the object is created, and memory is allocated for the
object.
 It is a special type of method which is used to initialize the object.
 it constructs the values at the time of object creation.

There are two rules defined for the constructor.

 Constructor name must be the same as its class name


 A Constructor must have no explicit return type
 A Java constructor cannot be abstract, static, final, and synchronized

There are two types of constructor:

 Default constructor (no-arg constructor)


 Parameterized constructor
DEFAULT CONSTRUCTOR:

 A constructor is called "Default Constructor" when it doesn't have any parameter.


 Every time an object is created using new() keyword, at least one constructor is called.
 It is not necessary to write a constructor for a class. It is because java compiler creates
a default constructor if your class doesn't have any.

PROGRAM:

public class Fruit {

Fruit(){
System.out.println("apple is created without any parameter");
}

public static void main(String[] args) {

Fruit f = new Fruit();


}
}

PARAMETERIZED CONSTRUCTOR:

 A constructor which has a specific number of parameters is called a parameterized


constructor.
 The parameterized constructor is used to provide different values to the distinct objects.
 However, you can provide the same values also.

PROGRAM:

public class Fruit {

int number;
String name;

Fruit(int a, String b){


this.number=a;
this.name=b;
}

public void show(){


System.out.println(number+" "+name);
}

public static void main(String[] args) {

Fruit f = new Fruit(1,"apple");


Fruit f1 = new Fruit(2,"orange");
Fruit f2 = new Fruit(3,"mango");

f.show();
f1.show();
f2.show();
}
}

PROGRAM: using toString

public class Fruit {

int number;
String name;

Fruit(int a, String b){


this.number=a;
this.name=b;
}
@Override
public String toString() {
return number+" "+name;
}

public static void main(String[] args) {

Fruit f = new Fruit(1,"apple");


Fruit f1 = new Fruit(2,"orange");
Fruit f2 = new Fruit(3,"mango");

System.out.println(f);
System.out.println(f1);
System.out.println(f2);
}
}

PROGRAM: Using bean class, ArrayList, toString and Enhanced for

public class Tree {

private String apple;


private String orange;
private String mango ;

Tree(String apple, String orange, String mango){


this.apple=apple;
this.orange=orange;
this.mango=mango;
}
public String getApple() {
return apple;
}

public void setApple(String apple) {


this.apple = apple;
}

public String getOrange() {


return orange;
}

public void setOrange(String orange) {


this.orange = orange;
}

public String getMango() {


return mango;
}

public void setMango(String mango) {


this.mango = mango;
}

@Override
public String toString() {
return getApple()+" "+getOrange()+" "+getMango();
}

import java.util.ArrayList;

public class Branch {

public static void main(String[] args) {

ArrayList<Tree> a = new ArrayList<>();

Tree t = new Tree("apple1","orange1","mango1");


Tree t1 = new Tree("apple2","orange2","mango2");
Tree t2 = new Tree("apple3","orange3","orange3");
a.add(t);
a.add(t1);
a.add(t2);

for(Tree s:a){

System.out.println(s);

}
}

You might also like