Download Understanding OOP: Abstraction, Encapsulation, Inheritance, Polymorphism and more Summaries Object Oriented Programming in PDF only on Docsity!
Introduction toObject Oriented
Programming
CMSC 331
Concept of Abstraction
- āAn abstraction is a view or representation
of an entity that includes only the attributesof significance in a particular context.ā~Sebesta, pg. 434ā Process abstractionā Data abstraction
Encapsulation
- Beyond a certain size, process-abstraction
is no longer sufficient to organize largeprograms
- Modularization ā the organization of code
into logically related processes and data
- Encapsulation ā organization of ⦠into a
separately or independently compilableunit
Data Abstraction
- Abstract Data Type (ADT) is an
encapsulation containing the datarepresentation and methods for
one
specific data type.
Data Abstraction: Formally
-^
An abstract data type is a data type that satisfiesthe following two conditions:ā The representation, or definition, of the type and the
operations on objects of the type are contained in asingle syntactic unit. Also, other program units maybe allowed to create variables of the defined type.
- The representation of objects of the type is hidden
from the program units that use the type, so the onlydirect operations possible on those objects are thoseprovided in the typeās definition.
Advantages
- Helps to organize program into logical
units.
- Reduces code interdependencies by
creating interface āboundariesā āenhancing modification and reuse.
- Improves reliability by keeping details
āhiddenā.
Example
Design Issues
- Facilities must be present for
encapsulation of type definition andmethods
- Basic objects should have minimal
definitions
- Some entities will be common to most
objects
Inheritance: Benefits
- Facilitates reuse⢠Allows structuring of Classes into logical
hierarchies
Polymorphism
- Polymorphic variables can hold a value of
more than one typeā e.g. if B is a derived Class of Class A, and
variable x has type A, x can hold a value oftype B as well
Polymorphism: Benefits
- Allows for more flexible code⢠Supports operations on generic objects⢠Provides for use of virtual classes⢠Drawback: Does make type checking
more difficult; requires more caution atruntime
Application of OO Language
-^
āAn executing program in an object-orientedlanguage can be described as a simulation of acollection of computers that communicate witheach other through messages.ā (Sebesta p. 463)
-^
Invoking the method of a class is āsending amessageā
-^
Collection of methods define a āmessageprotocolā
-^
Identify the objects and interactions in yourproblem, and simulate them
Design Issues
- Exclusivity of Objects⢠Are Subclasses Subtypes?⢠Implementation and Interface Inheritance⢠Type Checking and Polymorphism⢠Single and Multiple Inheritance⢠Allocation and Deallocation of Objects⢠Dynamic and Static Binding
Issues: Exclusivity of Objects
- In purest form, āeverythingā is an object
- Benefit of elegance in simplicityā Drawback in that message interface is slower
- Compromises:
- Retain a complete, imperative typing model,
add object model
- Retain imperative model only for primitive,
scalar types
Issues: Subclass = Subtype?
- Generally, this reduces to the question of
whether a subclass inherits all entities ofthe parent classā Where entities are overriden, they should be
compatible
Issues: Inheritance
- Interface Inheritance
- Only the interface of the superclass is seen by
the subclass
- Implementation Inheritance
- The implementation details of the superclass
are available to the subclass
Summary
- OO = ADT + Inheritance + Dynamic
Binding
- OO Languages support this with Classes,
methods, objects, and message passing
- Some languages, like C++, are hybrid⢠Others, like Java support only OO
A Few Words on Javaā¦
CMSC 331
Java isā¦
- Object oriented!⢠Portable!⢠The language of the WWW!
OO
- The Java programming language provides
support for:ā Abstract Data Types (classes)ā Inheritance (single, with some extensions)ā Polymorphism
Portability
- Java is a hybrid language
byte code
- Byte code is interpreted by a Java Virtual
Machine (JVM)
- JVM has been ported to many platforms⢠Byte code for a Java program should run
on any JVM
- Programs are not 100% portable
Memory Management
- JVM implements garbage collection
- No explicit deallocation of objectsā Unreferenced objects are periodically
recycled automatically
Self-Documenting Code
- Java supports the writing of self-
documenting code:ā A simple markup language for use in source
code, which allows embedded html
- A documentation compiler, ājavadocā, which
creates easy to read html Classdocumentation.
Everything is a Class
- From basic types, to programs⢠Language contains certain primitive types
- int, doubleā Arraysā ā¦
- Primitive data types have analogs in the
Class hierarchyā Integer, Double, ā¦
Exception Handling
- Error conditions generate
exceptions
- Like everything else in Java, exceptions
are Classesā Predefinedā User defined
thrown
by a method, or
caught
and
handled
Security
- Execution inside a virtual machine makes
it possible to tightly control access tomachine resourcesā Diskā Networkā ā¦
- There are facilities for digitally signing
code
Jar
- Classes can be bundled together in jar
files
- Jar works just like tar (Unix)⢠Jar files can be used as libraries
Threading
- Java provides good support for writing
multi-threaded programs, and managingconcurrency.ā Thread Classā Low-level synchronization primitives
Memory? What Memory?
- No pointers⢠No pointer arithmetic⢠No memory hassles⢠Only
references
to Class instances