Java Program to Allocate and Initialize Super Class Members Using Constructor
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); } } |
Output
constructor 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 ); } } |
Output
Hi 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(); } } |
Output
Name 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
Please Login to comment...