Inheritance.md - brainchildservices/curriculum GitHub Wiki

Slide 1

C# Inheritance

image

Slide 2

What is Inheritance?

Inheritance is one of the major pillars of object-oriented programming
Inheritance is a mechanism by which a new class is derived from an existing one. The new class, which is being created, is called a derived class(child class) and the class from which it is derived is called a base class (Parent class).

To inherit from a class, use the : symbol.

The inheritance means to receive the properties of an already existing class.

Slide 3

Eg1: https://dotnetfiddle.net/DmroUg Here we are creating a class of Person and then using inheritance to derive class Employee from Person. Employee gets all the members of class Person. Employee class has additional details like employee Id.
In the Main method, we have create an object of class Employee. Assigned values to it. Using the same object we can print both the Employee details. If we need only the Personal details, we can use the same object to print the Personal details alone of the Person.

Eg2: https://dotnetfiddle.net/pFDdn5 We are doing the same program without using inheritance: In this case, for the same person, we will have to assign his values both in Person object as well as Employee object. So we would need to create two separate objects for both Employee as well as Person

Slide 4

What are derived-class : base-class?
Derived Class (child) - the class that inherits from another class
Base Class (parent) - the class being inherited from

Slide 5

Exercise:

Q1. Design and implement class Vehicle and inherit class Car from it.
The class Vehicle should have 2 fields that are unique to all vehicles. Create a method that is unique to all vehicles and use the fields declared earlier.
The class Car should inherit the class Vehicle. Car should have members that are unique to cars. Create method inside Class Car, and call the method defined in Vehicle. Also print something unique to Car in the method using the members declared earlier.

In Main method, create object of class Car. Assign value to fields of class Vehicle. Call the method in Car. Use the same object to call the method in class Vehicle.

In Main method, create another object of class Car. Assign value to fields of class Vehicle and class. Call the method in Car. Use the same object to call the method in class Vehicle

Slide 5 Downwards

Q2. Design and implement class Tree and inherit class AppleTree from it
The class Tree should have 2 fields that are unique to all trees. Create a method that is unique to all trees and use the fields declared earlier.
The class AppleTree should inherit class Tree. Apple should have members that are unique to Apple Trees. Create a method inside Class AppleTree, and call the method defined in Tree. Also print something unique to Apple tree in the method sing the members declared earlier.

In Main method, create an object of class AppleTree. Assign a value to fields of class Tree and class AppleTree. Call the method in AppleTree
Use the same object to call the method in class Tree.

In Main method, create another object of class AppleTree. Assign a value to fields of class Tree and class AppleTree. Call the method in AppleTree
Use the same object to call the method in class Tree.

Slide 6

What are the types of inheritance?

The C# and .NET support only single inheritance. However based on how different classes derive from their base class, we can categories them as:

  • Single Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance (achievable using the Interface)

Slide 7

Single Inheritance
In single inheritance, subclasses inherit the features of one superclass.
image
https://dotnetfiddle.net/LUH8Rg

Hierarchical Inheritance
In this type of inheritance, the multiple classes derives from one base class. It's like having multiple kids, all inheriting traits from parent, but in their own different ways.
image
https://dotnetfiddle.net/8qRziQ

Slide 8

Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class.
It's like a child inherits the traits of his/her parents, and parents inherit the traits of their grandparents.
image
https://dotnetfiddle.net/copd15

What is the use of Inheritance?

  • It helps in using the same code again means code reusability.
  • It reduces code redundancy.
  • It helps in reading the code more comfortably.
  • It also reduces the size of the source code and file.
  • It helps in providing the extensibility to code.
  • The code is easy to manage as it divided into classes of the base class and child class.
  • It helps in dividing the large code into small pieces.

Slide 9

What is a sealed class?
Sealed class is used to stop a class to be inherited. You cannot derive or extend any class from it.
https://dotnetfiddle.net/BikdcR

Exercise:

  1. Develop a project with the following goals. Write a program for creating Laptop. Name, Price, Processor, Ram and Hard drive should be defined in base class-LaptopBase. You need to inherit these functionalities in derived class(Laptop) and Print Details. All the laptop should have different name, price, processor, ram and hard drive
    image

Slide 9 Downwards

  1. Develop a project based on the following design:
    image
    Animal is the base class: Eagle, Tiger, Wolf are the classes derived from class Animal

  2. Define a class Human with properties "first name" and "last name". Define the class Student inheriting Human, which has the property "mark". Define the class Worker inheriting Human with the property "wage" and "hours worked". Implement a "calculate hourly wage" method, which calculates a worker’s hourly pay rate based on wage and hours worked.

Slide 9 Downwards

The use of new keyword
Creating object of child class with parent type Vs Creating object of child class with child type
Use of virtual
Method Over-riding
What is a sealed method?

References:
https://www.c-sharpcorner.com/article/overview-of-inheritance-in-c-sharp/ https://www.w3schools.com/cs/cs_inheritance.asp
https://www.geeksforgeeks.org/c-sharp-inheritance/
https://www.c-sharpcorner.com/UploadFile/0c1bb2/types-of-inheritance-in-C-Sharp/
https://www.c-sharpcorner.com/UploadFile/9582c9/inheritance-with-example-in-C-Sharp/
https://www.c-sharpcorner.com/UploadFile/e06010/playing-with-inheritance-in-C-Sharp-net/
https://www.c-sharpcorner.com/article/overview-of-inheritance-in-c-sharp/
https://docs.google.com/document/d/1GORN4Vz00hJH5esOHpAPrl9tALIfn4tIdH8qPB7yEnI/edit?usp=sharing https://www.studytonight.com/post/csharp-inheritance