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 (20)

Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Applets in java
Applets in javaApplets in java
Applets in java
 
JVM
JVMJVM
JVM
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Inline function
Inline functionInline function
Inline function
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 

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

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 

Dernier (20)

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 

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++.