You are on page 1of 4

OBJECT ORIENTED PROGRAMMING

Procedural vs. Object-Oriented Programming

• In procedural programming, the data is separated from the procedures and often the data is
global, so it is easy to modify data that is outside your scope
• In OO programming, objects combine data and behavior:
o They contain data and methods (OO language for functions or procedures)
o Combining data and methods in an object is called encapsulation
 In good O-O design, an object should only reveal the interfaces needed to
interact with it. Details not pertinent to the use of the object should be hidden
from other objects
o You can control access to members of an object
o Some members can be hidden from other objects
o OO programming allows the coder to define relationships between classes that
facilitate code reuse and overall better design, by organizing classes and factoring in
commonalities of various classes.

What is an Object?

• An object is a manifestation of a particular type of combination of data and methods


• The data stored within an object represents the state of an object; the data is referred to as
the object’s attributes
• The behavior of an object is what an object can do
o Behaviors are contained in methods
o You invoke a method by sending a message to it including the following information:
 The name of the method
 The parameters passed to the method
 The return type of the method
o A method may implement behaviors that are called from other objects or provide
internal behavior of the class
o Internal methods are not accessible by other objects
• Messages are the communications mechanism between objects
• The interface is the fundamental means of communications between objects
o Class design specifies the interfaces for the proper instantiation and operation of
objects
o Any behavior that the object provides must be invoked by a message sent using one
of the provided interfaces
o Only considers public attributes and methods
• The implementation is private. It is the way in which the object privately executes its methods
• A class is a template from which objects are made; when an object is created, we say that it
is instantiated

UML Class Diagrams

• Contains attributes and methods for a particular class, detailing which is public and which is
private
Employee Name

-SocialSecurityNumber: String
-Gender: Boolean Data
-DateOfBirth: Date

+getSocialSecurityNumber: String
+getGender: Boolean
+getDateOfBirth: Date Method
+getSocialSecurityNumber: void
+getGender: void
+getDateOfBirth: void

What is a Class?

• A class is a blueprint for an object; you cannot have an object without first having a class
• Classes can be reused
• Reusable classes tend to have interfaces that are more abstract than concrete (i.e. specific)
• Inheritance allows a class to inherit the attributes and methods of another class, allowing the
creation of brand new classes by abstracting out common attributes and behaviors
o The superclass, or parent class, contains all the attributes and behaviors that are
common to the classes that inherit from it
o Overriding means replacing an implementation of a parent with one from a child 
polymorphism
o When the name of the class and the method is the same and no return type is
provided, the method is a special one, called a constructor  a good place to
perform initializations
• The interface is the set of services that are presented to the end user
• The implementation details of the interface services are hidden from the user and can be
changed as long as the interface remains the same
• If a method is public, then a programmer can access it, and it is considered part of the
interface

Composition

• An inheritance relationship is considered an is-a relationship


• A composition relationship (in which an object may contain other objects) is a has-a
relationship

Interface Design

• Give the users only what they absolutely need  minimum number of interfaces
o It is better to have to add an interface because users really need it than to give the
users more interfaces than they need
• Public interfaces are all the users will ever see
o You should initially hide the entire class from the user. Add interfaces incrementally
as the need arises
• It is vital to design classes from a user’s perspective and not from an information systems
viewpoint
• The class will most likely evolve and need to be updated when a prototype of the system is
built

Constructors

• Constructors are methods that share the same name as the class and have no return type
• When a new object is created, one of the first things that happens is that the constructor is
called
• The most important function of a constructor is to initialize the memory allocated when the
new keyword is encountered; it sets the new object to its initial, stable state
• If the class provides no explicit constructor, a default constructor will be provided
o Calls only the constructor of the superclass
• It is good programming practice to always include at least one constructor in a class. If there
any attributes in the class, they should be initialized in a constructor
• You may have multiple constructor – this is called overloading a method
o Overloading allows a programmer to use the same method name over and over, as
long as the signature of the method is different each time. The signature consists of
the method name and a parameter list

Scope

• Multiple objects can be instantiated from a single class  each of these objects has its own
identity and state
• Some attributes and methods may be shared by all the objects instantiated from the same
class, thus sharing the memory allocated for these class attributes and methods
• There are three types of attributes:
o Local attributes: local to a specific method
o Object attributes: shared by several methods within the same object
o Class attributes: shared by two or more objects

The Anatomy of a Class

• The Class Name


• Comments
• Attributes
• Constructors
• Accessors: Private or Public
• Public Interface Methods
• Private Implementation Methods

Designing With Objects: The Software Development Process

• Solid OO design process includes the following steps:


o Doing the proper analysis of user requirements and the underlying process
o Developing a statement of work that describes the system
 A text document that describes the system
 Written in paragraph form
 Should give anyone who reads it a complete understanding of the system
 Must represent the complete system and be clear about how the system will
look and feel
o Gathering the requirements from this statement of work  Requirements document
 Describes what the users want the system to do
 Not necessarily highly technical
 Must be sufficiently specific to represent the true nature of the user’s needs
for the end product
 Must allow the user to make educated judgments about the completeness of
the system
 Presented as bulleted items
o Developing a prototype of the user interface
o Identifying the classes
 One way is to highlight all the nouns in the requirements and then parsing
down iteratively
o Determining the responsibilities of each class
o Determining how the various classes interact with each other
o Creating a high-level model that describes the system to be built consisting of class
diagrams and class interactions

You might also like