Denis Jakus

making tech less cryptic

CTO x Unique People

Denis Jakus

making tech less cryptic

CTO x Unique People

CTO Note

Inheritance1 min read

12 November 2017 .NET
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.

i.e.
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.

SUMMARY

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.

Taggs:
Related Posts
How to organize Visual Studio projects

Facebook Twitter Evernote Hacker News Gmail Print Friendly Yahoo Mail reddit LinkedIn Blogger Tumblr Like In my latest Youtube video,…

Caching in DotNet Core WebAPI using Strategy Pattern

Facebook Twitter Evernote Hacker News Gmail Print Friendly Yahoo Mail reddit LinkedIn Blogger Tumblr Like Let me write today about…

Write a comment