0% found this document useful (0 votes)
62 views27 pages

C# Inheritance and Interfaces Guide

The document provides an overview of C# inheritance and interfaces, explaining the basics of class inheritance, method overriding, and the use of interfaces to implement multiple behaviors. It highlights the restrictions of single inheritance in C#, the importance of constructors, and how interfaces can be used to enforce method implementation. Additionally, it includes exercises for practical application of these concepts in a programming context.

Uploaded by

hoster20125
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)
62 views27 pages

C# Inheritance and Interfaces Guide

The document provides an overview of C# inheritance and interfaces, explaining the basics of class inheritance, method overriding, and the use of interfaces to implement multiple behaviors. It highlights the restrictions of single inheritance in C#, the importance of constructors, and how interfaces can be used to enforce method implementation. Additionally, it includes exercises for practical application of these concepts in a programming context.

Uploaded by

hoster20125
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

TOOL DEVELOPMENT

C# INHERITANCE & INTERFACES


A DIGITAL ARTS AND ENTERTAINMENT PRESENTATION
INHERITANCE
C# SYNTAX
c# syntax: inheritance
INHERITANCE BASICS
 Struct = value type > no inheritance
 Class = reference type > inheritance
• in fact, every class implicitely inherits [Link]
 Batman > Hero > [Link]

 Restricted in C#: you can only inherit from 1 class!


• no multiple inheritance
• (part of) solution: interfaces
we will get to that in a moment 

.3

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: inheritance
WHAT IS INHERITED?
 Fields and properties
 public/protected fields & props are inherited

 Methods
 public/protected methods are inherited

 Constructors
• not inherited!
• but can be accessed through base;
• “base” gives you access to the base Class
 calls Hero’s
constructor
• in fact, base class constructor must be called
if the base class does not have a default constructor! .4

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: inheritance
METHOD OVERRIDING
 Remember ToString()?

 ToString() is virtual in [Link],


 therefore, we can override it

 Declare as virtual in the base class:


 Override in the inheriting class:
 What if you don’t override it? > default behavior in base class .5
3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: inheritance
INHERITANCE: EXERCISE
 Given Hero class
• Add the given Hero class to your project
• Place it in a Model folder!
• Change the namespace to yours

• Add a method called SaySomething():


• Its default behavior is to return “I am [Hero Name]’!”
• Make sure it can be overridden

 Create a Batman class


• Inherit correctly from Hero
• When SaySomething() is called on this class, it should return
“Tada-dada-dada-dada BATMAAAAN!”

 Test using the given test code


• Your classes should match the code, not vice versa 
.6

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
INTERFACES – EXAMPLE 1
C# SYNTAX
INTERFACES
EXAMPLE 1: CLASS
Situation: we have a class called Event and a list of Event
instances:

.8

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
INTERFACES
EXAMPLE 1: LIST OF EVENTS
Situation: we have a class called Event and a list of Event
instances:

.9

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
INTERFACES
EXAMPLE 1: SORT LIST OF EVENTS
UPDATE: We want to sort our events:

 So... Based on what will these events be sorted? Name? Date?


.10
...??
3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
INTERFACES
EXAMPLE 1: SORT LIST OF EVENTS
ANSWER: It will crash!

 Runtime error. It does not know how to compare the items.


.11
 The answer to the problem is literally there: IComparable
3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
INTERFACES
EXAMPLE 1: SOLUTION
 When
implementi
ng the
interface we
are forced to
implement
the
CompareTo
function!
 It allows us
to choose
how events
are ordered
(here:
scheduled .12
time)
3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
INTERFACES
EXAMPLE 1: RESULT
UPDATE: We want to sort our events:

 Events are sorted based on their Scheduled Time


.13

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
INTERFACES
EXAMPLE 1: HOW DOES IT WORK?
1. ICompare interface

 The interface has a Compare function “header”; not implemented!


 Every class that implements it must implement this function!
(it is like signing a contract)

2. When sorting, the List class will call the Compare function on
the object. This is only possible if the object implements
ICompare!
 Therefore, it crashes if it does not. .14

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
INTERFACES
EXAMPLE 1: IMPLEMENTING MULTIPLE
INTERFACES

.15

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: interfaces
EXISTING INTERFACE EXAMPLE C#
 int[] (implicitely)
implements
IEnumberable<int>

 List<string> implements
IEnumberable<string>

 IEnumberable<T> interface
• allows to loop over objects
.16

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
INTERFACES – EXAMPLE 2
CREATING A CUSTOM INTERFACE
c# syntax: interfaces
INTERFACES – WHAT
“Class” that only contains signatures of
 Methods
 Properties
 Events
 Indexers

An interface has no implementations


• (except when a method is declared static, then it must be implemented)
• (from C# 8.0 on, default implementation for data members is allowed)

Can be compared to a pure virtual class in C++ .18

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: interfaces
INTERFACES – WHAT & WHY
 C# does not allow multiple inheritance,
but you can implement multiple interfaces!
 Implementing an interface is like entering a contract:
• Every member/method/.. must be implemented!

.19

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: interfaces
INTERFACES – WHAT & WHY: LOG TO CONSOLE

.20

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: interfaces
INTERFACES – WHAT & WHY: LOG TO FILE

.21

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: interfaces
INTERFACES – WHAT & WHY (USAGE)
 Creating / using an object that implements the Interface

 Can be an instance of any cla


that implements ICanLog!

 We can safely call this method


it will use the implementation
the specific object!
.22

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: interfaces
EXERCISE
 Given is a class called BaseHero, and Batman/Robin/Superman that
inherit this
 Currently you get an error on the subclasses. Why?
 Give them a constructor without parameters and pass their literal name to the
basehero class.
 Create the following interfaces (the methods write to the console):
 ICanFly > method Fly();
 ICanJump > method Jump();
 ICanSwim > method Swim();
 IHasXRayVision > method SeeThroughStuff();
 IUseless > method Die();
 In the subclasses (Batman/Robin/..), implement
interfaces of your choice (example screenshot)
 Create a test class
 Add Batman, Robin and Superman to a list .23
 Check their abilities 3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax - there is one more...
ABSTRACT

.24

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: inheritance &
OVERVIEW
interfaces + ABSTRACT KEYWORD
 We have a base class:
 we want some default implementation,
 but we want to allow inheriting classes to override this behavior,
 only if they want to though!
 use the virtual keyword

 We have a contract:
 we have no default implementations,
 and we want to force the user to implement everything in the
contract
 use an interface instead of a class

 So what if we want to implement part of a class, but not


everything?? .25

TOOL DEV
 abstract
DIGITAL ARTS AND LIES PINKET
3D.1
c# syntax: inheritance &
ABSTRACT
interfaces & VIRTUAL KEYWORDS

 Once abstract is used in class, the class itself becomes


abstract!

.26

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET
c# syntax: inheritance &
ABSTRACT
interfaces & VIRTUAL KEYWORDS

.27

3D.1
TOOL DEV DIGITAL ARTS AND LIES PINKET

You might also like