0% found this document useful (0 votes)
5 views16 pages

C# Inheritance Types Explained

Uploaded by

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

C# Inheritance Types Explained

Uploaded by

Hasan Saleh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

 Describe inheritance and its types in C#

 It is always nice if we could reuse something that already exists


rather than creating the same over again.
 C#, being a pure object-oriented language) supports this feature.
 In fact, the ability to create new classes from the existing classes is
the major motivation and power behind using OOP languages.
 C# classes can be reused in several ways.
 Reusability is achieved by designing new classes, reusing all or
some of the properties of easting ones.
 The mechanism of designing or constructing one class from another
is called inheritance.
 Inheritance represents a kind of relationship between two classes.
 Let us Consider two classes A and B. We can create a class hierarchy such
that B is derived from A.
 Class A, the initial class that is used as the basis for the derived class is
referred to as the base class, parent class or super class.
 Class B, the derived class, is referred to as derived class, child class or sub
class.
 A derived class is a completely new class that incorporates all the data and
methods of its base class.
 It can also have its own data and method members that are unique to
itself.
public class Animal

public void Greet()

[Link]("Hello, I'm some sort of animal!");

public class Dog : Animal

Examples of the above is-a relationship is: Dog is-a type of animal. The Dog
class is inherited from Animal class.
Animal animal = new Animal(); // creates a new object of Animal class
[Link](); //calls Greet () method
Dog dog = new Dog(); // creates a new object of Dog class
[Link](); //calls Greet () method

Even though the Greet() method is not defined in the Dog class, any
object of Dog class can access the Greet() method through
inheritance.
 Single inheritance (only one base class)

 Multiple inheritance (several base classes)

 Hierarchical inheritance (one base class, many subclasses)

 Multilevel inheritance (derived from a derived class)

C# does not directly implement multiple inheritance, however, this


concept is implemented using a secondary inheritance path in the
form of interfaces.
Some important characteristics of inheritance are:

•A derived class extends its direct base class. It can add new members to
those it inherits. However, it cannot change or remove the definition of an
inherited member.
•Constructors and destructors are not inherited. All other members,
regardless of their declared accessibility in base class, are inherited.
However, their accessibility in the derived class depends on their declared
accessibility in the base class.
•A derived class can hide an inherited member
•A derived class can override an inherited member
MEMBER
MODIFIER
PUBLIC INTERNAL PRIVATE

Public Everywhere Only program Only class

Internal Only program Only program Only class

Private Only class Only class Only class


Continue…
 This chapter explains:-

›Inheritance and different types of inheritance.

You might also like