Chapter3: Introduction to
Classes and Objects
Classes and Objects: Definitions
Objectives
•What is an object
•What is a class
•UML representation of a class
•Objects and Instance variables
•Primitive types and reference type
•Practical Organization
Page 2 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Let’s consider the following
• Let’s consider two doors D1 and D2.
• We aim to develop an application monitoring these
doors.
• What actions may be applied on these doors:
• Open and close.
Page 3 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Procedural Programming
• In Procedural programming:
• The doors are considered as passive entities of
the real world with no interaction with their
environments.
• Two robots (procedures) with specific roles are
created: one for Opening doors, the other for
closing.
) Open(doorId) ) Close(doorId)
• In order to open or to close a given door, the
user should:
Order the appropriate robot to perform the
required action on the specified door.
– Open(d); or
– Close(d); where d is either D1 or D2
Page 4 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Object Oriented Programming
• In Object-Oriented programming:
• The doors are considered as active entities of
the real world capable of interacting with their
environments.
• Each one of them offers two services open and
close.
) Open() ) Close()
• In order to open or to close a door, the user
should:
Order the appropriate door to perform the required
action.
– d.Open(); or
– d.Close(); where d is either D1 or D2
Page 5 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Objects
• Objects are key-concept to understand object-
oriented technology.
• Objects are entities of the real-world that may
interact with their environments by performing
services on demand.
• Examples of real-world objects: your Car, your
Cell-phone, the coffee slot-machine.
• Each Nokia-N71 cell-phone is an object and
may execute some services.
Page 6 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Classes
• Objects of the real world may be
classified into types: Cars, Cell-
Phones, CD Players, etc.
• Objects of the same type have the
same characteristics and are
manufactured using the same
blueprint.
• A class is a blueprint or prototype
from which objects of the same type
are created.
• A class describes a set of objects
having the same characteristics and
offering the same services.
Page 7 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Object Oriented Basic Principles
• Abstraction • Inheritance
• Encapsulation • Overriding
• Information Hiding • Polymorphism
• Message Passing • Dynamic Binding
• Overloading
• Information hiding, Message passing and
Overloading are covered by chapter 5 of this
course.
• Inheritance, Polymorphism, Overriding and
Dynamic binding are discussed in CSC 113.
Page 8 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Abstraction Principle
• Data Abstraction • Functionality Abstraction
– In order to process – Modeling functionality
something from the real suffers from
world we have to extract • unnecessary functionality
the essential characteristics may be extracted,
of that object. • or alternatively, an
– Data abstraction is the important piece of
process of: functionality may be
omitted.
• Refining away the
unimportant details of an – Functionality abstraction is
object, the process of determining
• Keeping only the useful which functionality is
characteristics that define important. view
the object.
– For example, depending on
how a car is viewed (e.g. in
terms of something to be
registered, or alternatively
something to be repaired, view view
etc.) different sets of
characteristics will emerge
as being important.
Page 9 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Encapsulation Principle
• Abstraction involves reducing a real world
entity to its abstraction essential defining
characteristics.
• Encapsulation extends this idea by also
modeling and linking each data of an entity
to the appropriate functionality of that
entity.
Page 10 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Encapsulation Gives Classes
• OOP makes use of • Encapsulation is the OO
encapsulation to ensure that principle that allows objects to
data is used in an appropriate contain the appropriate
manner. operations that could be
– by preventing from applied on the data they store.
accessing data in a non-
intended manner (e.g. asking – My Nokia-N71 cell-phone
if an Integer is true or false, stores:
etc.). • My contacts,
• Missed calls
• Through encapsulation, only a • … etc.
predetermined appropriate
group of operations may be – My Nokia-N71 may perform
applied (have access) to the the following operations on
data. the data it contains:
• Edit/Update/Delete an
existing contact
• Place data and the operations • Add a new contact
that act on that data in the • Display my missed calls.
same class. • …etc.
Page 11 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
UML Representation of a Class
• UML represents a class with a rectangle having 3
compartments stacked vertically.
• The top compartment shows the class's name.
• The middle compartment lists the attributes.
• The bottom compartment lists the operations: methods or
services.
ClassName
- att1: dataType1
-… Attributes
- atti: dataTypei
+ m1(…): dataType1 Methods
+ ... (Services)
+ mj(…): dataTypej
Page 12 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Attribute
• An attribute is an abstraction of a single
characteristic possessed by all objects of the
same class.
• An attribute has a name unique within the
class.
• There are two types of attributes:
• Class attributes
– Independent of any object and their values are shared by
all objects of the class.
• Instance attributes
– Dependent to the objects and their values are associated
with and accessed through objects.
Page 13 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Declaring a Class with Java
ClassName
- att1: dataType1
-… Attributes
- atti: dataTypei
+ m1(…): dataType1 Methods
+ ... (Services)
+ mj(…): dataTypej
public class ClassName {
// Attributes
// Methods (services)
Page 14 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Declaring Attributes With Java
<modifiers> <data type> <attribute name> ;
Modifiers
Modifiers Data
DataType
Type Name
Name
public String studentName ;
Page 15 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Example of a Class Declaration
with Java
public class Course {
// Attributes
public String studentName;
public String courseCode ;
// No method Members
}
Page 16 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP