Which constructor executes first the parent constructor or the child constructor?

Constructor

Constructor is a special kind of method in a class with the same name of the class and without return type. 

We can have multiple constructors in a class [Parameterized Constructor/Copy Constructor/Static Constructor/Private Constructor]. 

When we create a class without a constructor then, the compiler will automatically create a default constructor of the class.

Example

public class Animal
{
    public Animal[]
    {
        Console.WriteLine["I am a constructor"];
    }
}

Order of constructors calling with inheritance


With non-static constructors?

While object creation of a class, the default constructor of that class is automatically called to initialize the members of the class.

In case of inheritance if we create an object of child class then the parent class constructor will be called before child class constructor.

i.e. The constructor calling order will be top to bottom.

Example

public class Animal
{
    public Animal[]
    {
        Console.WriteLine["I am animal constructor."];
    }
}
public class Dog : Animal
{
    public Dog[]
    {
        Console.WriteLine["I am dog constructor."];
    }
}
class Program
{
    static void Main[string[] args]
    {
        Dog dog = new Dog[];
        Console.Read[];
    }
}

Output

So as we can see in the output, the constructor of Animal class[Base class] is called before Dog class[Child].

But why?

Because when we inherit a class into another call then the child class can access all the features of the base class [except private members].

So if in the initialization process of child class there may be the use of parent class members.

This is why the constructor of the base class is called first to initialize all the inherited members.

With static constructors?

In the case of a static constructor, calling order will be reversed from non-static i.e. it will call from bottom to top.

Example

public class Animal
{
    static Animal[]
    {
        Console.WriteLine["I am animal constructor."];
    }
}
public class Dog : Animal
{
    static Dog[]
    {
        Console.WriteLine["I am dog constructor."];
    }
}
class Program
{
    static void Main[string[] args]
    {
        Dog dog = new Dog[];
        Console.Read[];
    }
}

Output

Here child class [Dog] constructor is called before the parent class [Animal] constructor.

Summary

In this article, we learned that in the case of non-static constructors, calling order is top to bottom, and with static constructors calling order is bottom to top.

Introduction

Inheritance is the act of deriving a new class from an existing class. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods and variables of the original class. The new derived class is called a child class, or subclass. The original class is called parent class, or superclass, or base class. Since the derived class is always more specific, the relation between these classes is called is-a relation. Class extension can be used for a number of purposes. The most common use is specialization - where the extended class defines new behaviors and thus becomes a specialized version of its superclass. Consider two classes Book and Dictionary. Which one is more general? Book, since a dictionary is a specific kind of a book. Therefore, we say that Dictionary extends Book.

public class Dictionary extends Book
{
} 

All public methods declared in Book available in Dictionary, but not vice versa: inheritance is a one-way street. Inheritance allows the following three changes in derived class:

  1. add new fields [variables]
  2. add new methods
  3. override existing [in base class] methods

The following two changes are NOT allowed:

  1. derived class cannot remove fields [variables]
  2. derived class cannot remove methods.

There are two forms of inheritance:

  1. inheritance of type: whereby the subclass acquires the type of the base class;
  2. inheritance of implementation: whereby the subclass acquires the implementation of the base class;

These two forms always occur together.

Implementation by Inheritance

The following picture demonstrates the stack implementation by inheritance

See Stack.java for a complete implementation of the Stack class.

super

Constructors are not inherited in a derived class. Therefore, a child's constructor is responsible for calling the parent's constructor:
public Stack[]
{
   super[];
}

By keyword super Java provides a mechanism to call parent's constructor. The above super[] is actually a call for ArrayList's constructor. The super must be used in the first line [literally] of a constructor.

If the child class constructor does not call super, the parent's constructor with no arguments will be implicitly called. If parent class implements a constructor with arguments and has no a constructor with no arguments, then the child constructors must explicitly call a parents constructor.

The super reference can also be used to invoke other methods in parent's class. How would you call the ArrayList's toString method from inside the Stack's toString?. Since both methods have the same signature, we say that Stacks's toString overrides Book's toString[]. We will distinguish between them using this and super keywords. Consider the following code example and trace new B[].m1[] call:

public class Demo
{
   public static void main[String[] args]
   {
      System.out.println[ new B[].m1[]  ];
   }
}

class A
{
   int m1[] {return m3[];}
   int m2[] {return this.m3[];}
   int m3[] {return 1;}
}

class B extends A
{
   int m1[] {return super.m1[];}
   int m3[] {return 4;}
   int m4[] {return m2[];}
}

Here is a trace: B.m1[] -> A.m1[] -> B.m3[] -> 4.

A child class can override any public parent's method that is not defined with the final modifier. Also, classes with final modifier cannot be extended. The Java's String and StringBuffer classes are examples of such classes.

protected

The derived class inherits all variables and methods, which are public. It does not inherit those variables and methods, which are private. However, we do not want to declare all variables and methods to be public, since they will be visible for everybody. Therefore, Java offers another modifier called protected. Protected variables are not accessible for other classes, but only for derived classes.

Hierarchies

Multiple classes can be derived from a single parent. There is no limit to the number of children a class can have [but a child can have only one parent]. Two children of the same parent are called siblings. Siblings are NOT related to each other by inheritance. Inheritance is transitive. All methods and fields are passed from a parent to the children and then from children to their children and further. There is no single best hierarchy, the decision is made when you design your classes.

Polymorphism

The term polymorphism can be defined as having many forms. A polymorphic reference is one that refer to different types of the objects. Polymorphism allows programs to be written more abstractly and therefore less redundantly. Polymorphism is implemented in Java using a technique called dynamic binding - where exact types are determined during the program execution.

Consider a collection of greeting cards: the base class is Card which has a subclass Holiday which has a subclass ApprilFool:

public class Card
{
   String toWhom;

   public Card[String name]
   {
      toWhom = name;
      System.out.println["\nDear " + toWhom + "!"];
   }
}

public class Holiday extends Card
{
   public Holiday[String name]
   {
      super[name];
      System.out.println["Season's Greetings!"];
   }
}

public class AprilFool extends Holiday
{
   public AprilFool[String name]
   {
      super[name];
      System.out.println["Just Kidding!"];
   }
}

Next we declare a reference of the type Card.

Card myCard;

myCard is a polymorphic reference that can have any of three types: Card, Holiday or AprilFool.:

myCard = new Card["Charlie"];
myCard = new Card["Caitlin"];
myCard = new Card["Alex"];
This will print
Dear Charlie!

Dear Caitlin!
Season's Greetings!

Dear Alex!
Season's Greetings!
Just Kidding!

It's important to note that the declaration

Holiday hol = new Card["fill the blank"]; 
is incorrect - a variable can hold a reference to an object who's class is a descendant of the class of the variable.

Which constructor is executed first?

Order of execution of constructor in Single inheritance In single level inheritance, the constructor of the base class is executed first.

In which order the constructors are executed?

Constructors of Virtual base classes are executed, in the order that they appear in the base list. Constructors of nonvirtual base classes are executed, in the declaration order. Constructors of class members are executed in the declaration order [regardless of their order in the initialization list].

Which constructor is executed first in Java?

The compiler knows that when an object of a child class is created, the base class constructor is called first. And if you try to manually change this behavior, the compiler won't allow it.

Does a child constructor always invoke a parent constructor?

If parent class implements a constructor with arguments and has no a constructor with no arguments, then the child constructors must explicitly call a parents constructor.

Chủ Đề