Let us take a look at various other constructors in Java.
A copy constructor is a type of constructor in Java that creates an object by copying another object from the same Java class. It returns a copy of a class object that already exists.
If we want to, we can use the copy constructor:
public class Student
{
private double fpercent;
private String fname;
//constructor to initialize roll number and name of the student
Student(double fPercent, String fName)
{
fpercent = fPercent;
fname = fName;
}
//creating a copy constructor
Student(Student student)
{
System.out.println("\nAfter invoking the Copy Constructor:\n");
fpercent = student.fpercent;
fname = student.fname;
}
//creating a method that returns the price of the fruit
double showPercent()
{
return fpercent;
}
//creating a method that returns the name of the fruit
String showName()
{
return fname;
}
//class to create student object and print roll number and name of the student
public static void main(String args[])
{
Student s1 = new Student(91, "Rabecca Fatima");
System.out.println("Name of the first student: "+ s1.showName());
System.out.println("Percentage of the first student: "+ s1.showPercent());
//passing the parameters to the copy constructor
Student s2 = new Student(s1);
System.out.println("Name of the second student: "+ s2.showName());
System.out.println("Price of the second student: "+ s2.showPercent());
}
}
Name of the first student: Rabecca Fatima
Percentage of the first student: 91.0
After invoking the Copy Constructor:
Name of the second student: Rabecca Fatima
Price of the second student: 91.0
Java Constructor | Java Method |
---|---|
An object’s state is initialized using a constructor. | A method is used to expose an object’s behaviour. |
A return type cannot be used in a constructor. | A method must have a return type. |
The constructor is invoked implicitly. | The method is invoked explicitly. |
If a class lacks a constructor, the Java compiler provides a default constructor. | The compiler does not provide the method in any case. |
The constructor name must be the same as the class name. | The method name may or may not be the same as the class name. |
Constructor chaining is the process of calling a constructor from another constructor in the same class. It happens because of inheritance. When we create a derived class instance, all the constructors of the inherited class (base class) are called first, followed by the constructor of the calling class (derived class).
There are two ways to achieve constructor chaining:
public class ConstructorChain
{
//default constructor
ConstructorChain()
{
this("Coderzpy");
System.out.println("Default coA copy constructor is a type of constructor instructor called.");
}
//parameterized constructor
ConstructorChain(String str)
{
System.out.println("Parameterized constructor called");
}
//main method
public static void main(String args[])
{
//initializes the instance of example class
ConstructorChain cc = new ConstructorChain();
}
}
Parameterized constructor called
Default constructor called.
We created an instance of the class in the preceding example without passing any parameters. Because of this, it first calls the default constructor, which then redirects the call to the parameterized constructor. The parameterized constructor’s statements are executed before returning to the default constructor. The rest of the default constructor statements are then executed, and the object is successfully initialized.
Note: also read about the Constructor in Java
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…