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# 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.
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 Person
public void Greet()
[Link]("Hello, I’m a new person!");
public class Student : Person
}
Person p = new Person(); // creates a new object of Person class
[Link](); //calls Greet () method
Student s = new Student(); // creates a new object of Student class
[Link](); //calls Greet () method
Even though the Greet() method is not defined in the Student class,
any object of Student class can access the Greet() method through
inheritance.
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
Continue…
This chapter explains:-
›Inheritance and different types of inheritance.