Java Program to Show Inherited Constructor Calls Parent Constructor By Default


What are the constructors?

Constructors are used to initialize the values of a particular object. The default constructor is a constructor which has no parameters.

These constructors are used to create objects, which don't have any specific value as the initial. In java, there is a keyword super(). This method is widely used in Java environment when the inheritance applied on a Java code. This super() class is used to call to the constructor of the parent class. The parent class must contain two public constructors which takes two int parameters. As we know the constructors cannot be inherited in Java so we cannot implement it here to show inherited constructor calls parent constructor by default. So there is no need to declare the final function before the constructors. As an output we will get a runtime error for this. Let’s take an example

public interface InterfaceTest {
   public InterfaceTest(){
   }
   public abstract void display();
   public abstract void show();
}

Algorithm to show inherited constructor calls parent constructor by default

  • Step 1 − Start

  • Step 2 − Declare a public class.

  • Step 3 − Take two variables as the base class.

  • Step 4 − Declare the data of a public class.

  • Step 5− Put the value of the input variables.

  • Step 6 − Get the process done.

  • Step 7 − Find the value of the output.

  • Step 8 − Return the value.

  • Step 9 − Terminate

Syntax to show inherited constructor calls parent constructor by default

public class Abc{
   public int p, q;
   public Abc(int p1, int p2){
      p = p1;
      q = p2;
   }
   public int sum(int x, int y){
      return (x + y);
   }
}
//derived class/ child class
public class Pqr : Abc{
   public int a;
   public Pqr(int a1,int p1, int p2):base(p1,p2){
      a = a1;
   }
   public int sub(int x, int y){
      return (x - y);
   }
}

//Java syntax to demonstrate the inheritance properties 
class ARBRDD {
   
   // Creating method m1 for class Programming
   public void m1(){
      System.out.println("Programming For The Tutorialspoint");
   }
}
class DP extends Programming {
   
   //Creating an another method mTwo for class DP

   public void mTwo(){
     System.out.println("DP");
   }
}
public class ARBRDD {
   
   public static void main(String[] args){
      // Creating Obj for Class DP and
      // Calling both m1 from class programming
      // And calling mTwo from class DP respectively.
      DP obj = new DP();
      obj.m2();
      obj.m1();
   }
}

Here in this particular syntax, we have tried to show that how a constructor class really works in a Java environment. This syntax will help us to build a code to show inherited constructor calls parent constructor by default.

Approaches to follow

Here are the possible approaches to show inherited constructor calls parent constructor by default. Please go through them.

  • Approach 1 − Java program to demonstrate inheritance properties

  • Approach 2 − Java program to demonstrate inheritance properties with constructor child class

  • Approach 3 − Java program to demonstrate inheritance properties with constructor child class print method

Java program to demonstrate inheritance properties

To demonstrate the inheritance properties, here we write a code with the above possible algorithm mentioned here.

Example 1

class Programmingwith2023 {
   public Programmingwith2023 (){
      System.out.println("Hey! I Am A Coder....");
   }
   public Programmingwith2023 (int i, int j){
      System.out.println("How Are You Brother/Sister!?");
   }
}
class RDDARB extends Programmingwith2023 {
   public RDDARB(){
      super(10, 20);
      System.out.println("We Are From Tutorialspoint");
   }
   public RDDARB (int i, int j){
      System.out.println("RDDARB + +");
   }
}
public class Rudra {
   public static void main(String[] args){
      RDDARB obj = new RDDARB();
   }
}

Output

How Are You Brother/Sister!?
We Are From Tutorialspoint

Example 1 A

class Programming2001 {
   public Programming2001(){
		System.out.println("Hey Brother!");
	}
}
class RDD extends Programming2001 {
   public RDD() { System.out.println("RDD"); }
}
public class Aboni {
   public static void main(String[] args){
		RDD obj = new RDD();
	}
}

Output

Hey Brother!
RDD

Inheritance properties with constructor child class

Here in this particular example we write a Java program to demonstrate the inheritance properties with a constructor child class.

Example 2

public class RDDARB {
	public static void main(String[] a){
		new child();
		new parent();
	}
}
class parent {
	parent(){
		System.out.println("I am an Indian");
	}
}
class child extends parent {
	child(){
		System.out.println("..................");
	}
}

Output

I am an Indian
..................
I am an Indian

Inheritance properties with constructor child class print method

Here in this particular example we have written a Java program to demonstrate the inheritance properties with a constructor child class encoded a print method.

Example 3

public class indiancode {
	public static void main(String[] a){
		new child();
		new child(2001);
	}
}
class parent {
	parent(){
		System.out.println("I am an Indian");
	}
}
class child extends parent {
	child(){
		System.out.println("...........@...........");
	}
	child(int x){
		System.out.println("I live in West Bengal");
	}
}

Output

I am an Indian
...........@...........
I am an Indian
I live in West Bengal

Conclusion

In this article today, we have found about the process how an inherited constructor calls parent constructor by default. With some possible java codes by following the syntaxes and algorithm we have tried to demonstrate the problem here. Hope this article helped you to understand the modus operandi of the parent constructor calling by the default.

Updated on: 12-Apr-2023

202 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements