Java Program to Allocate and Initialize Super Class Members Using Constructor Last Updated : 08 Dec, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report A constructor in Java is a special method that is used to initialize an object. Whenever an object is created using the new keyword at least one construction is called. The constructor name must match with the class name and cannot have a return type. If there is no constructor available in the class in such a case java compiler provides a default constructor(no parameter constructor) by default. Parameterized Constructor: The parameterized constructor is used to initialize the field of the class with our own values. Example: Java // Parameterized Constructor Example in Java import java.io.*; class parameterizedConstructor { // fields of the class String name; int regestrationNumber; // creating a parameterized constructor so that we can // initialize the value of the class parameterizedConstructor(String name, int regestrationNumber) { System.out.println("constructor call"); this.name = name; this.regestrationNumber = regestrationNumber; } } class GFG { public static void main(String[] args) { // creating our first object parameterizedConstructor obj1 = new parameterizedConstructor("Nilesh", 2021806); System.out.println("Name of the student " + obj1.name); System.out.println("Registration Number " + obj1.regestrationNumber); // creating second object parameterizedConstructor obj2 = new parameterizedConstructor("Bhaskar", 2021807); System.out.println("Name of the student " + obj2.name); System.out.println("Registration Number " + obj2.regestrationNumber); } } Outputconstructor call Name of the student Nilesh Registration Number 2021806 constructor call Name of the student Bhaskar Registration Number 2021807 In a class hierarchy constructor are called according to the hierarchy, first parent class constructor will be invoked and then the child class constructor will be invoked Example 1 Java // Java Program to Allocate and Initialize Super Class // Members Using Constructor import java.util.*; class y { int b; int c; // parameterized constructor of class y y(int b, int c) { this.b = b; this.c = c; System.out.println("Hi I am parent constructor"); System.out.println("multiplication of two number " + b + " and " + c + " is " + b * c); } } class x extends y { int a; // parameterized constructor of class x x(int b, int c, int a) { // calls constructor of y super(b, c); System.out.println( "Hi I am child class constructor"); System.out.println("class field of x class is " + a); } } class GFG { public static void main(String[] args) { // creating an object of class x // this will invoke the constructor of x // but before invoking the constructor of class x // it will invoke the constructor of it's parent // class which is y x obj = new x(3, 4, 5); } } OutputHi I am parent constructor multiplication of two number 3 and 4 is 12 Hi I am child class constructor class field of x class is 5 Example 2 Java // Java Program to Allocate and Initialize Super Class // Members Using Constructor import java.io.*; class grandParent { int grandParentAge; String grandParentName; // constructor grandParent(int grandParentAge, String grandParentName) { this.grandParentAge = grandParentAge; this.grandParentName = grandParentName; } } class parent extends grandParent { int parentAge; String parentName; // parameterized constructor parent(int grandParentAge, String grandParentName, int parentAge, String parentName) { // calls grandparent constructor super(grandParentAge, grandParentName); this.parentAge = parentAge; this.parentName = parentName; } } class child extends parent { int childAge; String childName; // constructor child(int grandParentAge, String grandParentName, int parentAge, String parentName, int childAge, String childName) { // calls parent constructor super(grandParentAge, grandParentName, parentAge, parentName); this.childAge = childAge; this.childName = childName; } public void dispalyDetails() { System.out.println("Name of grand parent " + grandParentName + " and he is " + grandParentAge + " year old"); System.out.println("Name of parent " + parentName + " and he is " + parentAge + " year old"); System.out.println("Name of child " + childName + " and he is " + childAge + " year old"); } } class GFG { public static void main(String[] args) { // creating an object of class child // this will invoke the constructor of child // but before invoking the constructor of class // child it will invoke the constructor of it's // parent class which is parent but parent is child // of grandparent class so, before invoking the // constructor of parent class it will invoke the // constructor of grandparent class child obj = new child(85, "Pan Singh", 39, "M.S.Dhoni", 5, "Ziva Dhoni"); obj.dispalyDetails(); } } OutputName of grand parent Pan Singh and he is 85 year old Name of parent M.S.Dhoni and he is 39 year old Name of child Ziva Dhoni and he is 5 year old Comment More infoAdvertise with us Next Article Java Program to Use Method Overriding in Inheritance for Subclasses L le0 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Constructors +1 More Practice Tags : Java Similar Reads Java Program to Create an Object for Class and Assign Value in the Object Using Constructor Java is one of the most popular programming languages. It is an object-oriented programming language which means that we can create classes, objects, and many more. It also supports inheritance, polymorphism, encapsulation, and many more. It is used in all applications starting from mobile applicati 3 min read Java Program to Illustrate the Availability of Default Constructor of the Super Class to the Sub Class by Default A constructor in Java is a special method that is used to initialize an object. Whenever an object is created using the new() keyword at least one construction is called. The constructor name must match with the class name and cannot have a return type. If there is no constructor available in the cl 5 min read Java Program to Show Inherited Constructor Calls Parent Constructor By Default In java, there exists a very important keyword known as super() keyword in java which is widely used in java being object-oriented and hence inheritance comes into play. So whenever we use super keyword inside a child constructor then it calls the default parent constructor by itself. Example 1 Java 2 min read Access Super Class Methods and Instance Variables Without super Keyword in Java A class describes the property and the attributes of the object. Â A class contains mainly includes the following components. Modifiers: The keywords in java which provide a set of restriction on the class and its members and methods.Class keyword: The initialization of the class is done by class res 3 min read Java Program to Use Method Overriding in Inheritance for Subclasses Method overriding in Java is when a subclass implements a method that is already present inside the superclass. With the help of method overriding we can achieve runtime polymorphism. When we are overriding a method then we must keep three things in mind. The method in the subclass must have the sam 5 min read How to Initialize a Vector with a Specific Initial Capacity in Java? In Java, Vector Class allows to creation of dynamic arrays that can grow or shrink as per the need. If we know the approximate size of elements, we will store this in the vector. Then we optimize memory usage, and we can initialize it with the specific initial capacity. In this article, we will lear 2 min read Like