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

Inheritance & Polymorphism

The document discusses inheritance and polymorphism in C#, explaining that inheritance allows a subclass to extend the functionality of a base class, while C# supports single inheritance and interfaces can inherit multiple interfaces. It also covers polymorphism, which enables objects of different classes to be treated as objects of a common base class, and outlines compile-time and run-time polymorphism. Additionally, the document touches on structures, enumerations, and the implications of boxing and unboxing in C#.

Uploaded by

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

Inheritance & Polymorphism

The document discusses inheritance and polymorphism in C#, explaining that inheritance allows a subclass to extend the functionality of a base class, while C# supports single inheritance and interfaces can inherit multiple interfaces. It also covers polymorphism, which enables objects of different classes to be treated as objects of a common base class, and outlines compile-time and run-time polymorphism. Additionally, the document touches on structures, enumerations, and the implications of boxing and unboxing in C#.

Uploaded by

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

Inheritance

&
Polymorphi
sm
Hina Farooq
Inheritance

• Process of sub-classing a class to extend its functionality is called Inheritance or sub-


typing.
• The original class (or the class that is sub-typed) is called the base, parent or super
class.
• The class that inherits the functionality of the base class and extends it in its own way is
called the sub, child, derived or inherited class.
Before we go on to implementation, here are
some key-points about inheritance in C#
• C#, like Java and contrary to C++, allows only
single class inheritance. Multiple inheritance of
classes is not allowed in C#.
• The Object class defined in the System
namespace is implicitly the ultimate base class
of all the classes in C# (and the .NET
framework)
• Interfaces in C# can inherit more than one
interface.
Example
Single Inheritance (One class inherits
from another)
Multilevel Inheritance(Inheritance in a
chain of classes)
Hierarchical Inheritance (Multiple
classes inherit from one base class)
Protected
Access
modifier
Sealed Class
Finally, if you don’t want your class to
be inherited by any class, you can
mark it with the sealed keyword. No
class can inherit from a sealed class.
Real-Time Example: Banking System
(Preventing Inheritance for Security)

In a banking application,
the BankAccount class should be sealed to prevent unauthorized
modifications through inheritance.
Example;
Polymorphism

Polymorphism is one of the four pillars of Object-Oriented Programming (OOP)


in C#. It allows objects of different classes to be treated as objects of a
common base class, enabling code reusability and flexibility.
There are two types of polymorphism in C#:
1. Compile-time polymorphism (Method Overloading)
2. Run-time polymorphism (Method Overriding)
Late
binding
Late
Binding
Early
Binding
Boxing and Un-
boxing
Boxing allows value types to be
implicitly treated like objects.

un-boxing is explicit conversion from


object type to value type.
Structures, Enumeration,
Garbage Collection &
Nested Classes
• A struct is of value type, contrary to classes
which are of reference type. This means that
structures are
allocated on the stack and passed to methods by
value, that is, by making their copies.

• A struct may contain constructors (except for


the no-argument constructor), fields, methods and
Structures properties just like in classes.

(struct)
• Like all value types, structs can neither inherit
another class, nor can they be inherited.

• A struct can implement interfaces.

• Instances of a struct can be created with and


without using the new keyword.
Enumeration
C# supports enumerations using the enum keyword. Enumerations,
like classes and structures, also allow us to define new types.
Enumeration types contain a list of named constants.

You might also like