Friday, September 19, 2014

Constructor inheritance(ovveriding) and reasons behind restricting constructor inheritance in Java.

Inheritance is one the key concept of OOP. Being object oriented language, Java supports it. With inheritance every Child becomes a Parent. You can access the properties of Parent from a Child once you inherit Parent (using extends keyword). Here is a key point to note that, though you can access the members of Parent you cannot inherit/ or override constructor in Java.
In this article let us discuss the reasons behind restricting Constructor Inheritance.
  • With inheritance you can access only Class members from Parent. Since constructor is not a class member, hence you cannot inherit from Parent. In a class, class members are only Fields, Methods and Nested classes.
  • Another possible reason to restrict constructor inheritance is that as we discussed in the article about constructor constructor must be the Class's simple name. If you inherit your parent constructor, then the rule breaks as Child and Parent have a different Class names.
  • And also consider Parent class have a no argument constructor and when you inherit that Parent, in case of Child you cannot provide Child's own default constructor which with a private access modifier. So making your Child class constructor private (downgrading the access) is a dream if there is constructor inheritance.

By observing the above points, it makes sense that there are good amount of reasons to stop overriding/inheriting the constructor of Parent. So we can conclude that the constructor inheritance is not possible in Java. That is not finishes the story. Still you can use or invoke the super class constructor.

Invoking super class constructor :

Though you cannot use inheritance with constructor are free to invoke Parent class constructor with help of super keyword from Child
(super(), super(args)).

If we are saying constructor inheritance is not possible, that doesn't mean that you cannot communicate between parent class constructor and child constructors. There can be a clean communication between the constructors using super keyword.
ChildClassConstructor(){  
   super(param_needed); //Invoking super class constructor with params  
 }  
 ChildClassConstructor(){  
   super(); //Invoking super class default constructor.  
 }  
Like the way shown in above you can invoke any super class constructor using super. That is all about invoking Parent class constructor and what happens when you are not explicitly invoking any super class constructor (for your needs)?? Though you are not invoking Invoking a super class constructor doesn't mean that you are executing only Child class constructor alone. There is a interesting fact that your super class constructors(till n'th super class, which is Object class constructor) also calls in that process(shown below in code). you can observe when you invoke any Child constructorThere is a chain call to the immediate parent class constructor from the current class. And the call continues until the Object class constructor invokes since that the possible most Parent classes super class is. For ex:
 public class ParentClass {  
      public ParentClass() {  
           System.out.println("Parent default constructor invoked");  
      }  
      public ParentClass(int a) {  
           System.out.println("Parent argumented constructor invoked");  
      }  
      public static void main(String[] args) {  
           SubSubClass sub = new SubSubClass();  
      }  
 }  
 class SubClass extends ParentClass {  
      public SubClass() {// not calling any super  
           System.out.println("Child default constructor invoked");  
      }  
      public SubClass(int b) {  
           super(b);  
           System.out.println("Child default constructor invoked");  
      }  
 }  
 class SubSubClass extends SubClass {  
      public SubSubClass() {// not calling any super  
           System.out.println("Sub Child default constructor invoked");  
      }  
      public SubSubClass(int b) {  
           super(b);  
           System.out.println("Sub Child default constructor invoked");  
      }  
 }  
 OUTPUT:  
 Parent default constructor invoked  
 Child default constructor invoked  
 Sub Child default constructor invoked

As you are seeing that we are not calling any super class constructor, still we can see the calling hierarchy of Constructor calling till the most possible super class which Object class's constructor and returns to the current (Child) class's constructor business logic. Summarizing the points to remember below.
  • If you call any specific super constructor (super or super(args)), then that specific super constructor invokes.
  • If you are not calling any specific constructor, then default super constructor(super()) invokes.
  • When you invoke any specific (super(args)) parent constructor that only executes and default constructor of Parent wont get execute.

Update: The folks at Webucator, a provider of Java training, put together a video tutorial for this article.























Post a Comment