SlideShare a Scribd company logo
1 of 36
Copyright © 2005 Elsevier
Object-Oriented Programming
• Control or PROCESS abstraction is a very old
idea (subroutines!), though few languages
provide it in a truly general form (Scheme
comes close)
• Data abstraction is somewhat newer, though its
roots can be found in Simula67
– An Abstract Data Type is one that is defined in
terms of the operations that it supports (i.e., that
can be performed upon it) rather than in terms of its
structure or implementation
Copyright © 2005 Elsevier
Object-Oriented Programming
• Why abstractions?
– easier to think about - hide what doesn't matter
– protection - prevent access to things you
shouldn't see
– plug compatibility
• replacement of pieces, often without recompilation,
definitely without rewriting libraries
• division of labor in software projects
Copyright © 2005 Elsevier
Object-Oriented Programming
• We talked about data abstraction some back in
the unit on naming and scoping
• Recall that we traced the historical
development of abstraction mechanisms
– Static set of var Basic
– Locals Fortran
– Statics Fortran, Algol 60, C
– Modules Modula-2, Ada 83
– Module types Euclid
– Objects Smalltalk, C++, Eiffel,
Java, Oberon, Modula-3, Ada 95
Copyright © 2005 Elsevier
Object-Oriented Programming
• By deriving new classes
from old ones, the
programmer can create
arbitrarily deep class
hierarchies, with
additional functionality
at every level of the tree.
• The Smalltalk class
hierarchy for Smalltalk
has as many as seven
levels of derivation (see
attached Figure 9.2)
Copyright © 2005 Elsevier
Object-Oriented Programming
• Statics allow a subroutine to retain values
from one invocation to the next, while
hiding the name in-between
• Modules allow a collection of subroutines to
share some statics, still with hiding
– If you want to build an abstract data type,
though, you have to make the module a
manager
Copyright © 2005 Elsevier
Object-Oriented Programming
• Module types allow the module to be the
abstract data type - you can declare
a bunch of them
– This is generally more intuitive
• It avoids explicit object parameters to many
operations
• One minor drawback: If you have an operation
that needs to look at the innards of two different
types, you'd define both types in the same manager
module in Modula-2
• In C++ you need to make one of the classes (or
some of its members) "friends" of the other class
Copyright © 2005 Elsevier
Object-Oriented Programming
• Objects add inheritance and dynamic
method binding
• Simula 67 introduced these, but didn't have
data hiding
• The 3 key factors in OO programming
– Encapsulation (data hiding)
– Inheritance
– Dynamic method binding
Copyright © 2005 Elsevier
Encapsulation and Inheritance
• Visibility rules
– Public and Private parts of an object
declaration/definition
– 2 reasons to put things in the declaration
• so programmers can get at them
• so the compiler can understand them
– At the very least the compiler needs to know
the size of an object, even though the
programmer isn't allowed to get at many or
most of the fields (members) that contribute to
that size
• That's why private fields have to be in declaration
Copyright © 2005 Elsevier
Encapsulation and Inheritance
Classes (C++)
• C++ distinguishes among
– public class members
• accessible to anybody
– protected class members
• accessible to members of this or derived classes
– private
• accessible just to members of this class
• A C++ structure (struct) is simply a class
whose members are public by default
• C++ base classes can also be public, private,
or protected
Copyright © 2005 Elsevier
Encapsulation and Inheritance
Classes (C++)
• Example:
class circle : public shape { ...
anybody can convert (assign) a circle* into a shape*
class circle : protected shape
{ ...
only members and friends of circle or its derived classes
can convert (assign) a circle* into a shape*
class circle : private shape
{ ...
only members and friends of circle can convert (assign) a
circle* into a shape*
Copyright © 2005 Elsevier
Encapsulation and Inheritance
Classes (C++)
• Disadvantage of the module-as-manager approach:
include explicit create/initialize & destroy/finalize
routines for every abstraction
– Even w/o dynamic allocation inside module, users don't
have necessary knowledge to do initialization
– Ada 83 is a little better here: you can provide initializers
for pieces of private types, but this is NOT a general
approach
– Object-oriented languages often give you constructors
and maybe destructors
• Destructors are important primarily in the absence of garbage
collection
Copyright © 2005 Elsevier
Encapsulation and Inheritance
Classes (C++)
• A few C++ features you may not have learned:
– classes as members
foo::foo (args0) : member1
(args1), member2 (args2) { ...
args1 and args2 need to be specified in terms of
args0
• The reason these things end up in the header of foo is that
they get executed before foo's constructor does, and the
designers consider it good style to make that clear in the
header of foo::foo
Copyright © 2005 Elsevier
Encapsulation and Inheritance
Classes (C++)
• A few C++ features (2):
– initialization v. assignment
foo::operator=(&foo) v.
foo::foo(&foo)
foo b;
foo f = b;
// calls constructor
foo b, f;
// calls no-argument constructor
f = b;
// calls operator=
Copyright © 2005 Elsevier
Encapsulation and Inheritance
Classes (C++)
• A few C++ features (3):
– virtual functions (see the next dynamic method
binding section for details):
Key question: if child is derived from parent
and I have a parent* p (or a parent& p) that
points (refers) to an object that's actually a
child, what member function do I get when I
call p->f (p.f)?
• Normally I get p's f, because p's type is parent*.
• But if f is a virtual function, I get c's f.
Copyright © 2005 Elsevier
Encapsulation and Inheritance
Classes (C++)
• A few C++ features (4):
– virtual functions (continued)
• If a virtual function has a "0" body in the parent
class, then the function is said to be a pure virtual
function and the parent class is said to be abstract
• You can't declare objects of an abstract class; you
have to declare them to be of derived classes
• Moreover any derived class must provide a body for
the pure virtual function(s)
• multiple inheritance in Standard C++ (see next)
– friends
• functions
• classes
Copyright © 2005 Elsevier
Initialization and Finalization
• In Section 3.2, we defined the lifetime of an
object to be the interval during which it
occupies space and can hold data
– Most object-oriented languages provide some
sort of special mechanism to initialize an object
automatically at the beginning of its lifetime
• When written in the form of a subroutine, this
mechanism is known as a constructor
• A constructor does not allocate space
– A few languages provide a similar destructor
mechanism to finalize an object automatically at
the end of its lifetime
Copyright © 2005 Elsevier
Initialization and Finalization
Issues
• choosing a constructor
• references and values
– If variables are references, then every object must be
created explicitly - appropriate constructor is called
– If variables are values, then object creation can happen
implicitly as a result of elaboration
• execution order
– When an object of a derived class is created in C++, the
constructors for any base classes will be executed before
the constructor for the derived class
• garbage collection
Copyright © 2005 Elsevier
Dynamic Method Binding
• Virtual functions in C++ are an example of
dynamic method binding
– you don't know at compile time what type the
object referred to by a variable will be at run
time
• Simula also had virtual functions (all of
which are abstract)
• In Smalltalk, Eiffel, Modula-3, and Java all
member functions are virtual
Copyright © 2005 Elsevier
Dynamic Method Binding
• Note that inheritance does not obviate the
need for generics
– You might think: hey, I can define an abstract
list class and then derive int_list, person_list,
etc. from it, but the problem is you won't
be able to talk about the elements because you
won't know their types
– That's what generics are for: abstracting over
types
• Java doesn't have generics, but it does have
(checked) dynamic casts
Copyright © 2005 Elsevier
Dynamic Method Binding
• Data members of classes are implemented
just like structures (records)
– With (single) inheritance, derived classes have
extra fields at the end
– A pointer to the parent and a pointer to the
child contain the same address - the child just
knows that the struct goes farther than the
parent does
Copyright © 2005 Elsevier
Dynamic Method Binding
• Non-virtual functions require no space at run
time; the compiler just calls the appropriate
version, based on type of variable
– Member functions are passed an extra, hidden, initial
parameter: this (called current in Eiffel and self in
Smalltalk)
• C++ philosophy is to avoid run-time overhead
whenever possible(Sort of the legacy from C)
– Languages like Smalltalk have (much) more run-time
support
Copyright © 2005 Elsevier
Dynamic Method Binding
• Virtual functions are the only thing that requires
any trickiness (Figure 9.4)
– They are implemented by creating a dispatch table
(vtable) for the class and putting a pointer to that
table in the data of the object
– Objects of a derived class have a different dispatch
table (Figure 10.5)
• In the dispatch table, functions defined in the parent come
first, though some of the pointers point to overridden
versions
• You could put the whole dispatch table in the object itself
– That would save a little time, but potentially waste a LOT of
space
Copyright © 2005 Elsevier
Dynamic Method Binding
Copyright © 2005 Elsevier
Dynamic Method Binding
Copyright © 2005 Elsevier
Dynamic Method Binding
• Note that if you can query the type of an
object, then you need to be able to get from
the object to run-time type info
– The standard implementation technique is to
put a pointer to the type info at the beginning of
the vtable
– Of course you only have a vtable in C++ if your
class has virtual functions
• That's why you can't do a dynamic_cast on a pointer
whose static type doesn't have virtual functions
Copyright © 2005 Elsevier
Multiple Inheritance
• In C++, you can say
class professor : public
teacher, public researcher {
...
}
Here you get all the members of teacher
and all the members of researcher
– If there's anything that's in both (same name
and argument types), then calls to the member
are ambiguous; the compiler disallows them
Copyright © 2005 Elsevier
Multiple Inheritance
• You can of course create your own member in
the merged class
professor::print () {
teacher::print ();
researcher::print (); ...
}
Or you could get both:
professor::tprint () {
teacher::print ();
}
professor::rprint () {
researcher::print ();
}
Copyright © 2005 Elsevier
Multiple Inheritance
• Virtual base classes: In the usual case if you
inherit from two classes that are both
derived from some other class B, your
implementation includes two copies of B's
data members
• That's often fine, but other times you want a
single copy of B
– For that you make B a virtual base class
Copyright © 2005 Elsevier
Object-Oriented Programming
• Anthropomorphism is central to the OO
paradigm - you think in terms of real-world
objects that interact to get things done
• Many OO languages are strictly sequential, but
the model adapts well to parallelism as well
• Strict interpretation of the term
– uniform data abstraction - everything is an object
– inheritance
– dynamic method binding
Copyright © 2005 Elsevier
Object-Oriented Programming
• Lots of conflicting uses of the term out there
object-oriented style available in many
languages
– data abstraction crucial
– inheritance required by most users of the term O-O
– centrality of dynamic method binding a matter of
dispute
Copyright © 2005 Elsevier
Object-Oriented Programming
• SMALLTALK is the canonical object-
oriented language
– It has all three of the characteristics listed above
– It's based on the thesis work of Alan Kay at Utah
in the late 1960‘s
– It went through 5 generations at Xerox PARC,
where Kay worked after graduating
– Smalltalk-80 is the current standard
Copyright © 2005 Elsevier
Object-Oriented Programming
• Other languages are described in what
follows:
• Modula-3
– single inheritance
– all methods virtual
– no constructors or destructors
Copyright © 2005 Elsevier
Object-Oriented Programming
• Ada 95
– tagged types
– single inheritance
– no constructors or destructors
– class-wide parameters:
• methods static by default
• can define a parameter or pointer that grabs the object-
specific version of all methods
– base class doesn't have to decide what will be virtual
– notion of child packages as an alternative to
friends
Copyright © 2005 Elsevier
Object-Oriented Programming
• Java
– interfaces, mix-in inheritance
– alternative to multiple inheritance
• basically you inherit from one real parent and one or
more interfaces, each of which contains only virtual
functions and no data
• this avoids the contiguity issues in multiple inheritance
above, allowing a very simple implementation
– all methods virtual
Copyright © 2005 Elsevier
Object-Oriented Programming
• Is C++ object-oriented?
– Uses all the right buzzwords
– Has (multiple) inheritance and generics
(templates)
– Allows creation of user-defined classes that look
just like built-in ones
– Has all the low-level C stuff to escape the
paradigm
– Has friends
– Has static type checking
Copyright © 2005 Elsevier
Object-Oriented Programming
• In the same category of questions:
– Is Prolog a logic language?
– Is Common Lisp functional?
• However, to be more precise:
– Smalltalk is really pretty purely object-oriented
– Prolog is primarily logic-based
– Common Lisp is largely functional
– C++ can be used in an object-oriented style

More Related Content

What's hot

What's hot (20)

Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
 
Packages in java
Packages in javaPackages in java
Packages in java
 
List in java
List in javaList in java
List in java
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
 
Interface
InterfaceInterface
Interface
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Java threads
Java threadsJava threads
Java threads
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Java program structure
Java program structure Java program structure
Java program structure
 

Similar to Data abstraction and object orientation

Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not JavaChris Adamson
 
C++ Introduction brown bag
C++ Introduction brown bagC++ Introduction brown bag
C++ Introduction brown bagJacob Green
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxShaownRoy1
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Hemlathadhevi Annadhurai
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop Kumar
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSkillwise Group
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Vu Tran Lam
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
Drupalcon cph
Drupalcon cphDrupalcon cph
Drupalcon cphcyberswat
 
UsingCPP_for_Artist.ppt
UsingCPP_for_Artist.pptUsingCPP_for_Artist.ppt
UsingCPP_for_Artist.pptvinu28455
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesDigitalDsms
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csjaved75
 

Similar to Data abstraction and object orientation (20)

Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not Java
 
C++ Basics
C++ BasicsC++ Basics
C++ Basics
 
4-OOPS.pptx
4-OOPS.pptx4-OOPS.pptx
4-OOPS.pptx
 
C++ Introduction brown bag
C++ Introduction brown bagC++ Introduction brown bag
C++ Introduction brown bag
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 
gcdtmp
gcdtmpgcdtmp
gcdtmp
 
Drupalcon cph
Drupalcon cphDrupalcon cph
Drupalcon cph
 
UsingCPP_for_Artist.ppt
UsingCPP_for_Artist.pptUsingCPP_for_Artist.ppt
UsingCPP_for_Artist.ppt
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year cs
 

More from Hoang Nguyen

Rest api to integrate with your site
Rest api to integrate with your siteRest api to integrate with your site
Rest api to integrate with your siteHoang Nguyen
 
How to build a rest api
How to build a rest apiHow to build a rest api
How to build a rest apiHoang Nguyen
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsHoang Nguyen
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching worksHoang Nguyen
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cacheHoang Nguyen
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherenceHoang Nguyen
 
Python your new best friend
Python your new best friendPython your new best friend
Python your new best friendHoang Nguyen
 
Python language data types
Python language data typesPython language data types
Python language data typesHoang Nguyen
 
Programming for engineers in python
Programming for engineers in pythonProgramming for engineers in python
Programming for engineers in pythonHoang Nguyen
 
Extending burp with python
Extending burp with pythonExtending burp with python
Extending burp with pythonHoang Nguyen
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and pythonHoang Nguyen
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++Hoang Nguyen
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisHoang Nguyen
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsHoang Nguyen
 

More from Hoang Nguyen (20)

Rest api to integrate with your site
Rest api to integrate with your siteRest api to integrate with your site
Rest api to integrate with your site
 
How to build a rest api
How to build a rest apiHow to build a rest api
How to build a rest api
 
Api crash
Api crashApi crash
Api crash
 
Smm and caching
Smm and cachingSmm and caching
Smm and caching
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessors
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching works
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cache
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherence
 
Cache recap
Cache recapCache recap
Cache recap
 
Python your new best friend
Python your new best friendPython your new best friend
Python your new best friend
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python basics
Python basicsPython basics
Python basics
 
Programming for engineers in python
Programming for engineers in pythonProgramming for engineers in python
Programming for engineers in python
 
Learning python
Learning pythonLearning python
Learning python
 
Extending burp with python
Extending burp with pythonExtending burp with python
Extending burp with python
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and python
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Object model
Object modelObject model
Object model
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 

Recently uploaded

Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 

Recently uploaded (20)

Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 

Data abstraction and object orientation

  • 1. Copyright © 2005 Elsevier Object-Oriented Programming • Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it in a truly general form (Scheme comes close) • Data abstraction is somewhat newer, though its roots can be found in Simula67 – An Abstract Data Type is one that is defined in terms of the operations that it supports (i.e., that can be performed upon it) rather than in terms of its structure or implementation
  • 2. Copyright © 2005 Elsevier Object-Oriented Programming • Why abstractions? – easier to think about - hide what doesn't matter – protection - prevent access to things you shouldn't see – plug compatibility • replacement of pieces, often without recompilation, definitely without rewriting libraries • division of labor in software projects
  • 3. Copyright © 2005 Elsevier Object-Oriented Programming • We talked about data abstraction some back in the unit on naming and scoping • Recall that we traced the historical development of abstraction mechanisms – Static set of var Basic – Locals Fortran – Statics Fortran, Algol 60, C – Modules Modula-2, Ada 83 – Module types Euclid – Objects Smalltalk, C++, Eiffel, Java, Oberon, Modula-3, Ada 95
  • 4. Copyright © 2005 Elsevier Object-Oriented Programming • By deriving new classes from old ones, the programmer can create arbitrarily deep class hierarchies, with additional functionality at every level of the tree. • The Smalltalk class hierarchy for Smalltalk has as many as seven levels of derivation (see attached Figure 9.2)
  • 5. Copyright © 2005 Elsevier Object-Oriented Programming • Statics allow a subroutine to retain values from one invocation to the next, while hiding the name in-between • Modules allow a collection of subroutines to share some statics, still with hiding – If you want to build an abstract data type, though, you have to make the module a manager
  • 6. Copyright © 2005 Elsevier Object-Oriented Programming • Module types allow the module to be the abstract data type - you can declare a bunch of them – This is generally more intuitive • It avoids explicit object parameters to many operations • One minor drawback: If you have an operation that needs to look at the innards of two different types, you'd define both types in the same manager module in Modula-2 • In C++ you need to make one of the classes (or some of its members) "friends" of the other class
  • 7. Copyright © 2005 Elsevier Object-Oriented Programming • Objects add inheritance and dynamic method binding • Simula 67 introduced these, but didn't have data hiding • The 3 key factors in OO programming – Encapsulation (data hiding) – Inheritance – Dynamic method binding
  • 8. Copyright © 2005 Elsevier Encapsulation and Inheritance • Visibility rules – Public and Private parts of an object declaration/definition – 2 reasons to put things in the declaration • so programmers can get at them • so the compiler can understand them – At the very least the compiler needs to know the size of an object, even though the programmer isn't allowed to get at many or most of the fields (members) that contribute to that size • That's why private fields have to be in declaration
  • 9. Copyright © 2005 Elsevier Encapsulation and Inheritance Classes (C++) • C++ distinguishes among – public class members • accessible to anybody – protected class members • accessible to members of this or derived classes – private • accessible just to members of this class • A C++ structure (struct) is simply a class whose members are public by default • C++ base classes can also be public, private, or protected
  • 10. Copyright © 2005 Elsevier Encapsulation and Inheritance Classes (C++) • Example: class circle : public shape { ... anybody can convert (assign) a circle* into a shape* class circle : protected shape { ... only members and friends of circle or its derived classes can convert (assign) a circle* into a shape* class circle : private shape { ... only members and friends of circle can convert (assign) a circle* into a shape*
  • 11. Copyright © 2005 Elsevier Encapsulation and Inheritance Classes (C++) • Disadvantage of the module-as-manager approach: include explicit create/initialize & destroy/finalize routines for every abstraction – Even w/o dynamic allocation inside module, users don't have necessary knowledge to do initialization – Ada 83 is a little better here: you can provide initializers for pieces of private types, but this is NOT a general approach – Object-oriented languages often give you constructors and maybe destructors • Destructors are important primarily in the absence of garbage collection
  • 12. Copyright © 2005 Elsevier Encapsulation and Inheritance Classes (C++) • A few C++ features you may not have learned: – classes as members foo::foo (args0) : member1 (args1), member2 (args2) { ... args1 and args2 need to be specified in terms of args0 • The reason these things end up in the header of foo is that they get executed before foo's constructor does, and the designers consider it good style to make that clear in the header of foo::foo
  • 13. Copyright © 2005 Elsevier Encapsulation and Inheritance Classes (C++) • A few C++ features (2): – initialization v. assignment foo::operator=(&foo) v. foo::foo(&foo) foo b; foo f = b; // calls constructor foo b, f; // calls no-argument constructor f = b; // calls operator=
  • 14. Copyright © 2005 Elsevier Encapsulation and Inheritance Classes (C++) • A few C++ features (3): – virtual functions (see the next dynamic method binding section for details): Key question: if child is derived from parent and I have a parent* p (or a parent& p) that points (refers) to an object that's actually a child, what member function do I get when I call p->f (p.f)? • Normally I get p's f, because p's type is parent*. • But if f is a virtual function, I get c's f.
  • 15. Copyright © 2005 Elsevier Encapsulation and Inheritance Classes (C++) • A few C++ features (4): – virtual functions (continued) • If a virtual function has a "0" body in the parent class, then the function is said to be a pure virtual function and the parent class is said to be abstract • You can't declare objects of an abstract class; you have to declare them to be of derived classes • Moreover any derived class must provide a body for the pure virtual function(s) • multiple inheritance in Standard C++ (see next) – friends • functions • classes
  • 16. Copyright © 2005 Elsevier Initialization and Finalization • In Section 3.2, we defined the lifetime of an object to be the interval during which it occupies space and can hold data – Most object-oriented languages provide some sort of special mechanism to initialize an object automatically at the beginning of its lifetime • When written in the form of a subroutine, this mechanism is known as a constructor • A constructor does not allocate space – A few languages provide a similar destructor mechanism to finalize an object automatically at the end of its lifetime
  • 17. Copyright © 2005 Elsevier Initialization and Finalization Issues • choosing a constructor • references and values – If variables are references, then every object must be created explicitly - appropriate constructor is called – If variables are values, then object creation can happen implicitly as a result of elaboration • execution order – When an object of a derived class is created in C++, the constructors for any base classes will be executed before the constructor for the derived class • garbage collection
  • 18. Copyright © 2005 Elsevier Dynamic Method Binding • Virtual functions in C++ are an example of dynamic method binding – you don't know at compile time what type the object referred to by a variable will be at run time • Simula also had virtual functions (all of which are abstract) • In Smalltalk, Eiffel, Modula-3, and Java all member functions are virtual
  • 19. Copyright © 2005 Elsevier Dynamic Method Binding • Note that inheritance does not obviate the need for generics – You might think: hey, I can define an abstract list class and then derive int_list, person_list, etc. from it, but the problem is you won't be able to talk about the elements because you won't know their types – That's what generics are for: abstracting over types • Java doesn't have generics, but it does have (checked) dynamic casts
  • 20. Copyright © 2005 Elsevier Dynamic Method Binding • Data members of classes are implemented just like structures (records) – With (single) inheritance, derived classes have extra fields at the end – A pointer to the parent and a pointer to the child contain the same address - the child just knows that the struct goes farther than the parent does
  • 21. Copyright © 2005 Elsevier Dynamic Method Binding • Non-virtual functions require no space at run time; the compiler just calls the appropriate version, based on type of variable – Member functions are passed an extra, hidden, initial parameter: this (called current in Eiffel and self in Smalltalk) • C++ philosophy is to avoid run-time overhead whenever possible(Sort of the legacy from C) – Languages like Smalltalk have (much) more run-time support
  • 22. Copyright © 2005 Elsevier Dynamic Method Binding • Virtual functions are the only thing that requires any trickiness (Figure 9.4) – They are implemented by creating a dispatch table (vtable) for the class and putting a pointer to that table in the data of the object – Objects of a derived class have a different dispatch table (Figure 10.5) • In the dispatch table, functions defined in the parent come first, though some of the pointers point to overridden versions • You could put the whole dispatch table in the object itself – That would save a little time, but potentially waste a LOT of space
  • 23. Copyright © 2005 Elsevier Dynamic Method Binding
  • 24. Copyright © 2005 Elsevier Dynamic Method Binding
  • 25. Copyright © 2005 Elsevier Dynamic Method Binding • Note that if you can query the type of an object, then you need to be able to get from the object to run-time type info – The standard implementation technique is to put a pointer to the type info at the beginning of the vtable – Of course you only have a vtable in C++ if your class has virtual functions • That's why you can't do a dynamic_cast on a pointer whose static type doesn't have virtual functions
  • 26. Copyright © 2005 Elsevier Multiple Inheritance • In C++, you can say class professor : public teacher, public researcher { ... } Here you get all the members of teacher and all the members of researcher – If there's anything that's in both (same name and argument types), then calls to the member are ambiguous; the compiler disallows them
  • 27. Copyright © 2005 Elsevier Multiple Inheritance • You can of course create your own member in the merged class professor::print () { teacher::print (); researcher::print (); ... } Or you could get both: professor::tprint () { teacher::print (); } professor::rprint () { researcher::print (); }
  • 28. Copyright © 2005 Elsevier Multiple Inheritance • Virtual base classes: In the usual case if you inherit from two classes that are both derived from some other class B, your implementation includes two copies of B's data members • That's often fine, but other times you want a single copy of B – For that you make B a virtual base class
  • 29. Copyright © 2005 Elsevier Object-Oriented Programming • Anthropomorphism is central to the OO paradigm - you think in terms of real-world objects that interact to get things done • Many OO languages are strictly sequential, but the model adapts well to parallelism as well • Strict interpretation of the term – uniform data abstraction - everything is an object – inheritance – dynamic method binding
  • 30. Copyright © 2005 Elsevier Object-Oriented Programming • Lots of conflicting uses of the term out there object-oriented style available in many languages – data abstraction crucial – inheritance required by most users of the term O-O – centrality of dynamic method binding a matter of dispute
  • 31. Copyright © 2005 Elsevier Object-Oriented Programming • SMALLTALK is the canonical object- oriented language – It has all three of the characteristics listed above – It's based on the thesis work of Alan Kay at Utah in the late 1960‘s – It went through 5 generations at Xerox PARC, where Kay worked after graduating – Smalltalk-80 is the current standard
  • 32. Copyright © 2005 Elsevier Object-Oriented Programming • Other languages are described in what follows: • Modula-3 – single inheritance – all methods virtual – no constructors or destructors
  • 33. Copyright © 2005 Elsevier Object-Oriented Programming • Ada 95 – tagged types – single inheritance – no constructors or destructors – class-wide parameters: • methods static by default • can define a parameter or pointer that grabs the object- specific version of all methods – base class doesn't have to decide what will be virtual – notion of child packages as an alternative to friends
  • 34. Copyright © 2005 Elsevier Object-Oriented Programming • Java – interfaces, mix-in inheritance – alternative to multiple inheritance • basically you inherit from one real parent and one or more interfaces, each of which contains only virtual functions and no data • this avoids the contiguity issues in multiple inheritance above, allowing a very simple implementation – all methods virtual
  • 35. Copyright © 2005 Elsevier Object-Oriented Programming • Is C++ object-oriented? – Uses all the right buzzwords – Has (multiple) inheritance and generics (templates) – Allows creation of user-defined classes that look just like built-in ones – Has all the low-level C stuff to escape the paradigm – Has friends – Has static type checking
  • 36. Copyright © 2005 Elsevier Object-Oriented Programming • In the same category of questions: – Is Prolog a logic language? – Is Common Lisp functional? • However, to be more precise: – Smalltalk is really pretty purely object-oriented – Prolog is primarily logic-based – Common Lisp is largely functional – C++ can be used in an object-oriented style