In Java you can call one constructor from another and it’s known as
constructor chaining in Java. Don’t confuse between constructor
overloading and constructor chaining, former is just a way to declare more
than one constructor
in Java. this and super keyword is
used to call one constructor from other in Java. this() can be
used to call other constructor of same class while super() can be
used to call constructor from super class in Java. Just keep in mind that this() in realty
calls no argument constructor of same class, while this(2) calls
another constructor of same class which accepts one integer parameter.
Similarly super() can be used to call no argument
constructor of super class and super with parameter can be used to call other overloaded
constructor of parent class. Calling one constructor from other is called constructor chaining in Java, which we
seen while discussing constructor overloading in Java. Constructor chaining is
also used to implement telescoping
pattern where an object can be created with combination of multiple
property. In our last tutorial we have seen some important properties of Java
constructor as well as answered question What
is Constructor in Java and in this Java tutorial we will see example of how to call one constructor from other for
same class and super class.
How to call overloaded constructor in Java
![What is Constructor chaining in Java with example](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTVE8h1N8NJBr01MjW6J9XzkvHCt4f1MkEHZBW5oNozU7yfpwAIwOAbCtubl7GO7mWR8UXeU3BtXBg8fu73oO5OMzzRQ1i4UWhe_mnfGN70c-TKljVbSSIJLZW1C1Q3a6bpHSE8NKDJMx_/s1600/java_logo_50_50.jpg)
/**
* Simple Java program to demonstrate how to call one constructor from other.
* Calling one constructor from other is called constructor chaining.
* this() is used to call constructor of same class while super() is used to
* call constructor of Super class in Java.
* Simple Java program to demonstrate how to call one constructor from other.
* Calling one constructor from other is called constructor chaining.
* this() is used to call constructor of same class while super() is used to
* call constructor of Super class in Java.
*
* @author Javin Paul
*/
public class ConstructorChainingExample {
public static void main(String args[]) {
//this will first call one argument constructor of Child Class which
//in turn call corresponding constructor of super class using super(String)
System.out.println("Constructor chaining Example in Java");
Child child = new Child("Jeremy");
//this constructor will call no argument constructor of Child,
* @author Javin Paul
*/
public class ConstructorChainingExample {
public static void main(String args[]) {
//this will first call one argument constructor of Child Class which
//in turn call corresponding constructor of super class using super(String)
System.out.println("Constructor chaining Example in Java");
Child child = new Child("Jeremy");
//this constructor will call no argument constructor of Child,
//which
then call one argument constructor of
//same class, which finally call corresponding one argument constructor
//same class, which finally call corresponding one argument constructor
// of super class Parent.
System.out.println("---------------------------------");
Child emptyChild = new Child();
}
}
class Parent{
private String name;
/*
* Calling constructor of same class with one String argument
*/
protected Parent(){
this("");
System.out.println("No argument constructor of Parent called ");
}
protected Parent(String name){
this.name = name;
System.out.println("One String argument constructor of Parent called ");
}
}
class Child extends Parent{
private String name;
/*
* Calling constructor same class with one argument
*/
protected Child(){
this("");
System.out.println("No argument constructor of Child called ");
}
/*
* Calling constructor of super class with one argument
* call to super() must be first line in constructor
*/
protected Child(String name){
super(name);
System.out.println("One argument constructor of Super class called from sub class ");
}
}
Constructor chaining Example in Java
One String argument constructor of Parent called
One argument constructor of Super class called from sub class
---------------------------------
One String argument constructor of Parent called
One argument constructor of Super class called from sub class
No argument constructor of Child called
System.out.println("---------------------------------");
Child emptyChild = new Child();
}
}
class Parent{
private String name;
/*
* Calling constructor of same class with one String argument
*/
protected Parent(){
this("");
System.out.println("No argument constructor of Parent called ");
}
protected Parent(String name){
this.name = name;
System.out.println("One String argument constructor of Parent called ");
}
}
class Child extends Parent{
private String name;
/*
* Calling constructor same class with one argument
*/
protected Child(){
this("");
System.out.println("No argument constructor of Child called ");
}
/*
* Calling constructor of super class with one argument
* call to super() must be first line in constructor
*/
protected Child(String name){
super(name);
System.out.println("One argument constructor of Super class called from sub class ");
}
}
Constructor chaining Example in Java
One String argument constructor of Parent called
One argument constructor of Super class called from sub class
---------------------------------
One String argument constructor of Parent called
One argument constructor of Super class called from sub class
No argument constructor of Child called
That’s all on What is constructor chaining in Java. We have seen
How to call overloaded constructor from same class using this() and
constructor from super class using super(). Key thing
to remember is that call to another constructor must be first line in calling
constructor.
Other Java programming tutorial you may find useful
No comments:
Post a Comment