Object-Oriented Design

Download Report

Transcript Object-Oriented Design

Object-Oriented Design
Chapter 2
Objectives
Review core concepts underlying objectoriented programming
 Review how these concepts are
accomplished in a Java program
 Discuss the use of generic types to define
collection classes

Object-Orientation
An object is a fundamental entity in a Java
program.
 Java programs also manage primitive
data.
 Primitive data includes common,
fundamental values such as numbers and
characters.
 An object usually represents something
more sophisticated than primitive data.

OO Orientation
An object is defined by a class.
 A class can be thought of as the data type
of the object.


A data type is the values and operations on
those values
Objects manages and protects their own
data, this is an example of encapsulation.
 To request an object to perform an action,
you send the object a message

More OO
Classes can be inherited from other
classes, this is a form of reuse.
 Using inheritance you can create class
hierarchies.
 Classes, objects, encapsulation, and
inheritance are the primary ideas that
make up the world of oo software.

Using Objects
An object is an abstraction
 An abstraction hides the precise details of
how it works.
 In fact, those details are irrelevant from the
point of view of the client
 Abstraction is one of the most powerful
ideas of OO design

Class Libraries and Packages
A class library is a set of classes that
supports development of programs.
 Java has a very rich set of class libraries.
 Sometimes these are called the Java APIs
 http://java.sun.com/j2se/1.5.0/docs/api/
 The classes in the library are organized
into packages.

Packages
Packages group logically related classes
into a compilation unit
 Package names have to correspond to a
directory structure on your computer
 Web-CAT will want your package names
to be of a form like this:
edu.vt.cs.cs1706.projectname
 I’ll probably go with cs1706.projectname

State and Behavior
An object has a set of attributes which
define the object state of being.
 The set of attributes an object will have will
depend on context.
 An object representing a ball in a game
will different set of attributes from a ball in
an inventory system.

Encapsulation
Objects should be encapsulated.
 The rest of the program should interact
with an object only through a well-defined
interface.
 Visibility modifiers allow us to implement
encapsulation in Java
 Public, private and protected

Effects of Public and Private
Visibility
Public
Private
Variables Violates
encapsulation
Enforces
encapsulation
Methods Provides
services to
clients
Supports other
methods in the
class
References Revisited
Objects are stored as references
 A reference variable that does currently
point to an object is a null reference
 The this reference is a special reference
that refers to the currently executing object
 An alias occurs when two object
references refer to the same object.
 This should be avoided, but isn’t always a
bad thing

Interfaces
An interface is a collection of constants
and abstract methods.
 Some examples are:

Comparable
 Iterator
 Comparator
 Clonable

Inheritance
Private data is private even in an
inheritance hierarchy
 A way to allow the subclass to have
access to the private data of the parent
class is to declare the fields as protected.
 A subclass can invoke methods of the
parent class through the use of the super
keyword.

Polymorphism





Polymorphism is the ability for a software system
to decide at runtime which method is invoked.
This is sometimes called dynamic dispatch.
You need an inheritance hierarchy.
The subclass must overload at least one
method.
You can then declare an object of the super
class and have it refer to the subclass and the
subclass method will be invoked.
Generic Types
A generic type allows us to define a class
so that it stores, operates on, and
manages objects whose types is not
specified until the class is instantiated.
class Box<T>
{
}
 T is a placeholder to allow us to define
the class.
