Why we can not over ride a Constructor
This is a very important question, that, Why we can not over ride a Constructor. So, If we want to understand this, then we will need to understand first, What is Over riding and What is Constructor. So, Let’s understand both.
- What is Over riding (Method Overriding)
- What is Constructor
What is Over riding or Method Over riding
So, Method Over riding is a concept where one method over rides the other method. How does it happen, So it happens when the 2 methods are completly same in their exterior, like both the methods have same name, both the methods have same number of paramters, same type of parameters. So, If you will try to write this type of method in same class, then that is not possible. We can see that with an example.
So, 2 methods which are completly same should be written in different different classes, then only they over ride each other, but even then we will get a question that 2 different classes which are not related to each other, If they have their own methods with the same name and same parameters, How will one method from one class over ride the other method from other class.
So, this type of concept only comes, when 2 classes are related to each other, that means a parent and a child class. So, Let’s understand Method over riding in parent child class.
What is Constructor
Constructor is a special type of method, which has no return type and has same name of class name. So, this is the rule of creating a constructor. Constructors are invoked as soon as the object of the class is created, We don’t have to invoke a constructor explicitly.
Now, If we have to do Constructor Over riding, then we will have to create 2 Constructors in 2 different classes with same name and same paramters, but we have a rule for creating a Constructor, that Constructor should have the same name of class name, So, we can not Construcor of one class in some other class, and that is why Constructor Over riding is not possible. Constructors are tightly attached to a class, but methods are loosely attached to a class because Rule for declaring name of the Methods are independent of class names.