2. Concepts for this lecture
• class; object; instance
• method; parameter; signature
• data type
• multiple instances; state
• method calling; source code; method result
• Reading: Objects First with Java, Chapter 1
2
3. • Objects represent ‘things’ from the real world,
or from some problem domain
• e.g. the red car down there in the car park
• e.g. the lecturer talking to you now
• e.g. you!
• Classes represent all objects of a certain kind
• e.g. Car, Lecturer, Student
A class is a group of objects that have similar
characteristics and that exhibit similar behaviour
An object is a specific instance of a class
Classes and objects
3
4. Example
• The set of all students forms the class Student
• Each individual student is an object of the class Student
• John Smith and Janice Lee are instances of Student
4
5. Example
• The set of all dogs forms the class Dog
• Each individual dog is an object of the class Dog
• Spot, Rover, and Rex are all instances of Dog
5
6. Why do we use classes?
• To reduce complexity
• Often, we know how to deal with an object based
purely on knowing its class, without knowing anything
specifically about that particular instance
• For example, if we encounter a dog – i.e. an instance of
the class Dog – we already have a basic understanding
of how to deal with it, even if we have never previously
met that particular dog
• We know that it might bark, or bite, or wag its tail,
based purely on knowing that it is a Dog
• Barking, biting, and tail-wagging are best viewed as
features of the class Dog, not of any individual dog
6
7. Describing a class
• We describe a class by listing the common features that
are shared by all the objects in that class
• The attributes that each object has, and
• The actions that each object can perform
• Student number is an attribute of the class Student
• Every student has a student number; although each individual
student has a different student number
• Barking is an action that all objects of the class Dog do
• Every dog barks; although different dogs do it differently,
based on the attributes of a given individual
7
8. • A class is a blue print from which individual objects are created. A
sample of a class is given below:
8
Class
9. What is a Waiter ?
• A Waiter has the following attributes
• Name
• Tax File Number
• And the following actions
• Bring menus
• Take orders
• Bring meals
• This collection of attributes and actions defines
the class of Waiters
• We can deal with any individual waiter, whether
we have met them before or not, based solely on
our knowledge of the class Waiter
9
10. What's in an object?
• Objects have operations that can be invoked
• Java calls these methods
• An object usually does something when we invoke a method
• Objects have state
• The state is represented by the stored values of attributes in “fields”
• The state of an object is a “snapshot” of that object at a
particular moment in time
• e.g. the class Student might have
• An attribute studentNumber, that never changes, and
• An attribute booksBorrowed, that does change
11
11. Object state
• Notice the types of the fields:
int, String, boolean
• Types restrict the values that a field can take
12
12. • Example of creating an object is given below:
13
Creating Objects
13. Instances
• Multiple instances:
Many similar objects
can be created from
a single class
• An object has attributes:
values stored in fields
• The class defines what
fields an object has
• Each object stores
its own set of values
(the state of the object)
14
14. • Methods can have parameters (arguments) to provide
additional information for a task.
• The header of a method is called its signature. It
provides information needed to invoke that method.
• The additional values that some methods require are
called parameters.
• When calling, for example, the moveHorizontal
void moveHorizontal(int distance)
This is called the signature of the method.
• The part enclosed by parentheses (int distance) is the
information about the required parameter.
• For each parameter, it defines a type and a name.
15
About methods
16. A Java method
/**
* Move the circle horizontally by
* 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
17
17. • Parameters have types. The type defines what kinds of
values a parameter can take.
• A type specifies what kind of data can be passed to a
parameter. The type int signifies whole numbers (also
called “integer” numbers, hence the abbreviation “int”).
• If it has a parameter, the type and name of that
parameter is displayed.
18
Data Types
18. • In the list of methods for a circle class, you will see one
method with a different parameter type: the “color”
method has a parameter of type String.
• The string type indicates that a section of text (for
example, a word or a sentence) is expected.
• Strings are always enclosed within double quotes. For
example, to enter the word red as a string, type:
"red"
• Java supports several other data types, including
decimal numbers and characters.
19
1.5 Data Types (Cont …)
19. • Result mean methods may return information about an
object via a return value. Such methods have a non-void
return type
• Some methods may have void return types
• Suppose student class has changeName method.
• The signature of changeName states:
void changeName(String replacementName)
• The word void indicates that this method does not return
any result.
• Methods with return values enable us to get information
from an object via a method call.
20
Return Values
20. • In this lecture, we have explored the basics of classes and
objects.
• We have discussed the fact that objects are specified by
classes.
• Classes represent the general concept of things, while objects
represent concrete instances of a class. We can have many
objects of any class.
• Objects have methods that we use to communicate with
them.
• We can use a method to make a change to the object or to
get information from the object.
• Methods can have parameters, and parameters have types.
• Methods have return types, which specify what type of data
they return.
• If the return type is void, they do not return anything.
SCI101 Object Oriented Computing, (c) 2014 21
Summary
21. • Objects store data in fields (which also have types).
• All the data values of an object together are referred to
as the object’s state.
• Objects are created from class definitions that have
been written in a particular programming language.
• Much of programming in Java is about learning to write
class definitions.
• A large Java program will have many classes, each with
many methods that call each other in many different
ways.
• To learn to develop Java programs, we need to learn
how to write class definitions, including fields and
methods, and how to put these classes together well.
SCI101 Object Oriented Computing, (c) 2014 22
Summary (Cont …)