[go: up one dir, main page]

0% found this document useful (0 votes)
13 views33 pages

Unit 1.4

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)
13 views33 pages

Unit 1.4

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/ 33

Tribhuvan University

INSTITUTE OF SCIENCE AND TECHNOLOGY(IOST)


Bachelor in Computer Science and Information Technology (B.SC CSIT)
VI Semester
NET Centric Computing(CSC-367 )
Unit-1.4: Inheritance and Polymorphism in C#

Instructor
Tekendra Nath Yogi
Tekendranath@gmail.com
Contents
• Introduction to Inheritance

• Types of Inheritance

• Use of “base” keyword

• Method hiding and overriding

• Applying polymorphism in code extensibility

• Abstract class

• Sealed class

12/15/2024 By: Tekendra Nath Yogi 2


Introduction
• The mechanism of deriving a new class from an old one is called inheritance.

• The old class is referred to as the base, ancestor, parent or super class and the
new one is called the derived, descendent, child or sub class.

• Inheritance enables code reusability and the creation of a hierarchical


relationship between classes.

12/15/2024 By: Tekendra Nath Yogi 3


Contd…,
• E.g.,

12/15/2024 By: Tekendra Nath Yogi 4


Contd…,
• The derived class inherits some or all of the features from the base class and
can add new features of its own.

• In inheritance, private members of the base class can not be accessed by any of
the derived classes.
– It's only visible for parent class's methods and properties, even if they are inherited

• Derived class can not inherits

– Constructors, and destructors of the base class.

– Overloaded operators of the base class.

– Static members of the base class.

– Sealed class features.

12/15/2024 By: Tekendra Nath Yogi 5


Contd…,
• The general form of a class declaration that inherits a base class is:

class base_class_name

// body of base class

class derived_class_name : base_class_name

// body of derived class

12/15/2024 By: Tekendra Nath Yogi 6


Contd…,
• Example Program

12/15/2024 By: Tekendra Nath Yogi 7


Type (Forms) of Inheritance
• A class can inherit properties from one or more classes and from one or more

levels.

• Based on the number of base classes and number of levels involved in the

inheritance can be categorized into the following forms:


– Single inheritance

– Multiple inheritance (via Interfaces. C# does not support multiple inheritance with classes)

– Hierarchical inheritance

– Multilevel inheritance

– Hybrid inheritance

12/15/2024 By: Tekendra Nath Yogi 8


Single Inheritance

12/15/2024 By: Tekendra Nath Yogi 9


Multi-Level Inheritance

12/15/2024 By: Tekendra Nath Yogi 10


Hierarchical Inheritance

12/15/2024 By: Tekendra Nath Yogi 11


Hybrid inheritance

12/15/2024 By: Tekendra Nath Yogi 12


Access Specifiers and Inheritance

• A private class member will remain private to its class. It is not accessible to
any code outside its class, including derived classes.

• Solution: One is to use protected members. A second is to use public properties


to provide access to private data.

12/15/2024 By: Tekendra Nath Yogi 13


Constructors in base and derived class
• In the inheritance hierarchy, both base class and derived class have their own
constructors.
• To construct the base class object only the base class constructor is used.
• To construct the derived class object the constructor for the base class
constructs the base class portion of the object, and the constructor for the
derived class constructs the derived class part.
• When both base and derived class has no user defined constructors then the
compile supply the default constructor to initialize the object.
• When a derived class has user defined constructor and base class no user
defined constructor then for derived class object construction derived class user
defined constructor is used along with the base class default constructor.
• When both the base and derived class have user defined constructors. Program
use base keyword to pass the parameter to the base class constructor.
12/15/2024 By: Tekendra Nath Yogi 14
Contd…,
• Use of “base” keyword:

– The first use is to call a base class constructor.

– The second is to access a member of the base class that has been hidden by
a member of a derived class.

12/15/2024 By: Tekendra Nath Yogi 15


Contd…,
• Calling Base Class Constructors:
– The general form of derived class constructor for this purpose is:

derived_class_constructor(parameter_list) : base(arg_list)

// body of constructor

– Here, arg_list specifies any arguments needed by the constructor in the base class.

• Order of execution of Constructor: in a class hierarchy, constructors are


called in order of derivation, from base class to derived class.

12/15/2024 By: Tekendra Nath Yogi 16


Contd…,

12/15/2024 By: Tekendra Nath Yogi 17


Contd…,

12/15/2024 By: Tekendra Nath Yogi 18


Inheritance and Name Hiding:
• It is possible for a derived class to define a member (using new keyword) that
has the same name as a member in its base class.

• When this happens, the member in the base class is hidden within the derived
class.

• To Access a such Hidden Name in derived class use the base keyword as:
base.member;

12/15/2024 By: Tekendra Nath Yogi 19


Contd…,
• Example program,

12/15/2024 By: Tekendra Nath Yogi 20


Virtual Methods and Overriding –Run time polymorphism
• A virtual method is a method that is declared as virtual in a base class by using
virtual keyword.

• Method Overriding: Virtual methods defined in base class can be redefined in


one or more derived classes by using override keyword.

• Thus, each derived class can have its own version of a virtual method.

• Different versions of same method can be called by using single base class
reference.

• In this situation, C# determines which version of the method to call based upon
the type of the object referred to by the reference and this determination is
made at runtime.

• Thus, when different objects are referred to, different versions of the virtual
method are executed.
12/15/2024 By: Tekendra Nath Yogi 21
Contd…,
class base_class Class OverrideDemo
{ {
//base class body Static void Main( )
public virtual type Method_Name(param_list)
{
{
//virtual method body base_class b= new base_class();
}
derived_class d= new derived_class();
}
base_class br;
class derived_class:base_class
br=b;
{
//derived class body br.Method_Name(param_list)
Public override type Method_Name(param_list)
br=d;
{
//virtual method body br.Method_Name(param_list);
} }
} }
12/15/2024 By: Tekendra Nath Yogi 22
Using Abstract Classes
• A Class having one or more abstract methods is called abstract class. Abstract
method is a method without any implementation.
• Features of abstract class:
– One or more abstract methods
– Defined by using abstract keyword
– There can be no objects of an abstract class
– Non abstract derived class should implement the abstract class abstract methods.

• Features of abstract method:


– created by specifying the abstract type modifier
– An abstract method contains no body and is, therefore, not implemented by the base class.
Thus, a derived class must override it.
– an abstract method is automatically virtual, and there is no need to use the virtual modifier.
– The abstract modifier can be used only on instance methods. It cannot be applied to static
methods. Properties and indexers can also be abstract.

12/15/2024 By: Tekendra Nath Yogi 23


Contd…,
class abstract base_class
{
// class body
Public abstract type Mehtod_name(param_List);

}
class derived_class: base_class
{
// derived class body
Public override type Mehtod_name(param_List)
{
// abstract method implementation
}

12/15/2024 By: Tekendra Nath Yogi 24


Using sealed to Prevent Inheritance
• To prevent a class from being inherited, precede its declaration with sealed.

• Here is an example of a sealed class:


sealed class A

// ...

// The following class is illegal.

class B : A // ERROR! Can't derive from class A

{
// ...

12/15/2024 By: Tekendra Nath Yogi 25


Contd…,
• sealed can also be used on virtual methods to prevent further overrides.
class B
{
public virtual void MyMethod() { /* ... */ }
}
class D : B
{
// This seals MyMethod() and prevents further overrides.
sealed public override void MyMethod() { /* ... */ }
}
class X : D
{
// Error! MyMethod() is sealed!
public override void MyMethod() { /* ... */ }
}
12/15/2024 By: Tekendra Nath Yogi 26
The object class, boxing and unboxing
• C# defines one special class called object that is an implicit base class of all
other classes and for all other types (including the value types).

• This means that a reference variable of object type can refer to an object of any
other type.

• Process of using object class reference to refer to value type is called boxing.

• Unboxing is the process of retrieving a value from a boxed object. This action
is performed using an explicit cast from the object reference to its
corresponding value type.

12/15/2024 By: Tekendra Nath Yogi 27


Contd…,
// A simple boxing/unboxing example.

using System;

class BoxingDemo

{
static void Main()

int x;

object obj;

x = 10;

obj = x; // boxing x into an object

int y = (int)obj; // unbox obj into an int

Console.WriteLine(y);

}
12/15/2024 By: Tekendra Nath Yogi 28
Properties and Indexer in Inheritance
• Indexers in Inheritance

12/15/2024 By: Tekendra Nath Yogi 29


Contd…,

12/15/2024 By: Tekendra Nath Yogi 30


Contd…,
• Properties in inheritance

12/15/2024 By: Tekendra Nath Yogi 31


Contd…,

12/15/2024 By: Tekendra Nath Yogi 32


Thank You !

12/15/2024 By: Tekendra Nath Yogi 33

You might also like