1/67
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
The major language features of OOP:
abstract data types, inheritance, and polymorphism
With inheritance, productivity increases can come from
reuse
Inheritance allows new classes to be
defined in terms of existing ones.
inheritance addresses both of these concerns:
reuse ADTs after minor changes and define classes in a hierachy
ADTs are usually called
classes
Class instances are called
objects
a class that inherits is a
derived class or a subclass
The class from which one another class inherits is a
parent class or superclass
Subprograms that define operations on objects are called
methods
Calls to methods are called
messages
The entire collection of methods of an object is called its
message protocol or message interface
Messages of two parts
a method name and the destination object
In the simplest case, a class inherits
all of the entities of its parent
Inheritance can be complicated by access controls to encapsulated entities. Examples of this are:
A class can hide entities from its subclasses
A class can hide entities from its clients
A class can also hide entities for its clients while allowing its subclasses to
see them
Besides inheriting methods as is, a class can modify an inherited method by:
The new one overrides the inherited one
The method in the parent is overriden
Three ways a class can differ from its parent:
The subclass can add variables and/or methods to those inherited from the
parent
The subclass can modify the behavior of one or more of its inherited methods.
The parent class can define some of its variables or methods to have private
access, which means they will not be visible in the subclass
There are two kinds of variables in a class:
class variables (one/class)
instance variables (one/object)
There are two kinds of methods in a class:
Class methods - accept messages to the class
instance methods - accept messages to objects
One disadvantage of inheritance for reuse:
creates interdependencies among classes that complicate maintenance
a polymorphic variable can be defined in a class that is able to
reference (or point to) objects of the class and objects of any of its descendants
When a class hierarchy includes classes that override methods and such methods are called through a polymorphic variable…
the binding to the correct method will be dynamic
dynamic binding allows software systems to be more
easily extended during both development and maintenance
An abstract method is
one that does not include a definition (it only defines a protocol)
an abstract class is
one that includes at least one abstract method
an abstract class
cannot be instantiated
What are the design issues for object-oriented programming languages?
The Exclusivity of Objects
Are Subclasses Subtypes?
Single and Multiple Inheritance
Object Allocation and Deallocation
Dynamic and Static Binding
Nested Classes
Initialization of Objects
With the exclusivity of objects
everything is an object
it adds objects to a complete typing system
Includes an imperative-style typing system for primitives but makes everything else objects.
if a derived class is-a parent class, then
objects of the derived class must behave the same as the parent class object
A derived class is a subtype if it has
an is-a relationship with its parent class
subclass can only do what?
add variables and methods and override inherited methods in “compatible” ways
Subclasses inherit implementation; subtypes inherit
interface and behavior
Multiple inheritance allows
a new class to inherit from two or more classes
Disadvantages of multiple inheritance:
Language and implementation complexity (in part due to name collisions)
Potential inefficiency - dynamic binding costs more with multiple inheritance (but not much)
Advantage of multiple inheritance
sometimes it is quite convenient and valuable
If objects behave like ADTs, they can be allocated from
anywhere (run-time stack or explicitly create on the heap)
if the objects are all heap-dynamic, references can be uniform through
a pointer reference variable
if objects are stack dynamic, there is a problem with regard to
subtypes - object slicing
what is the use for scope resolution operator ( :: )?
a member that is not accessible in a subclass (due to private derivation) can be declared to be visible using this.
smalltalk is a pure
OOP language
In SmallTalk
everything is an object
all objects have local memory
all computation is through objects sending messages to objects
none of the appearances of imperative languages
all objected are allocated from the heap
all deallocation is implicit
Smalltalk classes cannot be
nested in other classes
SmallTalk Inheritance
a subclass inherits all of the instance variables, instance methods, and class methods of its superclass
all subclasses are subtypes
all inheritance is implementation inheritance
no multiple inheritance
SmallTalk Dynamic Binding
all binding of messages to methods is dynamic.
Process of binding messages to methods for SmallTalk
search the object to which the message is sent for the method. if not found, search the superclass etc. up to the system class which has no superclass
Evaluation of Smalltalk
The syntax of the language is simple and regular
Good example of power provided by a small language
Slow compared with conventional compiled imperative languages
Dynamic binding allows type errors to go undetected until run time
Introduced the graphical user interface
Greatest impact: advancement of OOP
Support for OOP in C++ Inheritance
A class need not be the subclass of any class
Access controls for members are private, public, and protected
Access controls: Private
visible only in the class and friends) (disallows subclasses from being subtypes
Access controls: Public
visible in subclasses and clients
Access controls: Protected
visible in the class and in subclasses, but not clients
Private derivation:
inherited public and protected members are private in the subclasses
Public derivation
public and protected members are also public and protected in subclasses
An abstraction is
a view or representation of an entity that includes only the most significant attributes
An abstract data type is
is a user-defined data type that satisfies two conditions
Two conditions for abstract data types
The representation of objects of the type is hidden from the program
units that use these objects
The declarations of the type and the protocols of the operations on objects of the type are contained in a single syntactic unit. Other program units are allowed to create variables of the defined type
Advantages of Data Abstraction: First condition
Reliability
Reduces the range of code and variables of which the programmer must be aware
Name conflicts are less likely
Advantages of Data Abstraction: Second condition
Provides a method of program organization
Aids modifiability (everything associated with a data structure is together), and
Separate compilation
Language requirements for ADTs
A syntactic unit in which to encapsulate the type definition
A method of making type names and subprogram headers visible to
clients, while hiding actual definitions
Some primitive operations must be built into the language processor
ADTs in C++
The class is the encapsulation device
A class is a type
All of the class instances of a class share a single copy of the
member functions
Each instance of a class has its own copy of the class data members
Instances can be static, stack dynamic, or heap dynamic
Information Hiding: Private Clause
for hidden entities
Information Hiding: Public Clause
for interface entities
Information Hiding: Protected Clause
for inheritance
Friend functions or classes
to provide access to private members to some unrelated units or functions
Parameterized ADTs allow
designing an ADT that can store any type elements – only an issue for static typed languages (also known as generic classes)
Encapsulation Constructs
a grouping of subprograms that are logically related into a unit that can be separately compiled (compilation units)
A naming encapsulation is used to
create a new scope for names
C++ Namespaces
Can place each library in its own namespace and qualify names used outsid with the namespace
C# also includes namespaces
Java Packages
Packages can contain more than one class definition; classes in a package are partial friends
Clients of a package can use fully qualified name or use the import declaration
Ruby Modules (Naming Encapsulations)
Ruby classes are name encapsulations, but Ruby
also has modules
Typically encapsulate collections of constants and
methods
Modules cannot be instantiated or subclassed, and
they cannot define variables
Methods defined in a module must include the
module’s name
Access to the contents of a module is requested
with the require method