• Tidak ada hasil yang ditemukan

Classes and Objects

N/A
N/A
Protected

Academic year: 2025

Membagikan "Classes and Objects"

Copied!
17
0
0

Teks penuh

(1)

Classes and Objects

(2)

WEEK 6

Lecture 2

(3)

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.

(4)

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 .

(5)

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 ( )

(6)

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

(7)

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) .

(8)

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

(9)

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 .

(10)

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 {

}

(11)

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

(12)

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 }

(13)

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.

(14)

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.

(15)

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.

(16)

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; }

}}

(17)

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.

Referensi

Dokumen terkait

corresponding author’s name, mailing address, telephone and fax numbers, and e-mail address of the author responsible for correspondence about the manuscript (E- mail

corresponding author’s name, mailing address, telephone and fax numbers, and e-mail address of the author responsible for correspondence about the manuscript (E-mail

package Big_Cat; class Big_Cat package bigCat; // Kelas abstrak public abstract class Big_Cat { protected static String name; protected abstract void eat; } // Kelas turunan class

Last Name: First Name: Middle Name: Complete Address: Email: Mobile: Land line: Ethnicity: Religion: Gender: Name of School: Address: Status: Currently enrolled Year Level:

2013 Non World Championships Selection Procedure EXPRESSION OF INTEREST First Name Last Name Address Address Line 2 City State Post Code Telephone Mobile Email Gender Female Male

www.gargi.du.ac.in Page 1 Title Ms First Name Saachi Last Name Chowdhry Photograph Designation Assistant Professor Adhoc Address B 154/A First Floor Derawal Nagar, New Delhi

Department of Electrical and Electronic Engineering University of Dhaka Application for Institutional Email Address First Name Last Name 1.. Current Email ID:

 For example an EMPLOYEE is an object, it may have a Name, SSN, Address, Sex, BirthDate  Each attribute has a value set or data type associated with it – e.g.. integer, string,