Learn Java by Example: Java Program to Call One Constructor from another

Java Program to Call One Constructor from another

In this example, we will learn how we can call one constructor from another constructor in Java.

 


Example 1: Java program to call one constructor from another


class Main{

  int sum;

  // first constructor
  Main() {
    // calling the second constructor
    this(5, 2);
  }

  // second constructor
  Main(int arg1, int arg2) {
    // add two value
    this.sum = arg1 + arg2;
  }

  void display(){
    System.out.println("Sum is: " + sum);
  }

  // main class
  public static void main(String[] args){

    // call the first constructor
    Main obj = new Main();

    // call display method
    obj.display();
  }
}

Output

Sum is: 7

In the above example, we have created a class named Main. Here, you have created two constructors inside the Main class.

Main() {..}

Main(int arg1, int arg2) {...}

Inside the first constructor, we have used this keyword to call the second constructor.

this(5, 2);

Here, the second constructor is called from the first constructor by passing arguments 5 and 2.

Note: The line inside a constructor that calls another constructor should be the first line of the constructor. That is, this(5, 2) should be the first line of Main().


Example 2: Call the constructor of the superclass from the constructor of the child class

We can also call the constructor of the superclass from the constructor of child class using super().


// superclass
class Languages{

  // constructor of the superclass
  Languages(int version1, int version2) {

    if (version1 > version2) {
      System.out.println("The latest version is: " + version1);
    }

    else {
      System.out.println("The latest version is: " + version2);
    }

  }
}

// child class
class Main extends Languages{

  // constructor of the child class
  Main() {
    // calling the constructor of super class
    super(11, 8);
  }

  // main method
  public static void main(String[] args){

    // call the first constructor
    Main obj = new Main();

  }
}

Output

The latest version is: 11

In the above example, we have created a superclass named Languages and a subclass Main. Inside the constructor of the Main class, notice the line,

super(11, 8);

Here, we are calling the constructor of the superclass (i.e. Languages(int version1, int version2)) from the constructor of the subclass (Main()).

 

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.