Classes and Objects
WEEK 6
Lecture 2
Object-Oriented Analysis and Design
Programs written with an object‐oriented language revolve around the objects in the problem domain .
Object oriented analysis and design (OOAD) Object‐oriented analysis and design (OOAD) refers to this process of designing pro‐grams that will be solved using an OOP language.
Obj t O i t d P i i b d iti
Object Oriented Programming
Object Oriented Programming is based on writing classes for the objects in the problem domain:
that is, the objects in the problem being solved.
An object is any noun that appears in your An object is any noun that appears in your
problem or that can be used to help solve the problem. A class is written for each object in the problem domain These classes are then
problem domain. These classes are then
instantiated in your program, thereby creating the objects in memory for use by your program .
Classes and Objects
A class is a piece of the program’s source code that describes a particular type of objects.
Obj t O i t d it l
Object Oriented programmers write class definitions.
An object is called an instance of a class. A program can create and use more than one object (instance) of the same class.j ( )
Classes and Objects A blueprint for
objects of a objects of a particular type Defines the
Attributes
Defines the
structure (number, types) of the
types) of the attributes
Defines available
Behaviors
Defines available behaviors of its objects
objects
Class Vs Object
A piece of the A tit i i
A piece of the
program’s source code
An entity in a running program.
code.
Written by a
programmer Created when the programmer. program is running
(by the main method or a constructor or
another method) .
Class Vs Object
Specifies the t t (th
Holds specific values structure (the
number and types) f it bj t ’
of attributes; these values can change of its objects’
attributes — the f ll f it
while the program is running.
same for all of its objects
Specifies the
possible behaviors
Behaves
appropriately when of its objects called upon
Writing a Java Class
A l i J i d l d i th
l
A class in Java is declared using the
class
keyword. A source code file in Java can keyword. A source code file in Java can contain exactly one public class, and the name of the file must match the name of
th bli l ith j t i
the public class with a .java extension .
Example
The fields and methods of a class appear within the curly brackets of the class
declaration The following code shows a declaration. The following code shows a simple class with no fields or methods declared yet.
bli l E l
public class Employee {
}
Adding Fields to a Class
The attributes of an object become fields in the j corresponding class. A field within a class
consists of the following:
Access specifier, which can be public, private, or protected; or the access specifier can be
omitted giving the field the default access omitted, giving the field the default access.
Data type.
Name, which is any valid identifier that is followed a e, c s a y a d de t e t at s o o ed by a semicolon
Example
public class Employee public class Employee {
public String name; //First and last name public String name; //First and last name public String address; //Mailing address public int number; //Employee number|
public int number; //Employee number|
public int SSN; //Social Security number public double salary; //Employee’s salary public double salary; //Employee s salary }
Example
The Employee class is being used to The Employee class is being used to describe employees of a company in the context of paying them weekly The the context of paying them weekly. The fields that appear in the Employee
class represent information about an class represent information about an employee that is needed to compute his or her pay
his or her pay.
Adding Methods to a Class
Behaviors of an object become methods in the j corresponding class. A method within a
class typically consists of the following.
A ifi
Access specifier.
Return value.
N hi h b lid id tifi
Name, which can be any valid identifier.
List of parameters, which appears within parentheses
parentheses.
Definition of the method.
In Java, the of a method must appear within the curly brackets that follow the method
declaration. The following class
demonstrates methods by adding two demonstrates methods by adding two methods to the Employee class.
Example-Employee Class
public class Employee {
public String name;
public String name;
public String address;
public int number;
public int SSN;
public double salary;
public void mailCheck() {
S t t i tl (“M ili h k t “ “\ ” dd )
System.out.println(“Mailing check to “ + name+ “\n” + address);
}
public double computePay() {{
return salary/52; }
}}
Employee class is the following.
The name of the class is Employee The name of the class is Employee.
The class has five public fields.
Th l h t bli th d
The class has two public methods.
The Employee class needs to appear in a file named Employee. java, and the
compiled byte code will appear in a file named Employee. class.