Polymorphism
Method Overloading
It is an approach of defining a method with multiple behaviors, where the behavior of the method will be changing on the based of parameters
IMP-> a class can not have 2 or more tan 2 methods with same name and same parameters with diff return type.
Method overloading can be done on:
number of parameters,
order of parameters,
type of parameters
Method Overloading vs Method Overriding
Overloading
1. we define multiple methods with same name but change the parameters.
2. Overloading can be within the same class or parent child class also.
3. while overloading, child class doesn't require any permission from parent class.
4. Overloading is all about defining multiple behaviors to a method.
Overriding
1. We define multiple methods with same name and same parameters.
2. Performed only between child and parent class. It can never be performed within same class.
3. Child class requires permission from parent class (e.g. Virtual)
4. Overriding is all about changing behavior of parent's method under the child class.
VImp-> if we want to override a parent's method under child class, that methods should be declared using the virtual modifier in parent class.
Method Hiding(Shadowing)
A parent class reference even if created by using child class instance can't access any members that are purely defined under the child class but can call overridden members of child class because overridden members are not considered as pure child class members.The members which are re-implemented using approach of hiding are considered as pure child class members and not accessible to parent's references.
Method Overloading
It is an approach of defining a method with multiple behaviors, where the behavior of the method will be changing on the based of parameters
IMP-> a class can not have 2 or more tan 2 methods with same name and same parameters with diff return type.
Method overloading can be done on:
number of parameters,
order of parameters,
type of parameters
Method Overloading vs Method Overriding
Overloading
1. we define multiple methods with same name but change the parameters.
2. Overloading can be within the same class or parent child class also.
3. while overloading, child class doesn't require any permission from parent class.
4. Overloading is all about defining multiple behaviors to a method.
Overriding
1. We define multiple methods with same name and same parameters.
2. Performed only between child and parent class. It can never be performed within same class.
3. Child class requires permission from parent class (e.g. Virtual)
4. Overriding is all about changing behavior of parent's method under the child class.
VImp-> if we want to override a parent's method under child class, that methods should be declared using the virtual modifier in parent class.
Method Hiding(Shadowing)
A parent class reference even if created by using child class instance can't access any members that are purely defined under the child class but can call overridden members of child class because overridden members are not considered as pure child class members.The members which are re-implemented using approach of hiding are considered as pure child class members and not accessible to parent's references.
No comments:
Post a Comment