SlideShare une entreprise Scribd logo
1  sur  25
ABSTRACTION
Michael Heron
Introduction
• Abstraction is a process that is important to the creation of
computer programs.
• Being able to view things at a higher level of connection than the
moving parts themselves.
• In this lecture we are going to talk about the nature of
abstraction with regards to object orientation.
• It has both a conceptual and technical meaning.
Designing Classes
• How do classes interact in an object oriented program?
• Passing messages
• How do messages flow through a class architecture?
• Uh…
• Need to understand how all the parts fit together in order
to understand the whole.
• Can’t skip this step and succeed.
Abstraction
• Process of successively filtering out low level details.
• Replace with a high level view of system interactions.
• Helped by the use of diagrams and other notations.
• UML and such
• Important skill in gaining big picture understanding of how
classes interact.
• Vital for applying Design Patterns and other such generalizations.
Abstraction in OO
• Abstraction in OO occurs in two key locations.
• In encapsulated classes
• In abstract classes
• Latter important for developing good object oriented
structures with minimal side effects.
• Sometimes classes simply should not be instantiated.
• They exist to give form and structure, but have no sensible reason
to exist as objects in themselves.
Abstract Classes
• In Java and C++, we can make use of abstract classes to
provide structure with no possibility of implementation.
• These classes cannot be instantiated because they are incomplete
representations.
• These classes provide the necessary contract for C++ to
make use of dynamic binding in a system.
• Must have a derived class which implements all the functionality.
• Known as a concrete class.
Abstract Classes
• In Java, abstract classes created using special syntax for
class declaration:
abstract class Student {
private String matriculationCode;
private String name;
private String address;
public Student (String m, String n, String a) {
matriculationCode = m;
name = n;
address = a;
}
public String getName() {
return name;
}
public String getCode() {
return matriculationCode;
}
public String getAddress() {
return address;
}
}
Abstract Classes
• Individual methods all possible to declare as abstract:
abstract class Student {
private String matriculationCode;
private String name;
private String address;
public Student (String m, String n, String a) {
matriculationCode = m;
name = n;
address = a;
}
public String getName() {
return name;
}
public String getCode() {
return matriculationCode;
}
public String getAddress() {
return address;
}
abstract public int getLoan();
abstract public String getStatus();
}
Abstract Classes
• In C++, classes are made abstract by creating a pure
virtual function.
• Function has no implementation.
• Every concrete class must override all pure virtual
functions.
• Normal virtual gives the option of overriding
• Pure virtual makes overriding mandatory.
virtual void draw() const = 0;
Abstract Classes
• Any class in which a pure virtual is defined is an abstract
class.
• Abstract classes can also contain concrete
implementations and data members.
• Normal rules of invocation and access apply here.
• Can’t instantiate an abstract class…
• … but can use it as the base-line for polymorphism.
Example
• Think back to our chess scenario.
• Baseline Piece class
• Defines a specialisation for each kind of piece.
• Defined a valid_move function in Piece.
• We should never have a Piece object
• Define it as abstract
• Every object must be a specialisation.
• Had a default method for valid_move
• Define it as a pure virtual
• Every object must provide its own implementation.
Intent to Override
• We must explicitly state our intent to over-ride in derived
classes.
• In the class definition:
• void draw() const;
• In the code:
#include "ClassOne.h"
#include <iostream>
using namespace std;
void ClassOne::draw() const {
cout << "Class One Implementation!";
}
Interfaces
• Related idea
• Interfaces
• Supported syntactically in Java
• Must be done somewhat awkwardly in C++
• Interfaces in Java are a way to implement a flavour of multiple
inheritance.
• But only a flavour
interface LibraryAccess {
public boolean canAccessLibrary();
}
Interfaces
public class FullTimeStudent extends Student implements LibraryAccess
{
public FullTimeStudent (String m, String n, String a) {
super (m, n, a);
}
public int getLoan() {
return 1600;
}
public boolean canAccessLibrary() {
return true;
}
public String getStatus() {
return "Full Time";
}
}
Interfaces
• Interfaces permit objects to behave in a polymorphic
manner across multiple kinds of subclasses.
• Both a Student and an instance of the LibraryAccess object.
• Dynamic binding used to deal with this.
• Excellent way of ensuring cross-object compatibility of
method calls and parameters.
• Not present syntactically in C++
Interfaces in C++
• Interfaces defined in C++ using multiple inheritance.
• This is the only time it’s good!
• Define a pure abstract class
• No implementations for anything
• All methods pure virtual
• Inheritance multiple interfaces to give the same effect as a
Java interface.
Interfaces in C++
• Interfaces in C++ are not part of the language.
• They’re a convention we adopt as part of the code.
• Requires rigor to do properly
• You can make a program worse by introducing multiple inheritance
without rigor.
• Interfaces are good
• Use them when they make sense.
Interfaces in C++
class InterfaceClass {
private:
public:
virtual void my_method() const = 0;
};
class SecondInterface {
private:
public:
virtual void my_second_method() const = 0;
};
Interfaces in C++
#include "Interface.h"
#include "SecondInterface.h”
class ClassOne : public InterfaceClass, public SecondInterface {
public:
void my_method() const;
void my_second_method() const;
};
#include "ClassOne.h"
#include <iostream>
using namespace std;
void ClassOne::my_method() const {
cout << "Class One Implementation!" << endl;
}
void ClassOne::my_second_method() const {
cout << "Class One Implementation of second method!" << endl;
}
Interfaces in C++
#include <iostream>
#include "ClassOne.h"
using namespace std;
void do_thing (InterfaceClass *c) {
c->my_method();
}
void do_another_thing (SecondInterface *s) {
s->my_second_method();
}
int main() {
ClassOne *ob;
ob = new ClassOne();
do_thing (ob);
do_another_thing (ob);
return 1;
}
Interfaces Versus Multiple Inheritance
• Interfaces give a flavour of multiple inheritance
• Tastes like chicken
• They handle multiple inheritance in the simplest way
• Separation of abstraction from implementation
• Resolves most of the problems with multiple inheritance.
• No need for conflicting code and management of scope
• Interfaces have no code to go with them.
Interfaces versus Abstract
• Is there a meaningful difference?
• Not in C++
• In Java and C# can…
• Extend one class
• Implement many classes
• In C++
• Can extend many classes
• No baseline support for interfaces
Interfaces versus Abstract
• Why use them?
• No code to carry around
• Enforce polymorphic structure only
• Assumption in an abstract class is that all classes will derive from a
common base.
• Can be hard to implement into an existing class hierarchy.
• Interfaces slot in when needed
• Worth getting used to the distinction.
• OO understand more portable.
More UML Syntax
Abstract class indicated by the use of
italics in attribute and method
names.
Use of interface indicated by two
separate syntactical elements
1. Dotted line headed with an
open arrow
2. Name of class enclosed in
double angle brackets
Summary
• Abstraction important in designing OO programs.
• Abstract classes permit classes to exist with pure virtual
methods.
• Must be overloaded
• Interfaces exist in C# and Java
• Not present syntactically in C++
• Concept is transferable
• Requires rigor of design in C++.

Contenu connexe

Tendances

Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Sanjit Shaw
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword pptVinod Kumar
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2MOHIT TOMAR
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaMOHIT AGARWAL
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++Jayant Dalvi
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 

Tendances (20)

Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Array in c++
Array in c++Array in c++
Array in c++
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 

En vedette

(2) cpp abstractions abstraction_and_encapsulation
(2) cpp abstractions abstraction_and_encapsulation(2) cpp abstractions abstraction_and_encapsulation
(2) cpp abstractions abstraction_and_encapsulationNico Ludwig
 
บทที่ 2 สถาปัตยกรรม
บทที่ 2 สถาปัตยกรรมบทที่ 2 สถาปัตยกรรม
บทที่ 2 สถาปัตยกรรมPrinceStorm Nueng
 
Data abstraction the walls
Data abstraction the wallsData abstraction the walls
Data abstraction the wallsHoang Nguyen
 
Degrees of data abstraction
Degrees of data abstractionDegrees of data abstraction
Degrees of data abstractionMary May Porto
 
03 data abstraction
03 data abstraction03 data abstraction
03 data abstractionOpas Kaewtai
 
Slide 3 data abstraction & 3 schema
Slide 3 data abstraction & 3 schemaSlide 3 data abstraction & 3 schema
Slide 3 data abstraction & 3 schemaVisakh V
 
Introduction to Data Abstraction
Introduction to Data AbstractionIntroduction to Data Abstraction
Introduction to Data AbstractionDennis Gajo
 
4.1 Defining and visualizing binary relations
4.1 Defining and visualizing binary relations4.1 Defining and visualizing binary relations
4.1 Defining and visualizing binary relationsJan Plaza
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Liju Thomas
 

En vedette (13)

(2) cpp abstractions abstraction_and_encapsulation
(2) cpp abstractions abstraction_and_encapsulation(2) cpp abstractions abstraction_and_encapsulation
(2) cpp abstractions abstraction_and_encapsulation
 
Lec3
Lec3Lec3
Lec3
 
บทที่ 2 สถาปัตยกรรม
บทที่ 2 สถาปัตยกรรมบทที่ 2 สถาปัตยกรรม
บทที่ 2 สถาปัตยกรรม
 
Data abstraction the walls
Data abstraction the wallsData abstraction the walls
Data abstraction the walls
 
Degrees of data abstraction
Degrees of data abstractionDegrees of data abstraction
Degrees of data abstraction
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
03 data abstraction
03 data abstraction03 data abstraction
03 data abstraction
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Slide 3 data abstraction & 3 schema
Slide 3 data abstraction & 3 schemaSlide 3 data abstraction & 3 schema
Slide 3 data abstraction & 3 schema
 
Introduction to Data Abstraction
Introduction to Data AbstractionIntroduction to Data Abstraction
Introduction to Data Abstraction
 
4.1 Defining and visualizing binary relations
4.1 Defining and visualizing binary relations4.1 Defining and visualizing binary relations
4.1 Defining and visualizing binary relations
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 

Similaire à 2CPP14 - Abstraction (20)

Abstraction
AbstractionAbstraction
Abstraction
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
 
Inheritance
InheritanceInheritance
Inheritance
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Design pattern
Design patternDesign pattern
Design pattern
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Interface
InterfaceInterface
Interface
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Csharp_mahesh
Csharp_maheshCsharp_mahesh
Csharp_mahesh
 

Plus de Michael Heron

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMichael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconductMichael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkMichael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility SupportMichael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and AutershipMichael Heron
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interactionMichael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityMichael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - TexturesMichael Heron
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)Michael Heron
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)Michael Heron
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationMichael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsMichael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsMichael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationMichael Heron
 

Plus de Michael Heron (20)

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
 
SAD04 - Inheritance
SAD04 - InheritanceSAD04 - Inheritance
SAD04 - Inheritance
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 

Dernier

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 

Dernier (20)

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 

2CPP14 - Abstraction

  • 2. Introduction • Abstraction is a process that is important to the creation of computer programs. • Being able to view things at a higher level of connection than the moving parts themselves. • In this lecture we are going to talk about the nature of abstraction with regards to object orientation. • It has both a conceptual and technical meaning.
  • 3. Designing Classes • How do classes interact in an object oriented program? • Passing messages • How do messages flow through a class architecture? • Uh… • Need to understand how all the parts fit together in order to understand the whole. • Can’t skip this step and succeed.
  • 4. Abstraction • Process of successively filtering out low level details. • Replace with a high level view of system interactions. • Helped by the use of diagrams and other notations. • UML and such • Important skill in gaining big picture understanding of how classes interact. • Vital for applying Design Patterns and other such generalizations.
  • 5. Abstraction in OO • Abstraction in OO occurs in two key locations. • In encapsulated classes • In abstract classes • Latter important for developing good object oriented structures with minimal side effects. • Sometimes classes simply should not be instantiated. • They exist to give form and structure, but have no sensible reason to exist as objects in themselves.
  • 6. Abstract Classes • In Java and C++, we can make use of abstract classes to provide structure with no possibility of implementation. • These classes cannot be instantiated because they are incomplete representations. • These classes provide the necessary contract for C++ to make use of dynamic binding in a system. • Must have a derived class which implements all the functionality. • Known as a concrete class.
  • 7. Abstract Classes • In Java, abstract classes created using special syntax for class declaration: abstract class Student { private String matriculationCode; private String name; private String address; public Student (String m, String n, String a) { matriculationCode = m; name = n; address = a; } public String getName() { return name; } public String getCode() { return matriculationCode; } public String getAddress() { return address; } }
  • 8. Abstract Classes • Individual methods all possible to declare as abstract: abstract class Student { private String matriculationCode; private String name; private String address; public Student (String m, String n, String a) { matriculationCode = m; name = n; address = a; } public String getName() { return name; } public String getCode() { return matriculationCode; } public String getAddress() { return address; } abstract public int getLoan(); abstract public String getStatus(); }
  • 9. Abstract Classes • In C++, classes are made abstract by creating a pure virtual function. • Function has no implementation. • Every concrete class must override all pure virtual functions. • Normal virtual gives the option of overriding • Pure virtual makes overriding mandatory. virtual void draw() const = 0;
  • 10. Abstract Classes • Any class in which a pure virtual is defined is an abstract class. • Abstract classes can also contain concrete implementations and data members. • Normal rules of invocation and access apply here. • Can’t instantiate an abstract class… • … but can use it as the base-line for polymorphism.
  • 11. Example • Think back to our chess scenario. • Baseline Piece class • Defines a specialisation for each kind of piece. • Defined a valid_move function in Piece. • We should never have a Piece object • Define it as abstract • Every object must be a specialisation. • Had a default method for valid_move • Define it as a pure virtual • Every object must provide its own implementation.
  • 12. Intent to Override • We must explicitly state our intent to over-ride in derived classes. • In the class definition: • void draw() const; • In the code: #include "ClassOne.h" #include <iostream> using namespace std; void ClassOne::draw() const { cout << "Class One Implementation!"; }
  • 13. Interfaces • Related idea • Interfaces • Supported syntactically in Java • Must be done somewhat awkwardly in C++ • Interfaces in Java are a way to implement a flavour of multiple inheritance. • But only a flavour interface LibraryAccess { public boolean canAccessLibrary(); }
  • 14. Interfaces public class FullTimeStudent extends Student implements LibraryAccess { public FullTimeStudent (String m, String n, String a) { super (m, n, a); } public int getLoan() { return 1600; } public boolean canAccessLibrary() { return true; } public String getStatus() { return "Full Time"; } }
  • 15. Interfaces • Interfaces permit objects to behave in a polymorphic manner across multiple kinds of subclasses. • Both a Student and an instance of the LibraryAccess object. • Dynamic binding used to deal with this. • Excellent way of ensuring cross-object compatibility of method calls and parameters. • Not present syntactically in C++
  • 16. Interfaces in C++ • Interfaces defined in C++ using multiple inheritance. • This is the only time it’s good! • Define a pure abstract class • No implementations for anything • All methods pure virtual • Inheritance multiple interfaces to give the same effect as a Java interface.
  • 17. Interfaces in C++ • Interfaces in C++ are not part of the language. • They’re a convention we adopt as part of the code. • Requires rigor to do properly • You can make a program worse by introducing multiple inheritance without rigor. • Interfaces are good • Use them when they make sense.
  • 18. Interfaces in C++ class InterfaceClass { private: public: virtual void my_method() const = 0; }; class SecondInterface { private: public: virtual void my_second_method() const = 0; };
  • 19. Interfaces in C++ #include "Interface.h" #include "SecondInterface.h” class ClassOne : public InterfaceClass, public SecondInterface { public: void my_method() const; void my_second_method() const; }; #include "ClassOne.h" #include <iostream> using namespace std; void ClassOne::my_method() const { cout << "Class One Implementation!" << endl; } void ClassOne::my_second_method() const { cout << "Class One Implementation of second method!" << endl; }
  • 20. Interfaces in C++ #include <iostream> #include "ClassOne.h" using namespace std; void do_thing (InterfaceClass *c) { c->my_method(); } void do_another_thing (SecondInterface *s) { s->my_second_method(); } int main() { ClassOne *ob; ob = new ClassOne(); do_thing (ob); do_another_thing (ob); return 1; }
  • 21. Interfaces Versus Multiple Inheritance • Interfaces give a flavour of multiple inheritance • Tastes like chicken • They handle multiple inheritance in the simplest way • Separation of abstraction from implementation • Resolves most of the problems with multiple inheritance. • No need for conflicting code and management of scope • Interfaces have no code to go with them.
  • 22. Interfaces versus Abstract • Is there a meaningful difference? • Not in C++ • In Java and C# can… • Extend one class • Implement many classes • In C++ • Can extend many classes • No baseline support for interfaces
  • 23. Interfaces versus Abstract • Why use them? • No code to carry around • Enforce polymorphic structure only • Assumption in an abstract class is that all classes will derive from a common base. • Can be hard to implement into an existing class hierarchy. • Interfaces slot in when needed • Worth getting used to the distinction. • OO understand more portable.
  • 24. More UML Syntax Abstract class indicated by the use of italics in attribute and method names. Use of interface indicated by two separate syntactical elements 1. Dotted line headed with an open arrow 2. Name of class enclosed in double angle brackets
  • 25. Summary • Abstraction important in designing OO programs. • Abstract classes permit classes to exist with pure virtual methods. • Must be overloaded • Interfaces exist in C# and Java • Not present syntactically in C++ • Concept is transferable • Requires rigor of design in C++.