Unit 2: Classes, Objects and Class Diagrams
Unit 2: Classes, Objects and Class Diagrams
Unit 2: Classes, Objects and Class Diagrams
Unit 2
Introduction
In the previous unit, you defined Object-Oriented
Programming (OOP) and stated the characteristics of
OOP. In this unit, you are going to be introduced to
classes, objects and class diagrams.
Success Criteria
By the end of this Unit, you should be able to:
define classes, objects and class diagrams
create classes and objects
state benefits of class diagrams
translate a real-life problem into an object
oriented design using class diagrams
Key Words
You will find the following key words or phrases in this
unit: class, object, attribute, multiplicity, class
diagram. Watch for these and make sure that you
understand what they mean and how they are used in
the unit.
Classes Objects
Animal Dog
Cat
Goat
Cow
Fruit Pawpaw
Orange
Mango
Lemon
Example:
Example:
Local Variables
Instance Variables
Static Variables (Class Variables)
Local Variable: This is a variable which is defined
within a block or method or constructor.
Example:
public class Nalikule{
public void pupAge() {
//creating local variable inside the method pupAge()
int x = 5
Computer Programming II, Object-Oriented 2-2
Domasi College of Education
Example:
class Domcol{
static int x; //static variable created outside
the method pupAge()
public void pupAge() {
}
}
Computer Programming II, Object-Oriented 2-3
Domasi College of Education
Creating an Object
In order to create an object, you must first of all create
a class.
Accessing Attributes
You may be interested to note that in order to access
attributes you are supposed to create an object of the
class, and use the dot syntax (.):
Practise Activity
Assign and print the roll number, phone number and
address of two students having names "Sam" and
"John" respectively by creating two objects of class
'Student'.
Definition 2.1
A class diagram is a static structure that gives an
overview of a software system by displaying classes,
attributes, operations, and their relationships between
each other.
Class Name
Attributes
Methods
Person Class
-name: String
+getName(): String
+ setName(name): void
You should note that the above example has used two
visibility markers to signify who can access the
information contained within a class. The following are
the some of the visibility markers:
Example:
2. Generalization: A generalization is a
relationship between a parent class
(superclass) and a child class (subclass). In
this, the child class is inherited from the
parent class.
Example:
Example:
Example:
Example:
Multiplicity
Indicator Meaning
0..1 Zero or one
1 One only
0..* Zero or more
n Only n (where n>1)
0..n Zero to n (where n>1)
1..n One to n (where n>1)
1..* * One or more
Summary
In this unit, you have been introduced to classes,
objects and class diagrams. You have also been
introduced to multiplicity.
object is defined as an instance of a class
class is defined as a template/blueprint that
describes the behaviour/state that the object of
its type supports
a class diagram is a static structure that gives
an overview of a software system by displaying
classes, attributes, operations, and their
relationships between each other
multiplicity is defined as a specific range of
allowable instances of attributes
Reflection
Which part of this unit is most challenging to you?
Why? Do you need special assistance?
Unit 2 Test
1. Create a class named 'Student' with String
variable 'name' and integer variable 'roll_no'.
Assign the value of roll_no as '2' and that of
name as "John" by creating an object of the class
Student.
2. A company consists of departments.
Departments are located in one or more offices.
One office acts as a head quarter. Each
department has a manager who is recruited from
the set of employees. Draw a class diagram
which consists of all the classes in your system
their attributes and operations, relationships
between the classes, multiplicity specifications,
and other model elements that you find
appropriate.
class Ans{
public static void main(String[] args){
Student s = new Student();
s.name = "John";
s.roll_no = 2;
System.out.println("Name is "+s.name+"
and roll number is "+s.roll_no);
}
}