Session 9 : Object-Oriented Programming

There are many object-oriented programming language...
- Some are support procedural and data-oriented programming (e.g., Ada 95+ and C++)
- Some are support functional program (e.g., CLOS)
- Newer languages do not support other paradigms but use their imperative structures (e.g., Java and C#)
- Some are pure OOP language (e.g., Smalltalk & Ruby)
- Some are functional languages support OOP, but they are not discussed in this chapter
there are three major language features :
1. abstract data types
2. inheritance
3. polymorphism
- Inheritance allows new classes defined  in terms of existing ones, i.e., by allowing them to inherit common parts
- Inheritance addresses both of the above concerns--reuse ADTs after minor changes and define classes in a hierarchy
to make it easily, inheritance can be imagined as parent and child. The parent is the class while  the child is a derived class or a subclass.
There are several ways a derived class can differ from its parent.1 Following are the most common differences between a parent class and its subclasses:
1. 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.
2. The subclass can add variables and/or methods to those inherited from the parent class.
3. The subclass can modify the behavior of one or more of its inherited methods. A modified method has the same name, and often the same protocol, as the one of which it is a modification.

Object-Oriented Concepts
* 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 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 have two parts--a method name and the destination object
* In the simplest case, a class inherits all of the entities of its parentThere 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

Dynamic binding
The third characteristic (after abstract data types and inheritance) of objectoriented programming languages is a kind of polymorphism provided by the dynamic binding of messages to method definitions. This is sometimes called dynamic dispatch.
- 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
- One purpose of dynamic binding is to allow software systems to be more easily extended during both development and maintenance.
dynamic binding concepts
* 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 virtual method
* An abstract class cannot be instantiated

Design Issues for OOP Languages
* The Exclusivity of Objects
- Everything is an object
Advantage - elegance and purity
Disadvantage - slow operations on simple objects
- Add objects to a complete typing system
Advantage - fast operations on simple objects
Disadvantage - results in a confusing type system (two kinds of entities)
- Include an imperative-style typing system for primitives but make everything else objects
Advantage - fast operations on simple objects and a relatively small typing system
Disadvantage - still some confusion because of the two type systems
* Are Subclasses Subtypes?
A subclass is a subtype if it has an is-a relationship with its parent class
* Single and Multiple Inheritance
- 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:
- Sometimes it is quite convenient and valuable
* Allocation and DeAllocation of Objects
- Allocated from the run-time stack
- Explicitly create on the heap (via new)
- Simplifies assignment - dereferencing can be implicit
- Is deallocation explicit or implicit?
* Dynamic and Static Binding
* Nested classes
* Initialization of Objects
Objects are initialized in implicit or explicit initialization.

Komentar