[go: up one dir, main page]

0% found this document useful (0 votes)
11 views26 pages

OOP2-Lecture-Week-04

The document covers the concept of inheritance in object-oriented programming, explaining the relationships between base classes and derived classes. It discusses various types of inheritance, including single, multilevel, and hierarchical inheritance, along with the IS-A and HAS-A relationships. Additionally, it provides examples in C# to illustrate how inheritance is implemented in programming.

Uploaded by

Raj Syn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views26 pages

OOP2-Lecture-Week-04

The document covers the concept of inheritance in object-oriented programming, explaining the relationships between base classes and derived classes. It discusses various types of inheritance, including single, multilevel, and hierarchical inheritance, along with the IS-A and HAS-A relationships. Additionally, it provides examples in C# to illustrate how inheritance is implemented in programming.

Uploaded by

Raj Syn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Inheritance

Course Code: CSC 2210 Course Title: Object Oriented Programming 2

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 04 Week No: 04 Semester: Summer 2019-2020


Lecturer:
Topics

1. Inheritance
2. HAS-A relationship
3. IS-A relationship
4. Single
5. Multilevel
6. Hierarchical Inheritance
Inheritance

Inheritance is ability for derived class to inherit all the members of the base class
Derived class also can be referred to as “child class” or “subclass”
Base class also can be referred to as “parent class” or “superclass”
Syntax to specify inheritance
class SubClass : SuperClass

Inheritance allows to implement Specialization and Generalization concepts e.g.:


Radio button is-a specialized case of Control
Control is a base class
Radio button derived class
What is
 Inheritance?
A relationship between a more general class (called the
superclass or base class) and a more specialised
class(called the subclass or derived class).
 For example, a Cat is a specific type of Animal. Therefore
the Animal class could be the superclass/base class, and
the Cat class could be a subclass of Animal.
Why is Inheritance
important?
 Inheritance is a fundamental concept in Object-Oriented
programming.

 Many objects exist that share a lot of commonality between them.


By using inheritance, subclasses “inherit” the public variables and
methods of the superclass, in addition to their own variables and
methods.

 Any private method or variables from the superclass will not be


inherited.

 Inheritance helps to improve code re-use and minimise duplicate


code among related classes.
Inheritance Hierarchies
 We can represent inheritance among objects via an inheritance
hierarchy.

Vehicle

Motorbike Car

Saloon MPV

Truck
IS-A Relationship
 Use the IS-A test to verify that your inheritance hierarchy is valid.

 A Dog IS-A Animal – makes sense, therefore the Dog class can inherit from
an Animal class.

 A Door IS-A Car– doesn’t make sense, so the Door class shouldn’t inherit
from the Car class.

 An Animal IS-A Dog – doesn’t make sense. A Dog IS-A Animal, but an Animal
is not a Dog.

 The IS-A test only works in one direction i.e. a Cat IS-A Animal, but an
Animal is not a Cat.
HAS-A Relationships
• Use the HAS-A test to verify whether an object should
contain another object as an instance variable. This is called
object composition.

• A Car IS-A Engine – doesn’t make sense.

• A Car HAS-A Engine – This makes sense. So therefore our


Car class would have an instance variable of type Engine.
public class Car{
private Engine carEngine;
public Car(){
carEngine = new Engine();
}
}
Inheritance in C#
public class Cat : Animal
{
public Cat() : base()
{
Console.WriteLine("Cat Constructor");
}

public void runUpATree()


{
Console.WriteLine("I am sleeping");
}
}

• To declare a subclass in C#, we use the following notation


public class Subclass : BaseClass

• We give the subclass a name followed by a colon (:) and the


name of the base class we want to inherit from.
Single Inheritance

In this type of inheritance, the derived class inherits properties and


behavior from a single base class. It's like a child inherits the traits of
his/her parents.
Single Inheritance

Output:
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.
Output:
Multilevel Inheritance

In this type of inheritance, a class inherits another derived/child


class which in turn inherits another class. It's like a child inherits
the traits of his/her parents, and parents inherit the traits of
their grandparents.
Output:
Thank You
Books

• C# 4.0 The Complete Reference; Herbert Schildt; McGraw-Hill Osborne Media; 2010
• Head First C# by Andrew Stellman
• Fundamentals of Computer Programming with CSharp – Nakov v2013
References

MSDN Library; URL: http://msdn.microsoft.com/library

C# Language Specification; URL: http://download.microsoft.com/download/0/B/D/0BDA894F-


2CCD-4C2C-B5A7-4EB1171962E5/CSharp%20Language%20Specixfication.doc

C# 4.0 The Complete Reference; Herbert Schildt; McGraw-Hill Osborne Media; 2010

You might also like