Saturday, December 29, 2012

OOP Concepts: Inheritance Example in C#.NET


Inheritance is the ability to  reuse, extend, and modify the behavior of the parent class by the child classes.

If the Parent class is Vehicle. There are common behaviors for a any vehicle. Lets assume every vehicle does run and does maintenance. So our parent class would be like below.

public class Vehicle
{
  public void Run()
   {
       //Code for run
   }
 
  public void Service()

   {
       //Code for Service
   }

}

Our child class is Car and it inherits the Vehicle class. So that Car class can reuse methods defined in the Vehicle class. Car class also can implement additional methods other than in the parent class. Which can be specific to a car.

public class Car:Vehicle
{

  Run();
  Service();
 
  private void TuneEngine()
  {
     //Code for engine tune
  }
}

No comments:
Write comments
Recommended Posts × +