Inheritance1 min read

What is inheritance?

It’s a relationship between two classes which enables us to inherit code from one class to the other.

Truck is a Vehicle

What are benefits of inheritance?

Benefits are code reuse and polymorphism.

Code samples:

public class Vehicle {
public int Width;
public int Height;
public int NumberOfWheels;
public void ShiftSpeed(){};
public void Drive(){};
public class Car: Vehicle {
public int TrunkVolume;
public void ReverseDrive();
public class Truck: Vehicle {
public bool HasTrailer;
public class Motor: Vehicle {
public void DriveOnSingleWheel();

If using Car class we have additional method “ReverseDrive” and is available only to Car class.
When using Motor class we have additional method “DriveOnSingleWheel” which is available only to Motor class.


Base class properties or methods are available to all of the inherited classes but each inhereted class might have also own properties and methods. This way we can reuse some of the existing code.

(Visited 21 times, 1 visits today)

Leave a comment

Your email address will not be published. Required fields are marked *