SlideShare uma empresa Scribd logo
1 de 25
ABSTRACTION
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 StringgetStatus();
}
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 MultipleInheritance
• 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 versusAbstract
• 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 versusAbstract
• 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 UMLSyntax
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++.

Mais conteúdo relacionado

Mais procurados

Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 
Java Programming - Polymorphism
Java Programming - PolymorphismJava Programming - Polymorphism
Java Programming - PolymorphismOum Saokosal
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#ANURAG SINGH
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3C# Learning Classes
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining ClassesIntro C# Book
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphismumesh patil
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Kumar Boro
 
04. Review OOP with Java
04. Review OOP with Java04. Review OOP with Java
04. Review OOP with JavaOum Saokosal
 

Mais procurados (20)

Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Java Programming - Polymorphism
Java Programming - PolymorphismJava Programming - Polymorphism
Java Programming - Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Java basic
Java basicJava basic
Java basic
 
Inheritance
InheritanceInheritance
Inheritance
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphism
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Introduction to java and oop
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
04. Review OOP with Java
04. Review OOP with Java04. Review OOP with Java
04. Review OOP with Java
 

Semelhante a Abstraction

Semelhante a Abstraction (20)

8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
 
Inheritance
InheritanceInheritance
Inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
 
Design pattern
Design patternDesign pattern
Design pattern
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Interface
InterfaceInterface
Interface
 
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
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.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
 
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
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 

Mais de zindadili

Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Virtual function
Virtual functionVirtual function
Virtual functionzindadili
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaingzindadili
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
Polymorphism
PolymorphismPolymorphism
Polymorphismzindadili
 
Function overloading
Function overloadingFunction overloading
Function overloadingzindadili
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritancezindadili
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritancezindadili
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritancezindadili
 
Abstraction1
Abstraction1Abstraction1
Abstraction1zindadili
 
Access specifier
Access specifierAccess specifier
Access specifierzindadili
 
Friend function
Friend functionFriend function
Friend functionzindadili
 

Mais de zindadili (20)

Namespaces
NamespacesNamespaces
Namespaces
 
Namespace1
Namespace1Namespace1
Namespace1
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Templates2
Templates2Templates2
Templates2
 
Templates1
Templates1Templates1
Templates1
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Aggregation
AggregationAggregation
Aggregation
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritance
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritance
 
Abstraction1
Abstraction1Abstraction1
Abstraction1
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Inheritance
InheritanceInheritance
Inheritance
 
Friend function
Friend functionFriend function
Friend function
 
Enum
EnumEnum
Enum
 

Último

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Último (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

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 StringgetStatus(); }
  • 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 MultipleInheritance • 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 versusAbstract • 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 versusAbstract • 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 UMLSyntax 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++.