Skip to content
February 27, 2013 / Danii Oliver

Classes – Object Oriented Analysis and Design

pt 2

Classes

How to get from Class to Object?
How to go from knowing you need a Class to making a Class?
CRC
How to capture the HAS part the relationship? (have, has, is, associated with)
Inheritance
Polymorphism

Classes
Modules – you can edit and make changes without messing up the other things.

Class – means I have something in my program that represents the real world
a class is a set = like a car is a class not all are the same but it would be the basis of all “cars”
a class is a generalization. it does not describe one object it describes all cars, no specifics.
five wheels, one for steering, at least one door and engine and a gas tank. No kinds.
a car class would also have methods that all cars should do (i.e. drive, reverse, stop, brake)
or
Class name Student
first name last name, user id,
read

Fields – are the line items that would be generalizations
Class =
Generalization
Blueprint of an object
Type : can declare variables the name of the type is the name of the class [var s:Student = new Student(); ]
Abstraction

Type: defining what kind of stuff can get stored at a memory location
Class = Abstract or User Defined } DataType
user defined because you make it up.
typing checking make

Student s; //C++ => object
Student t; //C#, vb.Net, Java, etc => a reference (not yet an object) they need to be instantiated.

Primitive types int, char/string, float/number, bool/boolean

All of the above is part of Design time when you build your class.

At runtime you will use the class. The blueprint is the class specifications to use it you will use the Object.

How to get from Class to Object?
Instantiation = to get the memory and return the address of the memory. This memory will make up the object like matter.

-The compiler know how much memory you need based on the primitive type fields and methods designed in the class.

-The object is empty of values but not empty of available memory for the one object.

Stack vs Heap = The stack is where memory for local vars or functions come from. The Heap is extra memory that is dynamically allocated to a “new” instantiation of an object.
C++ = Stack
All Others = Heap
declaring without new is the placeholder for the address but not the actual object. just declaring uses less memory until you instantiate it.

How to go from knowing you need a Class
to making a Class?

First remember to start with generalizations.
(Yes this begins with good requirements and to get good requirements you should first ask for or define use-cases =context for the requirements in the way they will be used instead of just because which might be out of order )

If you say we are going to have these kind of roles or different kinds of people the initial thing would be a class…. Role, People Cars these are classes.

Class = noun
Method = verb

All nouns in your requirements are potential classes
All verbs in your requirements are potential methods

*any function written must live in a class no more global functions they will be in a calls which makes it a method

*no variables will be global they must all be in a class making it a field

Look at your requirements / use case underline all nouns and circle all verbs.

A bank has customers. Each customer has an account

3 potential classes = bank, customer, account
now validate => CRC
CRC
Class Responsibility Collaborators
Name? Why? With which objects will objects of this class interact ?

Bank –
Why = the bank represents a real bank that would have customers and accounts
Who would it interact with = Objects of Account and Customers

This process is Iterative , you may have to go further down the list to learn who it will collaborate with.

Bank has to see account and customer
Customer needs to go to bank to see account
account need to know bank and customer

Iterative process is going back to see if each time you look at a class will it change when considering other objects. When nothing changes anymore, then you stop. This may take a number of times to look at, write down soon you will draw UML

You must justify if something should be a class in your program, it must solve a problem in the program. Every Class should not do the same thing. One Class does all it needs multiple should do the same thing. They should have one responsibility. You should understand why is it is there, if you can’t answer during design you can’t answer during runtime.

OOP is to translate our thinking to development. Behaviors and Data Points all stay together. Real world things also hav relationship. Objects and Class and relationships. HAS = means there is a relationship.

How to capture the HAS part the relationship?
(have, has, is, associated with)

Next Step realize that all relationships are not the same. There are requirements then there are nice to haves. This relationship will definitely help define what is needed in the class.

So far nothing connecting these three. There is not collaboration. If there are no connections then the requirement
A bank has customers. Each customer has an account
can’t be met.
Right now we see that there are three class that have nothing to do with each other nor any objects. You can’t acquire a relationship at runtime it must be done in the design.

Relationships:
Inheritance
Aggregation
Composition
Association

Inheritance
Inheritance is the only relationship the compiler will help you with.
**In a NON OOP environment you must start over each time, copy and paste the class then add to it. You can’t extend the generic verision in procedural because there is no way to make a relationship.

Inheritance resembles the real life relationship of inheriting all traits of a parent. The Class A can and will be a part of Class B. To understand if the is an Inheritance relationship add IS-A between the two classes
pruis is-a car -YES
car is-a pruis – NO

dog is-a animal – YES
bird is-a animal
pig is-a animal
*there will be some of the same field and some of the same methods.
legs, fur, make sounds,
* All of the things on the right needs to be true for the left

dog is-a cat -NO

bank is-a customer? NO
customer is-a bank? Not in this program
*We are trying to find if there is a similarity to form a relationship that would be based on Inheritance
* All of the things on the right needs to be true for the left

Inheritance is an all or nothing thing!!! If its not all true Inheritance is not appropriate or your Class A is not generic enough.

In UML the arrow image points to the class the connecting object is partly made up of.

Java, C++

single inheritance = means one immediate parent class
Printer p;
LaserPrinter l;

Class Printer {
}
C++
class LaserPrinter : Printer
Java
class Laser extends Printer
AS3
class Laser extends Printer
All others

If the only difference is what would be in the fields not the the behaviors or added fields there is no Inheritance you just would have two different objects passing different field data.

Inkjet is-a Printer
Laser is-a Printer
Inkjet is-s Laser – X

Even though a class has been extended the extended Class is still general. It is a specific Generic class Not an Object. As we go down the classes get more specific but they aren’t objects, this is still a type relationship.

A Subclass has two Types it is itself and it’s parent. Inheritance is a type relationship.

Reference Object
Printer t = new Laserjet();
that a is this every ← interpret right to left thus ( every LaserJet is a Printer ) Polymorphism
here you get to use “t” with any Printer you want not just printer or inkjet or laser
** Don’t have to start by knowing which one you want. It’s like Just in Time ordering or outsourcing the work to special shops at runtime if unknown at design time. This is a powerful allotment,
This is Polymorphism you can change a thing into its child or instantiate its subClass

Printer p = new Printer();
Laserjet l = new Laserjet();
p=l;
every “l” is a “p”

var t:Printer;
if () { t = new Laserjet(); }
else if() { t = Inkjet(); }
else if() { t = ColorLaser(); }

*** “new” is the allocated memory which makes a var an object

Inheritance is a type relationship A is the superclass ← B is inheriting from A. B is the subclass and includes all that A has.
A is of type: A
B is of type: B and A

if Subclass C it would be of type : C,B,A
Each object of a class that inherits another will have enough memory for all included
You cannot separate them. All A’s are not B’s A will not have any knowledge of what’s in B or C
You can add things to ‘B’ that are not in ‘A’
You create B by extending A

The beauty of inheritance is that you don’t have to change the meaning of a superclass when you want to add to it, just extend it and add to it in the subclass

*in UML your arrow always points to the base or parent class.

There are no penalties for having multiple classes but there is a penalty for using one big class.

If in your requirments to read something like “new type of A” or “a specialized version of A”. These words are triggers that you should use Inheritance.

Subclasses cannot count for more than there are Superclasses because it is a special version of it’s superclass. If there are 100 supers and 20 subs that 20 is counted for in the 100.

Related to fields in a Class : don’t put everything and the kitchen sink in the superclass. you should have fewer fields and fewer methods in each. If you are getting to specific break out and make a subclass.

If you are building and you realize that subclasses are sharing fields not in the superclass go back and add that field to the SuperClass.

One immediate superclass is single inheritance.
Having more than one immediate superclass is multiple inheritance.

INHERITANCE : something is a something = the same but, a special kind of

Other relationships
… will use
→ Has
→ Uses
– these two relationships don’t have a type relationship

Has
Is the relationship possessive or dependant.
Possession or Aggregation
“Has” => ownership:
=> whole/part: a page is part of a book without all the pages there is no longer a book;
car has windows, seats, gear shift, doors
=> container : book is a container of pages; a car is a container of all it’s parts
-> component* : has that component not uses it; component of

*only one of these need be true, there is no other expectation.

AGGREGATION : something has-a something.
*0..n or 0

*If you needed all to be true this is called Composition

Composition is aggregation plus => # of part > 0
when you are are expecting to have a certain number of things. more than zero parts not just a container, not just a component or part. Not whole/part it must be the whole.

COMPOSITION : something has-at least something
* 1:Many or 1:n

A steering wheel does not need a car to be a steering wheel but a Car needs a steering wheel to be a Car. The container is not complete without it’s parts. A package deal is Composition.
zero is not

Composition is a stronger form of Aggregation

( see UML page 1)
Dependency or Association (uses)

Association => uses
dependancy
You or a thing do not own another thing nor do you or the thing contain it. not aggreegation or composition
You are not that thing no inheritance
If you use it this is Association

ASSOCIATION : something uses something
*1:1
Without a specific class the other or dependent class cannot achieve a requirement or purpose. It doesn’t need to own it. But it needs to have access.

The relationship of a Gym to its equipment is Composition
The Relationship of a Member to the gym’s equipment is association.
The Member can never own the Equipment and the Gym is still a gym so long as the equipment is there even if there are no Members.

To use the needed class the dependant class will invoke or call a method on the needed class
When an object calls a function on another object it is ASSOCIATION

**All Programmers will not come up with the same solution, it doesn’t make one correct over the other. The key is to be sure that the Design satisfies the requirements.
**You may not get the relationships correct at first. You might have to cycle through a few times.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: