1. Constructors can’t be final because when we use final with methods , that means method can’t be override. Constructors by Java rules can’t be override anyways , so no point in making constructors as final.
2. Constructors can’t be static because constructors in java are used to instantiate object (in use with new keyword) whereas static keyword is used for class variables and not object.
3. Constructors can’t be abstract because when you set a method as ‘abstract’, it means method doesn't have any body and you want to implement it at another time in a child class, but the constructor is called implicitly when the new keyword is used so it can’t lack a body.
4. Constructors can’t be overrided and hence can it can't be inherited. A constructor cannot be called with an object but method can be called. To call a constructor, an object must be created.
5. A Subclass constructor can access super class constructor.
6. In method overriding, the super class method and subclass method should be of the same – same name, same return type and same parameter list. In constructors, same name constructor cannot be written in another class. It gives compilation error : invalid method declaration , return type required
7. We can use super() to call the parent class parameterized constructor. To call a parent class constructor the super() should be first statement in the child constructor.
8. Non parameterized constructors of parent class are called on their own once we call the child class constructor. Depending on condition we also use super() to call parent class constructors
2. Constructors can’t be static because constructors in java are used to instantiate object (in use with new keyword) whereas static keyword is used for class variables and not object.
3. Constructors can’t be abstract because when you set a method as ‘abstract’, it means method doesn't have any body and you want to implement it at another time in a child class, but the constructor is called implicitly when the new keyword is used so it can’t lack a body.
4. Constructors can’t be overrided and hence can it can't be inherited. A constructor cannot be called with an object but method can be called. To call a constructor, an object must be created.
5. A Subclass constructor can access super class constructor.
6. In method overriding, the super class method and subclass method should be of the same – same name, same return type and same parameter list. In constructors, same name constructor cannot be written in another class. It gives compilation error : invalid method declaration , return type required
7. We can use super() to call the parent class parameterized constructor. To call a parent class constructor the super() should be first statement in the child constructor.
8. Non parameterized constructors of parent class are called on their own once we call the child class constructor. Depending on condition we also use super() to call parent class constructors
No comments:
Post a Comment