0% found this document useful (0 votes)
17 views28 pages

05 - Interface & Abstraction

The document covers the concepts of abstraction in object-oriented programming, detailing how abstraction can be achieved through interfaces and abstract classes. It compares and contrasts interfaces and abstract classes, highlighting their characteristics and differences. Additionally, it provides examples of implementing these concepts through practical problems and solutions involving shapes and cars.

Uploaded by

KL M
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)
17 views28 pages

05 - Interface & Abstraction

The document covers the concepts of abstraction in object-oriented programming, detailing how abstraction can be achieved through interfaces and abstract classes. It compares and contrasts interfaces and abstract classes, highlighting their characteristics and differences. Additionally, it provides examples of implementing these concepts through practical problems and solutions involving shapes and cars.

Uploaded by

KL M
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/ 28

Lecture 05: Interface &

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

 Preserving information, relevant in a given


context, and forgetting information that is
irrelevant in that context
4
Abstraction in OOP
 Abstraction means ignoring irrelevant features, properties, or
functions and emphasizing the ones …

"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

public interface IPrintable {


public abstract void Print();
}

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:

public abstract void


Build();

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();
}

public interface IElectricCar


{
int Batteries { get; }
} 24
Solution: Cars (2)
public class Tesla : ICar, IElectricCar {
public string Model { get; private set; }
public string Color { get; private set; }
public int Batteries { get; private set; }
public Tesla (string model, string color, int bat-
teries)
{ // TODO: Add Logic here }
public string Start()
{ // TODO: Add Logic here }
public string Stop()
{ // TODO: Add Logic here }
} 25
Solution: Cars (3)
public class Seat : ICar {
public string Model { get; private
set; }
public string Color { get; private
set; }
public Tesla(string model, string
color)
{ // TODO: Add Logic here }
public string Start()
{ // TODO: Add Logic here }
public string Stop()
26
Summary

 Abstraction

 How
… do we achieve abstraction
 Interfaces

 Abstract classes

27
Exercises

• Time to practices

You might also like