- Basic Understanding of Inheritance
- Base and Derived Class
- Programming Example
Basic Understanding of Inheritance
Inheritance
is the one of the important pillar of Object Oriented Programming (OOP). In this chapter you will understand Inheritance in simple and easy language with real world example.
What is Inheritance?
Inheritance
allows you to access members of base class
in child class
. It enables you to create a child class
that can access and use all the functionality of its base class
. This way you can keep common variable and functions in a base class and use them as many times as you want in child class. Code reusability makes your program simpler and efficient.
Didn't understand? Don't worry; take a look at this real-world example.
- Your son/daughter looks like you and sometimes behaves like you but they have also own properties and behavior. It means your son/daughter has inherited your quality and mixed them with their own quality.
- Consider an example of Vehicle. All the vehicles need Tyres on their wheels. So, the manufacturer designs their vehicle as they want and import Tyre from its base class and fit into Wheels. They don’t need to design Tyre all the time. Once they designed the model and use it various times.
With the above two examples, I just want to show you the concept of Inheritance.
How Inheritance works in Programming?
Once again take the example of Vehicle but in a programming manner. This program shows how inheritance works in programming.
This program has a Tyre class
which is base class and has a member method TyreType()
which shows message that Tyre is Tubeless. There is also two class created Scooter
and Car
which inherits the functionality of Tyre class.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Basic_Example { class Program { static void Main(string[] args) { Scooter sc = new Scooter(); sc.ScooterType(); Car c = new Car(); c.CarType(); Console.ReadKey(); } } //Creating Base Class class Tyre { protected void TyreType() { Console.WriteLine("This is Tubeless Tyre"); } } //Creating Child Class class Scooter : Tyre { public void ScooterType() { Console.WriteLine("Scooter Color is Red"); TyreType(); } } //Creating Child Class class Car : Tyre { public void CarType() { Console.WriteLine("Car Type : Ferrari"); TyreType(); } } }
Output
Scooter Color is Red
This is Tubeless Tyre
Car Type : Ferrari
This is Tubeless Tyre
_
Initialization of Base class and Derived Class
Any class can be a base class. There is no dedicated rule for creating base class. When creating child class you can simple inherit base class followed by colon ( : ) symbol.
<acess-specifier> class <base_class> { ... } class <derived_class> : <base_class> { ... }
Inheritance
List of Contents
- Inheritance
- Member Access
- Abstract and Virtual Methods
- constructors and Inheritance
- Multiple Inheritance
- Sealed Inheritance
- Example
- Exercises
Summary
In this chapter you understood Inheritance
with a suitable programming example and real world scenario. In the next chapter you will learn Member Access in Inheritance.