Object Oriented Programming Chapter 1: Introduction to OOP Prepared by: Mahmoud Rafeek Alfarra 2016
Object Oriented Programming
Chapter 1: Introduction to OOP
Prepared by: Mahmoud Rafeek Alfarra
2016
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
Lecture Let’s think on concept of Class and Object
2
ومن أعرض عن ذكري )فإن له معيشة ضنكا ونحشره يوم القيامة
(أعمىسورة طه
Classes & Objects !
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
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
Class & Object
Class & Object
Class & Object
Class & Object
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.
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 …
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
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 constructorwhich gives initial values to the attributes of class
Is a reserved word The identifier of class Must be as any variable
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
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
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
PracticesGroup 1Compare between constructor and methods in class.
Group 2Detect 3 classes from your bedroom and 2 objects.
Group 3Diffrenciate between Class & Object
Group 4Draw the UML Class Diagram of Course class.
Group 5Draw the UML Class Diagram of Student class.
Group 6Thinking to build Circle class.
THANKS!
Any questions?You can find me at:
Fb/mahmoudRAlfarraStaff.cst.ps/mfarraYoutube.com/mralfarra1@mralfarra