Вы находитесь на странице: 1из 39

Fundamentals of Software

Development
CT010-3-1
Object-Oriented Programming

Prepared by: STH First Prepared on:27 th July 2005 Last Modified on:19th December 2005
Quality checked by: GTK
Copyright 2005 Asia Pacific University College of Technology and Innovation

Topic & Structure of the lesson


Object-Oriented Programming
Introduction to classes and objects
Defining a class
Adding variables and methods to a class
Defining an object
Visibility control
public, private, protected, default

Static/Instance members
CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 2 (of 39)

Learning Outcomes
At the end of this topic, you should be able to:
Explain object-oriented programming concepts
using appropriate examples.
Write class definitions with members.
Explain the usage of different visibility controls
using appropriate example.
Create, edit, compile, run and debug simple objectoriented Java programs.

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 3 (of 39)

Key Terms
If you have mastered this topic, you should be able to use
the following terms correctly in your assignments and
exams:
Class
Object
Methods
Instance Members
Class Members
Inheritance
Multiple Inheritance
Polymorphism
Encapsulation
Information Hiding
State of an object
Visibility Controls
CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 4 (of 39)

What are objects?

Objects are things

Footballer

Complex Number

Car

Bank Account

Scale

Lion

Objects may be simple or complex


Objects may be real or imaginary

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 5 (of 39)

What are objects?

What are not objects?


Some things are not objects but are attributes, values or
characteristics of objects.

Speed color size cost


Some things are merely attributes of objects such as color, size
or speed.
Attributes reflect an objects state, the speed of the car or, size of
the object ~ thus, speed is not considered an object in its own
right.

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 6 (of 39)

What are objects?


The Car Object

In order to define the car object, we need to be able to abstract the


states/attributes and behaviours/functions of the car. We need to
define the car in terms of :

State
Behaviour

CT010-3-1 Fundamentals of Software Development

What features/current state distinguish


it from other objects?
What it can do

Object Oriented Programming

Slide 7 (of 39)

What are objects?

An object (a software object) is a software bundle of variables and


related methods.
A software object maintains its state in variables and implements its
behaviour with methods.
State
==>
variables
Behaviour
==>
methods
We can represent real-world objects using software objects.
For eg. We may represent real-world dogs as software objects in an
animation program.
An object is the basic run time entity in Object Oriented
Programming.

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 8 (of 39)

What are objects?


The Car Object (contd)

State
A car may have the following features or attributes :
Color
Speed
Attributes
Size
Fuel
moving
Current
stopped

states

Behaviour
Functionally, a car can do the following:
Go
Stop/Brake
Turn right
Turn left

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 9 (of 39)

What are objects?


Benefits

Modularity - The source code for an object can be written


and maintained independently of the source code for
other objects. Also, an object can be easily passed
around in the system. You can give your car to someone
else and itll still work.

Information hiding - An object has a public interface that


other objects can use to communicate with it. But the
object can maintain private information and methods that
can be changed anytime without affecting the other
objects that depend on it. You dont need to understand
the gear mechanism on your bike to use it.

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 10 (of 39)

What are objects?


Object Encapsulation

Encapsulation
is the grouping together of related states and behaviors to form
objects, whereby some parts of the object remain visible while some
parts are hidden. Encapsulation involves information hiding.

Information Hiding
Only necessary information to accompish a task is provided to the
user (in this case, the programmer). This is the principle of
information hiding and is a related principle of encapsulation.

Some parts are visible (the public interface)


Other parts are hidden (or private)

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 11 (of 39)

What are objects?


The Car Object

Encapsulation is used to hide unimportant implementation details


from other objects.
When you want to change gears on your car, you dont need to know
how the gear mechanism works, you merely need to know which
lever to move.
Similarly, in software programs, you dont need to know how the
class is implemented, you just need to know which method to invoke.
(For eg. Keyboard.readInt() )
Thus, the implementation details can change anytime without
affecting other parts of program.

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 12 (of 39)

What are objects?


Object Encapsulation
E.g. The car steering wheel

-The steering wheel presents a public interface to the turn


mechanism on a car.
- How steering is implemented is private and may only be acted
upon by the steering wheel.

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 13 (of 39)

Data Encapsulation
The Car Object

Public
API

change
gears

5th gear
Gear implementation

brake
turn
steering

CT010-3-1 Fundamentals of Software Development

60 mph

Object Oriented Programming

Steering
implementation

Private
implementation
details

Brake implementation

Slide 14 (of 39)

Quick Review Questions


1. What is an object?
2. Give an example of an object.
3. What criteria would you use to justify that it is indeed an object?
4. Give 3 other examples of objects.
5. Give an example for Data encapsulation.

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 15 (of 39)

What are classes?


In the real world, we can have many objects of the same kind. For eg.
Ferrari is just one type of car while Honda is another type of car but we
classified them under car.
Thus, in OOP, we can say that :
car ==> class, Ferrari1 ==> object, Honda1 ==> object
==> An object is an instance of a class

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 16 (of 39)

What are classes?

Objects Vs Classes

- Class
A class defines a real world or abstract entity. It is the
type or classification of data. A class defines both the
behavior and attributes of a group of objects with similar
characteristics.
Car class

class name

start, go, stop, turn

methods (functions)

color, speed, fuel

attributes (data)

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 17 (of 39)

What are classes?


- Object
An object is an instance or variable of a class. It is
said to belong to the class. An object can be
distinguished from other members by its attributes.
Car.ferrari1

Object name
Methods :
start, go, stop, turn
Data / Attributes :
red, 175 m.p.h, full

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 18 (of 39)

Defining a class
A class is a user-defined data type.
Once the class type has been defined, we can create
variables of that type using declarations similar to
primitive data types.
Data and methods defined in the class are known as
instance members.
class classname
{
variable(s) declaration
method(s) declaration
}
CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 19 (of 39)

Creating an Object

Creating an object is referred to as instantiating an object.


Objects in Java are created using the new operator.
new operator creates an object of the specified class and
returns a reference to that object.
Eg.
Car a;
object declaration
Car()
a=new Car();
object definition

default constructor of the class

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 20 (of 39)

Quick Review Questions


1. What is a class?
2. Give five example of classes from real-life.
3. What are the components of a class?
4. Give an example of a class within the context of Java
programming language.
5. What is object instantiation?
6. List the difference between object declaration and
definition.

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 21 (of 39)

Adding members to a class


Data is encapsulated in a class by placing variables
inside the body of the class definition.
These variables are known as instance variables
because they are created whenever an object of the
class is created.
Methods that are necessary for manipulating the
data contained in the class are also placing inside
the body of the class definition.

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 22 (of 39)

Class methods
Method Structure
is a set of program statements
fundamental unit of execution
exists as part of a classs

Syntax of a method:
returnType methodName (paramList) {
// body of method
}
returnType return type (value)
declare void if no value returned
methodName name of method
paramList optional list of parameters
CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 23 (of 39)

Class methods
To invoke a method :
methodName(args);
invoke the method by using its name and passing it an
optional set of arguments
Example :
boolean process(int i, String s){
//body of method
}
method that accepts int and String parameters and
returns a boolean type
CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 24 (of 39)

Class methods

Write the program necessary to create a small application calSqrt that print
out Hello Malaysia and has one calculateSqrt() method. The main() method
will call the calculateSqrt() method which calculates the square root of 5 and
then prints out the answer on a new line.

Sample program :
class calSqrt {
public static void main (String[] args) {
System.out.println(Hello Malaysia);
calculateSqrt(5);
}
static void calculateSqrt(double num) {
double numSqrt;

numSqrt = Math.sqrt(num);
System.out.println(The squareroot of 5 : + numSqrt);

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 25 (of 39)

Messages
A single object alone is generally not very useful and usually appears as a component of a larger program
or application that contains many other objects ~ thus, object needs to interact with each other.
How do objects interact?
Objects interact by sending messages to each other.

Message
Object B

CT010-3-1 Fundamentals of Software Development

Object A

Object Oriented Programming

Slide 26 (of 39)

Messages
3 components comprise a message :
1. The object to whom the message is addressed
(Your Bicycle)
2. The name of the method to perform
(changeGears)
3. Any parameters needed by the method
(lower gear)

changeGears (lowerGear)

Your Car

You

Complete Message (Method call):


yourCar.changeGears(lowerGear)
CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 27 (of 39)

Visibility control
Term

Meaning

private

can be invoked only by code in the


same class

protected

can be invoked only by code in a subclass or


the same package

public

can be invoked by any other class

default

accessible within the same package only

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 28 (of 39)

Classes and objects in Java


Declaring Objects (Instance of Class) & Using It in
Java
The Car Class
Create an instance of a Class - Declare variables to
contain Car object :
Car ferrari1, ferrari2, honda;
when a variable of type Car is declared, it creates
a container that is empty.
Create an actual object using the operator new
ferrari1 = new Car(arguments)
==> creates a new object and invokes a method
called the constructor to initialize the new object.
CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 29 (of 39)

Classes and objects in Java


Declaring Objects (Instance of Class) & Using It in Java
The Car Class
Each object has a certain set of methods (the
instance methods) and a certain set of variables (the
instance variables)
Instance variables
ferrari1.speed=170;
Instance methods
ferrari1.turn(right);
CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 30 (of 39)

Classes and objects in Java


Example 1 :
public class Greetings {
public void greet(String name) {
System.out.println("Welcome To " + name + "'s Web Site");
}
}

Greetings
Class

class TestGreetings {
public static void main(String[] args) {
Greetings welcome = new Greetings();
welcome.greet(God");
}
}

CT010-3-1 Fundamentals of Software Development

TestGreetings
class

Object Oriented Programming

Slide 31 (of 39)

Classes and objects in Java


Example 2 :
public class Greetings {
String salutation;
Greetings(String s) {
salutation=s;
}
public void greet(String name) {
System.out.println(salutation + "Welcome To " + name + "'s Web
Site");
}
}

Greetings
class

class TestGreetings {
public static void main(String[] args) {
Greetings welcome = new Greetings("Hello,");
welcome.greet(God");
}
}
CT010-3-1 Fundamentals of Software Development

TestGreetings
class

Object Oriented Programming

Slide 32 (of 39)

Classes and objects in Java


public class Greetings {
String salutation;
public void greet(String name) {
System.out.println("Welcome To " + name + "'s Web Site");
}
public static void showTitle() {
System.out.println("Testing Modular Programming in Java ");
}

Greetings
class

}
class TestGreetings {
public static void main(String[] args) {
Greetings.showTitle();
Greetings welcome = new Greetings();
welcome.greet(God");
}

TestGreetings
class

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 33 (of 39)

Static modifier
Static Variables
eg.
private static final int April = 4, May = 5, June = 6;
static modifier may only be used on members at
the class level, not on variables and arguments
within methods.
also known as class variable

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 34 (of 39)

Static modifier
Static Methods
referenced through the class itself
eg. main method of a Java program must be
declared static so that main can be executed by
the interpreter without instantiating an object from
the class containing main
eg. Math class in the java.lang package

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 35 (of 39)

Quick Review Questions


In your own words,

What is a member variable?

What is a method?

List the visibility controls in Java

What is instance variable?

What is class variable?

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 36 (of 39)

Follow Up Assignment
Write an app that declares a Rectangle class.
Declare two instance variables width and height
of type double. Declare an instance method
getArea that calculates the area of the
rectangle. Display the area of the rectangle from
main class.
You will be required to demonstrate your
answer on the board during the next lesson.

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 37 (of 39)

Summary of Main Teaching


Points
Object-Oriented Programming
Introduction to classes and objects
Defining a class
Adding variables and methods to a class
Defining an object
Visibility control
public, private, protected, default

Static/Instance members
CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 38 (of 39)

Next Session
Inheritance
superclass and subclass
single level inheritance
using extends
Constructors and creating them
Sample programs

CT010-3-1 Fundamentals of Software Development

Object Oriented Programming

Slide 39 (of 39)

Вам также может понравиться