Search Header Logo
Inheritance -OOP

Inheritance -OOP

Assessment

Presentation

Information Technology (IT)

Practice Problem

Hard

Created by

Prosper Yeng

FREE Resource

10 Slides • 0 Questions

1

INT201: Object-Oriented Programming – Inheritance (C#)

By Dr. Prosper Yeng

2

Learning Objectives (Aligned with Bloom’s Taxonomy).By the end of this lesson, students will be able to:

  • Revise on the concepts of object, and class

  • Define inheritance in object-oriented programming.

  • Explain how inheritance enables code reusability and hierarchical class relationships.

  • Demonstrate single and multilevel inheritance in C# through practical code examples (e.g., Car → ElectricCar)

  • Differentiate between various types of inheritance

3

1. What Is an Object?

An object is a real-world entity that has data (attributes) and behavior (methods).

Example: A Car object has:

  • brand = Toyota

  • color = red

  • speed = 120

  • Methods: Start(), Stop()

Objects are created from templates or blueprints called classes.

4

2. What Is a Class?

A class is a blueprint that defines what data and actions its objects can have.

Example:

public class Car {

    public string brand;

    public int speed;

    public void Start() {

        Console.WriteLine("Car started");

    }

    public void Stop() {

        Console.WriteLine("Car stopped");

    }

}

5

3. Creating an Object instance from a Class

Example:

  • Car myCar = new Car();

  • myCar.brand = "Toyota";

  • myCar.speed = 120;

  • myCar.Start();

Output:

Car started

6

media
  • Inheritance is an object-oriented programming (OOP) principle

  • Allows a new class (called the child or derived class)

  • To acquire the properties and behaviors of an existing class (called the parent or base class).

  • Example: ElectricCar inherits all properties from Car and adds new ones like ChargeBattery().

4. What Is Inheritance?

7

5. Example: Car → ElectricCar

public class Car {

    public string brand;

    public void Start()

{

Console.WriteLine("Car started");

 }

}

public class ElectricCar : Car {

    public int batteryLevel;

    public void ChargeBattery()

  {

        Console.WriteLine("Battery charging...");

        batteryLevel = 100;

      }

}

8

6. Using the ElectricCar Class

  • ElectricCar myTesla = new ElectricCar();

  • myTesla.brand = "Tesla";

  • myTesla.Start();        // from Car

  • myTesla.ChargeBattery(); // from ElectricCar

  • Output:

  • Car started

  • Battery charging...

9

7. Calling Parent Method Using base

media

10

8.Recap

  • Object – real-world entity

  • Class – blueprint for objects

  • Inheritance – child uses parent’s code

  • Inheritance types

  • Next class:

  • base – calls parent method

  • override – modifies parent behavior

  • Inheritance type implementation

INT201: Object-Oriented Programming – Inheritance (C#)

By Dr. Prosper Yeng

Show answer

Auto Play

Slide 1 / 10

SLIDE