Classes and objects
Download
Report
Transcript Classes and objects
Lecture 11: Classes and Objects
Yoni Fridman
7/18/01
Outline
Objects in the real world
Representing objects in Java
Classes
Example – a telephone
Instance variables
Instance methods
Constructors
Creating new objects
Using instance methods
Strings revisited
Objects in the Real World
Java is an object-oriented language.
An object is something that has two main parts:
That means that objects play a crucial role in Java, but what are
objects?
It’s state – the characteristics of that particular object at a particular
point in time.
It’s behavior – the operations that can be performed on the object,
along with its response to those operations.
Example: A telephone.
What’s the state of this telephone?
What’s its behavior.
Representing Objects in Java
We can represent the state of an object in Java
using variables.
Each variable represents one characteristic of the object.
We can represent the behavior of an object in Java
using methods.
Each method represents one operation that can be performed.
Classes
The variables and the methods that are associated
with a particular kind of object (e.g., telephone)
are grouped into a class.
Variables that store the state of an object are called
instance variables.
Methods that store the behavior of an object are
called instance methods.
Classes
A class declaration begins with the Java reserved
words public and class, and then has the name of
the class.
As a convention, class names usually begin with a
capital letter.
public class Telephone
{
instance variables
constructor methods
instance methods
}
Classes
Within a class body, the first step is to declare
instance variables.
Unlike normal variables, instance variables
usually begin with the reserved word private.
This indicates that these
variables can only be modified
from within this class.
private boolean on;
public class Telephone
{
instance variables
constructor methods
instance methods
}
Classes
Instance methods usually begin with the reserved
word public.
This indicates that these methods can be called from anywhere in
the program, not just from within this class.
Unlike other methods, instance methods don’t use
the reserved word static
public class Telephone
(we’ll see why later).
{
public void turnOn()
{
on = true;
}
instance variables
constructor methods
instance methods
}
Classes
Before the instance methods come special
methods called constructors.
A constructor is a method that initializes an
object’s instance variables.
It has no return type, and it must
be named the same as the class.
public Telephone(boolean initialOn)
{
on = initialOn;
}
public class Telephone
{
instance variables
constructor methods
instance methods
}
Classes
A class can also have a default constructor.
This constructor takes no arguments, and
initializes the instance variables to default values.
Every class usually has both types of constructors.
public Telephone()
{
on = false;
}
public class Telephone
{
instance variables
constructor methods
instance methods
}
Creating New Objects
Once we’ve written a class, we can declare an
object of that class type:
And we can initialize it using either of our
constructors:
Telephone myPhone;
myPhone = new Telephone();
myPhone = new Telephone(false);
Both of these will initialize the instance variable on to false.
myPhone is an instance of the class Telephone,
which contains its own copies of the instance
variables.
Using Instance Methods
Once we’ve declared and initialized an object, how
do we perform operations on it?
For example, what if we want to turn myPhone on?
An operation is performed on an object by writing
the object name, a period, and then an instance
method name.
Example: myPhone.turnOn();
This will set the instance variable on contained in the object
myPhone to true.
The reserved word static is used for methods that
don’t belong to any object.
Strings Revisited
Strings are objects whose class has already been
written.
String myName = “Yoni”; can also be written
String myName = new String(“Yoni”);
The second option uses the String constructor.
The first option is a short-cut, since Strings are so
common.
myName.length() is an instance method of the
String class – it takes no arguments and returns the
length of the object myName.
Homework
Read: 3.7, 3.8