Professional Documents
Culture Documents
C# Inheritance of Specific Langugaes
C# Inheritance of Specific Langugaes
Inheritance
Inheritance
Simply defining a class in terms of another class.
Easier to reuse.
Faster implementation.
Example?
Inheritance in real life.
The class being defined is called a Derived or Child Class.
The existing class is called a Base or Parent Class.
Also called generalization and specialization.
Format:
Access-specifier class derived-class : base-class
Implements an IS-A relationship.
UML notation:
An arrow pointing towards the base class
Example
class
What can/cannot be inherited?
Can inherit:
Public and protected members and functions.
Protected members are only accessible inside the class.
Cannot inherit
Private member functions and variables
Constructors/destructors
Overloaded operators
Friend functions
Inheritance Types
Single-level
Multi-level
Hierarchy
Multiple
Or a hybrid approach
Multiple Inheritance
Multiple Inheritance
One class can be inherited from multiple classes as well
Syntax:
class derived-class: baseA, baseB....
Base classes are separated by commas
The child class will have member functions and attributes
from both classes.
Example
Problems?
Consider the following class diagram and see if you can spot any
problems there.
The Diamond Problem
When the time comes for Button to use the function equals()
which one will it use?
The one inherited from Rectangle?
Or clickable?
Or maybe Object?
Confusing for humans as well as the compiler.
Called The Diamond Problem
Solution?
Your reading assignment for next week