OOPS 2 - Encapsulation (access modifiers and constructors)
OOPS
Access Modifiers:
Define the level of protectiveness.
Please use below link to download access modifier package for practice.
https://github.com/PuneetKumarSinghIT/java.git
Constructors:
The first method which will trigger whenever you are creating an object of an class. It will define memory for your class object as class is a blueprint and object is real look of that blueprint which live.
Java will cancel default constructor once you create one of your own parameters list.
Responsibility of constructors:
- Create object of a class.
- assign default values to attributes of a class.
example:
int x =0; -> primitive datatype
Integer x = null; -> class
So constructor responsibility of constructor is to assign default values if datatype is promitive and null if datatype is derived or wrapper class object.
Syntax to create a constructor:
class puneet{
public puneet()
{
}
}
}
You can have paremeters or not for your constructor, calling your constructor will be done at the time of object creation.
puneet p = new puneet();
If you have created a constructor with parameters then you need to provide parameters at the time of creating an object. So if you want you can have multiple version of constructors. In a class you can have multiple constructors. Classic example of polymorphism.
Copy Constructors:
- Wrong solution: As it is not copying any value from one object to another it is just refering to old object.
Student s1 = new Student(o,"puneet");
Student s2 = s1;
It refer to memory location of s1 and will delete it whenever you will delete s2 variable. As s2 variable is referening/pointing to s1 student object.
- Create an object and copy data from first object to another manually. It is not scalable. Deep copy sample.
Student s2 = new Student();
s2.rollNo=s1.rollNo;
s2.name=s1.name;
- copy constructor creation - it is shallow copy method.
Student
{
public Student(int rollNo, String name)
public Student(int rollNo, String name)
{
this.rollNo=rollNo;
this.name=name;
}
public Student(Student oldStudent)
{
this.rollNo=oldStudent.rollNo;
this.name=oldStudent.name;
}
}
Student s1 = new Student(1,"puneet");
Student s2 = new Student(s1);
Advantage of copy constructor:
- You can access private attributes. Can use any access modifier.
How copy work in Java?
Object - Heap Memory
Primitive variable - Stack Memory
Copy constructor working in Java?
Two types of copy in Java:
- Deep copy - Where two or more objects will not share same memory location for attributes.
- Shallow copy - New object is created but attributes of new object will still refer to old memory address. It means both objects share the data and they are sharing the data so they are not independent.
Reason:
As you need to have new memory location for deep copy and as you use nesting in class and object world hence it become very hard.
Is Java pass by value or pass by reference?
Pass by value - only value is shared. - primitive variables follow it.
Pass by reference - memory is shared. - non-primitive variables follow it.
Java is passed by value but the value that is passed is address.





Comments
Post a Comment