Show Mobile Navigation

Webring

Powered by WebRing.
,

What are constructors and how constructors are invoked in Java?

Sachin R Kukale - 01:43

Constructors are the blocks of codes which are called whenever you create a new object in Java. In fact you can not create a object without calling a constructor. Constructors in Java play an important role.
Some rules to be remembered in case of constructors are:

  1. The name of constructors must match the name of class.
  2. The constructors can be overloaded;like methods.
  3. Constructors must not have any return types. If you declare constructor with return type then it will be treated as method with same name as class name. So one important difference between constructor and method is that constructors do no have return type and method can not be declared without return types.
  4. Constructors can have access modifiers like:default, public, private. protected.
  5. Constructors also have arguments; like methods.
  6. If you define your own constructor for a class then it will be default constructors but if you are not providing the constructor for your class compiler will provide default constructor and in this case it will be no-arg constructor always.
  7. Constructors can me parameterized or no-args constructor.

Program to prove that parent class constructor will always be called and it will be called before
child class constructor.

public class ConstructorDemo{


//Constructor for ConstructorDemo:

public ConstructorDemo(){


System.out.println("This message prints when parent class constructor is called.");


}


public static void main (String args[]) {

System.out.println("Creating the instance of only parent class:");

System.out.println("\n");

ConstructorDemo cd=new ConstructorDemo();

System.out.println("\n");

System.out.println("Now creating the instance of only child class:");

System.out.println("\n");


Child ch=new Child();


}

}

class Child extends ConstructorDemo{

//Constructor for Child

public Child(){


System.out.println("This message prints when Child class constructor is called.");


}

}

The child class constructor will be called before parent class constructor because whenever you
provide the constructor for your class the compiler places no-argument call to parent class
constructor using super() keyword.
If you do not provide the constructor for your class then compiler will place the default
no-argument constructor for your class.
And if you provide the constructor for your class explicitly
(i.e. by yourself) then it will be default constructor for your class. In this case compiler
does not provide the default constructor.

Why JVM provides default constructor for any class?

The JVM will always provide the default constructor for any class (In case if you are not providing
your constructor.) because it is not possible for
compiler to create the instance of class i.e. Object without calling constructor.

Why parent class constructor is called before child class constructor in Java??

Now you know that parent class constructor is called before child class's constructor; but why? It is because whenever there is inheritance relationship between parent and child classes the compiler will insert automatic call to super class constructor by use of keyword 'super'. And if there is super() keyword in constructor definition then it must be the first statement in definition of constructor. Thats why constructor of parent class always gets called before the constructor of child class.

0 comments:

Post a Comment