Object Oriented Programming
Instructor Name:
Lecture-5
Today’s Lecture
Components of Class
Fields
Methods
Main() Method, Setter Method, Getter Method, immutator.
Calling Method in same class Main method
Calling Method in other class.
Creating Objects from class
2
Class & Methods
Messages to Object
“Start the
engine of the Object
BMW” <7_series_BMW>
Start_Engine
3
Class & Methods
Method of Class
Class
<CAR>
Start_Engine
4
Class & Methods
Behavior
Behavior is how an object acts and reacts, in terms of state changes and
interactions with other objects.
An operation is some action that one object performs upon another in order
to elicit a reaction.
We will use the word method to describe object behavior in java.
Invoking a method causes the behavior to take place.
5
Class & Methods
What is a Method?
Method defines the behaviour or basic functionality of the class.
Methods have two parts: a header and a body.
Methods belong to a class
– Defined inside the class
Heading
– Return type (e.g. int, float, void)
– Name (e.g. nextInt, println)
– Parameters (e.g. println(…) )
– More…
Body
– enclosed in braces {}.
– Declarations and/or statements.
6
Class & Methods
Writing your own methods
The general form of method definition is
scope type name(argument list) {
statements in the method body
}
where scope indicates who has access to the method, type indicates what type
of value the method returns, name is the name of the method, and argument
list is a list of declarations for the variables used to hold the values of each
argument.
The most common value for scope is private, which means that the method is
available only within its own class. If other Class need access to it, scope
should be public instead.
If a method does not return a value, type should be void. Such methods are
sometimes called procedures. 7
Class & Methods
Returning value from methods
You can return a value from a method by including a return statement, which
is usually written as
return expression;
where expression is a Java expression that specifies the value you want to
return.
As an example, the method definition
private double feetToInches(double feet) {
return 12 * feet;
}
converts an argument indicating a distance in feet to the equivalent number
of inches, relying on the fact that there are 12 inches in a foot.
8
Class & Methods
Method Examples
public class Dog {
private String name;
private int age;
public setName(String dogName){
name=dogName;
}
public String getName(){
return name;
}
}
public class CreateDog {
public static void main (String args[]){
Dog myDog = new Dog();
}
} 9
Class & Methods
Methods Invocation
Invoked as operations on objects/Class using the dot ( . ) operator
[Link](arguments)
“Reference” can either be the class name or an object reference belonging to
the class
Inside the class: “reference” can be ommitted
10
Class & Methods
Methods Overloading
A class can have more than one method with the same
name as long as they have different parameter list.
public class Pencil {
. . .
public void setPrice (float newPrice) {
price = newPrice;
}
public void setPrice (Pencil p) {
price = [Link]();
}
}
How does the compiler know which method you’re invoking? —
compares the number and type of the parameters and uses the
matched one 11
Class & Methods
Methods – Parameter Value
Parameters are always passed by value.
public void method1 (int a) {
a = 6;
}
public void method2 ( ) {
int b = 3;
method1(b); // now b = ?
// b = 3
}
• When the parameter is an object reference, it is the object
reference, not the object itself, getting passed.
12
Method Example – (Parameter is Object Reference)
class PassRef{
plainPencil
public static void main(String[] args) {
Pencil plainPencil = new Pencil("PLAIN"); color: PLAIN
[Link]("original color: " +
[Link]); plainPencil p
paintRed(plainPencil); color: PLAIN
plainPencil p
[Link]("new color: " +
[Link]); color: RED
}
plainPencil p
public static void paintRed(Pencil p) {
color: RED NULL
[Link] = "RED";
p = null;
- If you change any field of the object which the parameter refers to, the object is changed for
}
every variable which holds a reference to this object 13
}
- You can change which object a parameter refers to inside a method without affecting the original
reference which is passed
- What is passed is the object reference, and it’s passed in the manner of “PASSING BY VALUE”!
Class & Methods
Accessor & Mutator Methods
Accessor methods are those which returns information to the caller about the
state of the object; they provide access to information about the state of object
Accessor usually contains the return statement in order to pass back that
information.
getName()
Mutator method is one that change the state of an object.
The most basic form of object is one that takes a single parameter whose
value is used to directly overwrite what is stored in the object’s field.
setName()
14
Class & Methods
Accessor Methods
15
Class & Methods
Mutator Methods
16
17