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 Allocate and Initialize Super Class Members Using Constructor L le0 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Constructors +1 More Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Like