CS 4773 Object Oriented Systems
Object Oriented Programming Concepts as Applied to Java
Previous Topic: The Java Language
Does the Language Make A difference?
Attributes of Complex Systems
Decomposition
What is Object Oriented Programming?
OOP Concepts
Responsibilities
Encapsulation
Classes and Instances
Class Hierarchies and Inheritance
Uses of Inheritance
Replacement
Refinement
Assignment
Equality
Polymorphism
Next Topic: Applets
This discussion is taken from
- An Introduction to Object-Oriented Programming by Timothy Budd
- Object Oriented Design with Applications by Grady Booch
Does the Language Make a Difference?
In Eskimo languages there are many words to describe different types of snow
(wet, fluffy, heavy, etc).
Does this mean that snow cannot be adequately described in English?
Suppose that in an Eskimo language there was no general word for snow,
just words for specific forms of snow.
Does this mean you could not express the concept: It is snowing?
Are there any advantages to a language that forces you to be specific
rather than general?
Do these languages make you think in different ways?
Two views
Church's Conjecture: Any computation for which there exists an
effective procedure can be realized by a Turing machine.
Sapir-Whorf hypothesis:It may be possible for an individual
working in one language to imagine thoughts that cannot be expressed in
another language, and cannot be understood by those using this other language.
It is possible for both of these to be correct.
Which ever is correct it is certainly true that the language we use
influences the way we think.
Object-oriented programming is a programming paradigm: an model or example
of how programs should be designed.
The paradigm is important to the design of complex systems.
Attributes of Complex Systems
- Complexity often takes the form of a hierarchy
- The choice of which components are primitive is relatively arbitrary and
largely at the discretion of the observer.
- Intracomponent linkages are generally stronger than intercomponent linkages.
- Hierarchic systems are usually composed of only a few different kinds of
subsystems.
- A complex system that works is usually one that has evolved from a simple
system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
Decomposition
The method of divide and conquer has been known since ancient times.
- Algorithmic Decomposition: also called top-down design or structured design
- Data-driven design: mapping system inputs to outputs
- Object-Oriented Decomposition
- Decompose into objects.
- An object is a tangible entity that exhibits some well-defined behavior.
- We ask objects to do things by sending them messages.
What is Object Oriented Programming?
Object-oriented programming is a method of implementation in which programs
are organized as cooperative collections of objects, each of which
represents an instance of some class, and whose classes are all members
of a hierarchy of classes united via inheritance relationships.
OOP Concepts
- Responsibilities
- Describe behavior in terms of an object in terms of
what should be accomplished, rather than
the method to accomplish it.
- This makes the description independent of the implementation
- Gives more flexibility in design
- An object can be thought of as an agent responsible for
accomplishing a goal.
- Communication with objects is through messages
- In Java, messages are sent by calling methods of the object
- Encapsulation
- The internal structure (implementation) should be hidden from the user
(as above).
- The internal structure should also be protected from the misuser.
- Parnas's principles: (as quoted in Budd's book)
- The developer of a software component must provide the intended
user with all of the information needed to make effective use of the
services provided by the component, and should provide no
other information.
- The developer of a software component must be provided with all
the information necessary to carry out the given responsibilities
assigned to the component, and should be provided with no
other information.
- Classes and Instances
- An object is an instance of a class, just as 3 is an instance of an
integer.
- A class defines the behavior of the objects in the class.
- In Java, variables can be either instance variables (one for each
instance) or class variables (shared by all instances).
Similarly for methods.
- Class Hierarchies and Inheritance
- In Java, a subclass is produced using extends
- If A extends B, A is a subclass of B and B is a superclass of A.
- A inherits the variables and methods of B
- A can override methods of B
- Uses of Inheritance
- Subclassing for Specialization (Subtyping)
- The new class is a specialized form of the parent and satisfies
all of the specifications of the parent.
- Satisfies the Principle of Substitutability:
If B is a subclass of A, it should be possible to substitute
instances of class B for instances of class A in any situation
with no observable effect.
- Example: Window and TextWindow.
Window operations: move, resize, iconify
- Subclassing for Specification
- The parent class may have some operations (methods) that are deferred,
or incompletely specified.
- The subclass must completely specify these.
- In Java this is done with implements on an interface
or extends on an abstract class.
- Example: implements Runnable or implements ActionListener
- Subclassing for Construction
- The subclass has new behavior (methods) which use the methods of
the parent, but the methods of the parent are not part of
the specifications of the subclass.
- Example: a set may be implemented as a subclass of a list.
The set does not have the ordering property of the list.
- Subclassing for Generalization
- The behavior of the parent is modified to allow a more
general behavior.
- At least one method of the parent must be overridden.
- Example: A ColoredWindow generalizes a Window which might always
have a white background.
- In theory you should avoid this and have Window as a subclass of
ColoredWindow, but this is not always possible.
- Subclassing for Maintenance (I made this one up)
- Similar to Generalization, but the purpose is to fix a bug
in the parent class.
- A broken method of the parent is overridden and replaced by
a fixed one.
- Subclassing for Extension
- The parent methods are left unchanged.
- Additional methods are added for increased functionality.
- Example: A StringSet extends Set to provide string operations.
- Example: Debugging methods may be added to show internal structure.
- Subclassing for Limitation
- Some of the operations on the parent are limited.
- Example: A stack may be a subclass of a double-ended queue.
- Subclassing for Variance
- Two or more classes have much in common but one is not
logically a subclass of the other.
- One is logically chosen as the parent and the other as the child.
- Example: Mouse and Graphics Tablet.
- Should be avoided with the common parts put in an abstract class.
- Subclassing for Combination
- This is usually called multiple inheritance
- In Java a class can only extend a single class
- Java supports this using implements
- Replacement
- Replacement refers to the replacement of a method from the
parent in a subclass.
- When a message is sent to an object, the search for a matching object
begins with the examination of the methods associated with the
class of the object.
- If no method is found, the methods associated with the immediate
parent class of the object are examined.
- If no matching method is found, the immediate parent of that class
is searched.
- This continues until a match is found or no parent exists.
- A method in a class that has the same name as a method in a
superclass is said to override the method in the parent class.
- In Java, the overriding method must have the same parameters
types and return value type as the overridden method.
- In Java it is also possible to override data fields.
- Refinement
- Refinement refers to supplementing the behavior of the parent
class so that the actions described by the code in the child
class are combined with the actions described by the parent class.
- This is often useful during the initialization of the new object.
- In Java this is accomplished by using super.
- Sending a message to super indicates that the search for the associated
method should begin with the parent class to the current class rather
than the current class.
- Assignment
- There are three possible interpretations of assignment
- Copy semantics: Assignment copies the entire value of the right
side, assigning it to the left side.
The two values are
independent and changes to one do not affect the other.
- Pointer semantics: Assignment changes the reference of the left side
to be the right side.
Also called pointer assignment
The two variables not only have the same value but also refer to
the same object.
- Copy on Write: Use pointer semantics for assignment but convert
the value into a new structure if it is ever modified.
- Java uses copy semantics for primitive types and pointer semantics
for everything else.
- In Java you can simulate copy semantics by using new or
clone.
- Equality
- There are two possible interpretations of equality
- identity: Also called pointer equivalence
The pointers refer to the same objects
- member equality: Also called structural equivalence
The members of the objects are the same
- Java uses identity for non-primitive types.
- Many classes have an equals method which tests for member equality.
- Polymorphism
- A polymorphic object is any entity, such as a variable or function
argument, that is permitted to hold values of different types during
the course of execution.
- Forms of polymorphism
- Polymorphic variables can hold values of different types.
In Java, a variable which is not of primitive type can hold
any subclass of the declared type.
- Overloading refers to 2 or more function bodies associated with the
same name.
Java does not allow user overloading of operators, but does
allow Parametric Overloading:
methods of the same name can have different number and types of
arguments.
- Templates refers to a way of parameterizing a class or function
by use of a type.
Java does not support this.
- Overriding: Java allows for overriding methods of a parent class.
- Deferred methods:In Java your extend abstract classes and implement
interfaces for this.
- In Java, the word final can be used to prevent overriding.