Inheritance
is one of the key concepts of object oriented programming, Inheritance helps us
to reuse/extend the methods and properties of another class. The class which
extends these members is called derived class and the original class is called
the base class. Apart from reuse/extend inheritance helps in standardizing the
basic features of the classes.
For example let us consider we want to create objects for Car, Bus, Jeep etc. Here all these are vehicles and have some property in common, hence we can create a base class say Vehicle and add the basic properties like Make, Model, Color etc to the base class, this way we make sure that any class which inherits the base class will have these properties.
For example let us consider we want to create objects for Car, Bus, Jeep etc. Here all these are vehicles and have some property in common, hence we can create a base class say Vehicle and add the basic properties like Make, Model, Color etc to the base class, this way we make sure that any class which inherits the base class will have these properties.
In C# inheritance is defined using the colon (:) symbol, with the
following syntax.
class : Derived Class Name : Base Class Name
In the following example we have a base class Vehicle and the class Bus derives from the base class and adds additional function GetSeatingCapacity(). The function GetNumber() is defined in the base class and is overridden in the derived class.
// Inheritance
class
In the following example we have a base class Vehicle and the class Bus derives from the base class and adds additional function GetSeatingCapacity(). The function GetNumber() is defined in the base class and is overridden in the derived class.
// Inheritance
public class Vehicle
{
public string GetNumber()
{
return "test";
}
}
//
public class Bus : Vehicle
{
public string
GetNumber()
{
return "test";
}
//
public string
GetSeatingCapacity()
{
return "test";
}
}
No comments:
Post a Comment