chapter 1: Lecture 2
Let’s think on concept of Class and Object
- Concept of Objects and classes
- UML Class Diagram
الكلية الجامعية للعلوم والتكنولوجيا - خان يونس
University college of science & technology
2. Outlines
◉ What is Object-Oriented Programming ?
◉ Procedural vs. Object-Oriented Programming
◉ OO Programming Concepts
◉ Concept of Objects and classes
◉ UML Class Diagram
◉ Visibility Modifiers and Accessor Methods
◉ Full Example
6. o Classes are constructs that define objects of the
same type.
o “Class” refers to a blueprint. It defines the variables
and methods the objects support.
What is Class ?
Class Name: Circle
Data Fields:
radius is _______
Methods:
getArea
A class template
7. o “Object” is an instance of a class.
o Each object has a class which defines its data and behavior.
o An object has both a state and behavior.
o The state defines the object, and the behavior defines what the
object does.
What is Object?
Class Name: Circle
Data Fields:
radius is _______
Methods:
getArea
Circle Object 1
Data Fields:
radius is 10
Circle Object 2
Data Fields:
radius is 25
Circle Object 3
Data Fields:
radius is 125
A class template
Three objects of
the Circle class
12. Thinking to build class …
Any Thing
Attributes
Behavior
Each one is presented as
a variable in the Class
Each one is presented as
a method in the Class
A new class will be considered
as a new data type, so you can
declare a variables (Objects) of
them and then you can set and
get data to its properties.
13. o A Java class uses variables to define data fields and methods to
define behaviors.
o Additionally, a class provides a special type of methods, known as
constructors, which are invoked to construct objects from the
class.
Thinking to build class …
14. Thinking to build class …
class Circle {
/** The radius of this circle */
double radius = 1.0;
/** Construct a circle object */
Circle() {
}
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}
/** Return the area of this circle */
double getArea() {
return radius * radius * 3.14159;
}
}
Data field
Method
Constructors
15. How to build my class?
Access_modifiers class class_name {
// variables = attributes
Access_modifiers class_name(par1, par2, …) {
}
// behavior = methods
}
Always, the class has
a method called constructor
which gives initial values
to the attributes of class
Is a reserved word The identifier of class
Must be as any variable
16. o A class can have three kinds of members:
fields: data variables which determine the status of the class
or an object
methods: executable code of the class built from
statements. It allows us to manipulate/change the status of
an object or access the value of the data member
nested classes and nested interfaces
Class Members
17. How to insatiate object?
class_name object_name = new class_name (arg1, arg2, …);
The name of class, which
you want to insatiate an
object of it.
Reserved word
Values based on the
parameters of constructor
The name
of object
18. UML Class Diagram
Circle
radius: double
Circle()
Circle(newRadius: double)
getArea(): double
circle1: Circle
radius = 1.0
Class name
Data fields
Constructors and
methods
circle2: Circle
radius = 25
circle3: Circle
radius = 125
UML Class Diagram
UML notation
for objects
19. Practices
Group 1
Compare between
constructor and
methods in class.
Group 2
Detect 3 classes from
your bedroom and 2
objects.
Group 3
Diffrenciate between
Class & Object
Group 4
Draw the UML Class
Diagram of Course
class.
Group 5
Draw the UML Class
Diagram of Student
class.
Group 6
Thinking to build Circle
class.