0% found this document useful (0 votes)
19 views

Inheritance, Polymorphism

Uploaded by

abhive106
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Inheritance, Polymorphism

Uploaded by

abhive106
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Inheritance:

 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.

class child_Class: Parent_Class

Advantages:
1. Code reusability
2. Less time
3. Reduce memory wastage

Imp. rule for Inheritance:


1. A parent class constructor must be accessible in child class otherwise
inheritance will be not possible.
2. A child class can access members of its Parent class but parent class member
cannot access member of child class.
3. We can initialize parent class variable in the child class, we can
initialize parent class variable through the child class object.
4. Every class defined by us or predefined class has a default parent class
Object under the system namespace. (We can say p1 as reference variable
because it is referring Parent object which has no name.)
5. If there is a parameterized cons. in parent class the implicit call form
child class cons. will not work. We have to explicitly call parent class
cons. from child class constructor.

Types of Inheritance
1.Single

2.Mulitilevel

3.Herarichiel

Not Support

1.Multiple

2.Hybrid

Because in c# one child class cannot have immediate 2 parents.


Multilevel

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{

Polymorphism:(Many-Form)-One thing many forms


 Ability to create many form/Achieving multiple behaviour with same
method/object.

Way to achieve Polymorphism


1. Method Overloading
2. Method Overriding
3. Operator Overloading
Method Overloading:
 Achieving multiple behaviour with same method name on the basis on different
inputs(signature).

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.

Why, what benefit

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.

Difference b/w Overriding and Hiding


1. In Overriding, we can re-implement that method only if which are virtual
in parent class.
2. In Hiding we can re-implement any method of parent class.
3. A Parent class reference even if created using child class instance can not
access any members that are purely define in child class but can call
overridden method of child class and in-case hiding parent class method
calls which we hide under the child class.

Operator Overloading

 It is an approach of implementing multiple behaviour to an operator on


the basis of different types of operand.

Abstract Class and Abstract Method:


 The concept of abstract method is nearly like concept of method overriding.

abstract class P{
public abstract void Fun();
}
class C:P{
public override void Fun(){

}
}

You might also like