The Object Oriented Thought Process
The Object Oriented Thought Process
• 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?
• 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
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