Inheritance, Polymorphism
Inheritance, Polymorphism
Consuming all the property and behaviour from one class to another class
by establishing a relationship called parent-child relation.
The class which uses/consume all the property and behaviour is known as
child class/sub class.
The class from which all the property and behaviour is being used, known
as parent class/base class.
Inheritance is possible only if there is is-a relationship between parent
and child class.
Advantages:
1. Code reusability
2. Less time
3. Reduce memory wastage
Types of Inheritance
1.Single
2.Mulitilevel
3.Herarichiel
Not Support
1.Multiple
2.Hybrid
class A{
class B:A{
class C:B{
Hierarchical
class A{
class B:A{
class c:A{
Single
class A{
class B:A{
class Server{
class client1:Server{
class client2:Server{
}
class Animal{
class dog:Animal{
class A{
public void Person(){}
public void Person(int n){} //method Overloading
public void Person(string s){}
}
method name and return type must be exactly same but parameter list/type/order
must not be same.
What:
It is an approach of defining multiple behaviour to a method.
Why:
1. for achieving Polymorphism
2. Because sometimes it is hard to define multiple method with diff. name and
as well as manage.
Method Overriding:
It is an approach of implementing the parent class method under the child
class with the same name and with the same signature (return type, parameter
type).
Class P{
Fun()
}
Class C:P{
Fun()
}
Difference B/W Overloading and Overriding
Overloading
1. Overloading can be performed either within the class as well as b/w parent
child class also.
2. In this case child class does not require any permission from parent class
to overload any method.
3. We can overload any method of Parent class under the child class.
4. Overloading is all about defining multiple behaviour to a method.
Overriding
1. This can be performed only b/w parent child class and never be performed
with the same class.
2. While overriding a parent's method under the child class, child class
require a permission from its parent to override.
3. If we want to override a parent's class method under the child class, then
child class can override that method which are declared using virtual
modifier in parent class.
4. Overriding is all about changing the behaviour of parent’s method under the
child class.
NOTE: If we want to override a parent’s method under the child then that
method should be declared by using virtual modifier inside parent class.
And under the child for overriding that virtual method we should use
override keyword. Any virtual method can be override under the child class.
Class Parent
{
public virtual void Test () {}
}
Class Child: Parent
{
public override void Test () {}
}
Method Hiding/Shadowing
It is an approach of re-implementing the parent class method under the
child class with the same name and with the same signature (return,
parameter).
After re-implementing parent class method under the child class, child’s
instance will start calling the local method (i.e. child method).
But if we want to call the parent class method then there are two way we
can call:
1. By creating the instance of Parent class under the child class and we can
call parent class method.
2. Using base keyword, we can parent class method from child class, but base
keyword can’t be use under the static context.
Operator Overloading
abstract class P{
public abstract void Fun();
}
class C:P{
public override void Fun(){
}
}