SlideShare a Scribd company logo
1 of 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++.

More Related Content

What's hot

Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
ย 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
Abhilash Nair
ย 

What's hot (20)

Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
ย 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
ย 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
ย 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
ย 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
ย 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
ย 
Abstraction
AbstractionAbstraction
Abstraction
ย 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
ย 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
ย 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
ย 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
ย 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
ย 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
ย 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
ย 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
ย 
Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
ย 
C presentation book
C presentation bookC presentation book
C presentation book
ย 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
ย 
Function in C program
Function in C programFunction in C program
Function in C program
ย 
Packages in java
Packages in javaPackages in java
Packages in java
ย 

Viewers also liked

Lec3
Lec3Lec3
Lec3
Saad Gabr
ย 
เธšเธ—เธ—เธตเนˆ 2 เธชเธ–เธฒเธ›เธฑเธ•เธขเธเธฃเธฃเธก
เธšเธ—เธ—เธตเนˆ 2 เธชเธ–เธฒเธ›เธฑเธ•เธขเธเธฃเธฃเธกเธšเธ—เธ—เธตเนˆ 2 เธชเธ–เธฒเธ›เธฑเธ•เธขเธเธฃเธฃเธก
เธšเธ—เธ—เธตเนˆ 2 เธชเธ–เธฒเธ›เธฑเธ•เธขเธเธฃเธฃเธก
PrinceStorm Nueng
ย 
Degrees of data abstraction
Degrees of data abstractionDegrees of data abstraction
Degrees of data abstraction
Mary May Porto
ย 
03 data abstraction
03 data abstraction03 data abstraction
03 data abstraction
Opas Kaewtai
ย 

Viewers also liked (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++
ย 

Similar to 2CPP14 - Abstraction

Inheritance
InheritanceInheritance
Inheritance
abhay singh
ย 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
teach4uin
ย 
Csharp_mahesh
Csharp_maheshCsharp_mahesh
Csharp_mahesh
Ananthu Mahesh
ย 

Similar to 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
ย 

More from Michael Heron

More from 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
ย 

Recently uploaded

CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female serviceCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
ย 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
ย 
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
ย 

Recently uploaded (20)

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
ย 
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female serviceCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female service
ย 
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
ย 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
ย 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.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...
ย 
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
ย 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
ย 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
ย 
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 โ˜‚๏ธ
ย 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
ย 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
ย 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
ย 
Vip Call Girls Noida โžก๏ธ Delhi โžก๏ธ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida โžก๏ธ Delhi โžก๏ธ 9999965857 No Advance 24HRS LiveVip Call Girls Noida โžก๏ธ Delhi โžก๏ธ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida โžก๏ธ Delhi โžก๏ธ 9999965857 No Advance 24HRS Live
ย 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
ย 
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 ...
ย 
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
ย 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
ย 
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
ย 
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
ย 

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