05 - Interface & Abstraction
05 - Interface & Abstraction
Abstract class
Table of Contents
• Abstraction
• Interfaces
• Abstract Classes
• Interfaces vs Abstract Classes
2
Achieving Abstraction
Abstraction
What is Abstraction?
From the Latin
Abs Trahere
(away from) (to draw)
Abstraction
"Relevant" to what?
... relevant to the context of the project we develop
Abstraction helps managing complexity
Abstraction lets you focus on what the object does instead of
how it does it
5
How Do We Achieve Abstraction?
There are two ways to achieve abstraction
Interfaces
Abstract class
public interface IAnimal {}
public abstract class Mammal {}
public class Person : Mammal, IAnimal
{}
6
Abstraction vs Encapsulation
Abstraction Encapsulation
Process of hiding the Used to hide the code
implementation details and data inside a single
and showing only unit to protect the data
functionality to the user from the outside world
Achieved with interfaces Achieved with access
and abstract classes modifiers (private,
protected, public … )
7
Working with Interfaces
Interfaces
Interface
Internal addition by compiler
public interface IPrintable {
void Print(); Keyword Name
compiler
9
Interface Example
The implementation of Print() is provided in
class Document
public interface IPrint-
able {
void Print();
}
class Document : IPrintable {
public void Print()
{ Console.WriteLine("Hello");
}
10
Interface (2)
Contains only the signatures of methods,
properties, events or indexers
Can inherit one or more base interfaces
When a base type list contains a base class and interfaces, the
base class must come first in the list
A class that implements an interface can explicitly implement
members of that interface
An explicitly implemented member cannot be accessed through a
class instance, but only through an instance of the interface
11
Multiple Inheritance
Relationship between classes and interfaces
Class Interface Interface
extends implements extends
Class Class Interface
Multiple inheritance
Interface Interface Interface Interface
implements extends
Class Interface
12
Problem: Shapes
Build a project that contains an interface for drawable objects
Implements two type of shapes: Circle and Rectangle
Both classes have to print on the console
their shape with "*"
<<IDrawable>>
<<IDrawable>> Rectangle <<interface>>
Circle IDrawable
-Width: int
+Radius: int -Height: int +Draw()
13
Solution: Shapes
public interface IDrawable
{
void Draw();
}
public class Rectangle : IDrawable {
// TODO: Add fields and a constructor
public void Draw() { // TODO:
implement } }
public class Circle : IDrawable {
// TODO: Add fields and a constructor
public void Draw() { // TODO:
implement } } 14
Solution: Shapes – Rectangle Draw
public void Draw() {
DrawLine(this.width, '*', '*');
for (int i = 1; i < this.height - 1; ++i)
DrawLine(this.width, '*', ' ');
DrawLine(this.width, '*', '*'); }
private void DrawLine(int width, char end, char
mid) {
Console.Write(end);
for (int i = 1; i < width - 1; ++i)
Console.Write(mid);
Console.WriteLine(end); } 15
Solution: Shapes – Circle Draw
double rIn = this.radius - 0.4;
double rOut = this.radius + 0.4;
for (double y = this.radius; y >= -this.radius; --
y) {
for (double x = -this.Radius; x < rOut; x +=
0.5) {
double value = x * x + y * y;
if (value >= rIn * rIn && value <= rOut *
rOut)
Console.Write("*");
else
16
Abstract Classes and Methods
Abstract Classes
Abstract Class
Cannot be instantiated
May contain abstract methods and accessors
Must provide implementation for all inherited
interface members
Implementing an interface might map the interface
methods onto abstract methods
18
Abstract Methods
An abstract method is implicitly a virtual method
Abstract method declarations are only permitted in
abstract classes
An abstract method declaration provides no actual
implementation:
19
Interfaces vs Abstract Classes
Interface vs Abstract Class
Interface Abstract Class (AC)
A class may implement May inherit only
several interfaces one abstract class
Cannot have access Can provide
modifiers, everything is implementation and/or
assumed as public just the signature that
have to be overridden
Cannot provide any
Can contain access
code, just the signature
modifiers for the fields,
functions, properties
21
Interface vs Abstract Class (2)
Interface Abstract Class
Fields and constants Fields and constants
can't be defined can be defined
If we add a new method If we add a new method
we have to track down we have the option of
all the implementations of providing default
the interface and implementation and
define implementation therefore all the existing
for the new method code might work properly
22
Problem: Cars
Build a hierarchy of interfaces and classes
<<IElectricCar>> <<ICar>>
+Battery +Model: string
+Color: string
+Start(): string
+Stop(): string
Tesl Sea
a t
23
Solution: Cars
public interface ICar {
string Model { get; }
string Color { get; }
string Start();
string Stop();
}
Abstraction
…
How
… do we achieve abstraction
Interfaces
…
Abstract classes
27
Exercises
• Time to practices