Constructor
Constructors are used to initialize the object. It has the same name as the class and returns void. When a new keyword is called, it invokes the constructor and the run time creates the object and returns the reference. If the class is inherited from another class, the superclass constructor is called before the current class constructor. We can explicitly call the superclass constructor by calling super().
- Copy constructors are special constructors where a new object is created by copying values from the existing one.
class HelloWorld extends First {
String name = "";
HelloWorld() {
System.out.println("HelloWorld Called");
}
HelloWorld(String name) {
super(name);
this.name = name;
System.out.println("HelloWorld Called "+ name);
}
HelloWorld(HelloWorld obj) {
this.name = obj.name;
System.out.println("HelloWorld obj Called ");
}
public static void main(String[] args) {
System.out.println("Try programiz.pro");
HelloWorld hw = new HelloWorld("Sandhya");
HelloWorld w = new HelloWorld(hw);
hw.name = "Sid";
System.out.println("hw " + hw.name);
System.out.println("w " + w.name);
}
}
class First {
First() {
System.out.println("First Called");
}
First(String name) {
System.out.println("First Called "+ name);
}
}
- Default copy constructor copies a shallow copy of an existing object meaning it copies the reference field rather than creating a new object.
class HelloWorld extends First {
String name = "";
HelloWorld() {
System.out.println("HelloWorld Called");
}
HelloWorld(String name) {
super(name);
this.name = name;
System.out.println("HelloWorld Called "+ name);
}
public static void main(String[] args) {
System.out.println("Try programiz.pro");
HelloWorld hw = new HelloWorld("Sandhya");
HelloWorld h = hw; // shallow copy
hw.name = "Karthik";
System.out.println("hw " + hw.name);
System.out.println("h " + h.name);
}
}
class First {
First() {
System.out.println("First Called");
}
First(String name) {
System.out.println("First Called "+ name);
}
}
- Cloning: We can duplicate objects in java without using a copy constructor by implementing Cloneable Interface and overriding clone() method
- Shallow Copy
class Test {
int x;
}
public class CloneSample implements Cloneable{
int age;
Test test = new Test();
@Override
public Object clone() throws CloneNotSupportedException {
System.out.println("Inside Clone Method");
return super.clone();
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public static void main(String[] args) throws CloneNotSupportedException {
CloneSample cs = new CloneSample();
cs.setAge(20);
cs.test.x = 30;
CloneSample cs2 = (CloneSample) cs.clone();
System.out.println(cs2.getAge());
System.out.println(cs2.test.x);
cs.setAge(10);
cs.test.x = 50;
System.out.println(cs.getAge());
System.out.println(cs.test.x);
System.out.println(cs2.getAge());
System.out.println(cs2.test.x);
}
}
2. Deep Copy
class Test2 {
int x;
}
public class CloneSample2 implements Cloneable{
int age;
Test2 test = new Test2();
@Override
public Object clone() throws CloneNotSupportedException {
System.out.println("Inside Clone Method");
CloneSample2 cs = (CloneSample2) super.clone();
cs.test = new Test2();
return cs;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public static void main(String[] args) throws CloneNotSupportedException {
CloneSample2 cs = new CloneSample2();
cs.setAge(20);
cs.test.x = 30;
CloneSample2 cs2 = (CloneSample2) cs.clone();
//System.out.println(cs2.getAge());
System.out.println(cs.test.x);
// cs.setAge(10);
cs.test.x = 50;
// System.out.println(cs.getAge());
System.out.println(cs.test.x);
// System.out.println(cs2.getAge());
System.out.println(cs2.test.x);
}
}
Constructor declarations may have private (access to class), protected (access to same package and all subclasses), default (access to same package) or public (access to world) access modifier. Unlike methods, a constructor can’t be abstract, static, final, native, or synchronized:
- It isn’t necessary to declare a constructor final because they are not class members and they do not inherit.
- The abstraction is unnecessary because we must implement the constructors.
- A static constructor is not required since each constructor is called with an object.
- An object under construction should not be synchronized since it would lock the object while it is being constructed, which is not normally made available to other threads until all the constructors have completed their work.
- There are no native constructors in Java because this is a language design decision that is meant to make sure that superclass constructors are always invoked during object creation.
Comments
Post a Comment