Back to the introduction
Introduction To Object-Oriented Programming
Learning Java requires you to understand the principles of object-oriented programming.
From your first year CPSC courses, you should have learned what an object is, what a class
is, how objects and classes are related, and how objects communicate by using messages. If
you do not know about these concepts, or if you simply wish to refresh your memory, then
read the following sections:
What is an Object?
If you look around you, you will find many examples of real-world objects: your book,
your computer, your pet, etc. In object-oriented programming, programs are viewed as
collections of objects. Objects have three characteristics:
state, behavior and
identity. We can use a bank account to illustrate these concepts:
- State: id, name, balance
- Behaviour: deposit, withdraw, etc.
- Identity: Joe's account is similar to Jane's, but its state has different values.
You can see that an object is essentially a group of values and behaviours (also called
methods, or member functions) that gives itself an identity. The following illustration is
a common visual representation of a software object:
The object diagram shows that the object's variables make up the center, or nucleus, of
the object. Methods surround and hide the object's nucleus from other objects in the
program. Packaging an object's variables within the protective custody of its methods is
called encapsulation. However, very often an object may wish to
expose some of its variables or hide some of its methods. In Java, an object can specify
one of four access levels for each of its variables and methods. The access level
determines which other objects and classes can access that variable or method. We will
talk more about accessing control later.
What is a Message?
A single object alone is generally not very useful. Instead, an object usually appears
as a component of a larger program or application that contains many other objects.
Through the interaction of these objects, programs can perform more complex functions.
Software objects interact and communicate with each other by sending
messages to each other. When object A wants object B to perform
some action (call one of its methods), object A sends a message to object B:
A message is comprised of three components:
- The object to which the message is addressed.
- The name of the method to perform.
- Any parameters needed by the method.
Messages provide two important benefits:
- The behaviour of an object is expressed through its methods, and thus (aside from
direct variable access) message passing supports all possible interactions between
objects.
- Objects do not need to be in the same process or even on the same machine to send and
receive messages back and forth to each other.
What is a Class?
In the real world, you often have many objects of the same kind. For example, your bank
account is just one of many bank accounts at your bank. In terms of object-oriented
programming, objects that share the same kinds of state and behaviour belong to the same
class. In other words, a class defines the state and behaviour
common to all objects of a certain kind, and any object belonging to that class is an
instance of that class. We can use a bicycle example to
illustrate these concepts. The bicycle class defines the variables and methods for a
bicycle:
Two instances of the bicycle class could be:
A class can define two types of variables and methods: class and
instance. The bicycle class above defines instance variables and classes. Each
object of the bicycle class can assign its own value to the variables. However, if we
define the bicycle class and make an object from the class this way:
Assuming that the bicycle class contains the same instance variables as before, the
variable numberOfGears is a class
variable. That is, this property is shared by all objects of the class
(bicycles). If the class assigns a value (e.g. 18) to this variable, then all instances of
the class will use that value.
The difference between instance methods and class methods may be less easy to
understand, but it can be summarized in two points:
- Instance methods operate on the instance variables of the
current object, and also have access to the class variables.
- Class methods can only access class variables directly.
They can only access instance variables through a specific instance of the class (object).
Also, since class methods are not applied to a specific object of the class, you call them
without specifying an instance.
By default, unless otherwise specified, a member (i.e., either a variable or a method)
declared within a class is an instance member. If you are still a little confused about
instance and class variables and methods, do not worry for now; the difference should
become more obvious when you see them in Java code.
What is Inheritance?
In the real world, cats and lions are felines, and all felines are mammals. This type
of relationship defines an inheritance hierarchy depicted as:
The same pattern applies to object oriented design. Child (derived) classes, also
called subclasses, can inherit properties and methods from a
parent (base) class, also called superclass:
A subclass inherits instance variables and methods from a superclass. A subclass can
also have its own methods, which can override or extend methods of the superclass. This
means we do not have to rewrite methods over and over for each class that needs to use
them. When a subclass inherits from a superclass, the subclass obtains a copy of all the
methods and instance variables contained in the superclass' class definition. Consider an
example class called employee that represents people
working at a company. All employee objects contain
methods such as:
- add_skill - add a skill the employee
knows
- has_skill - check to see if an employee
has a given skill
- salary - determine how much to pay an
employee
Then we can use a class hourly to represent
employees who are paid by the hour. Class hourly is a
subclass of class employee, and inherits all of its
instance variables and methods. Thus, an hourly
object can respond to methods such as add_skill
and has_skill, even though they are not written
explicitly in the definitions of the hourly class.
Stated simply, inheritance allows you to start with a previously defined base class
(e.g., employee) and then modify or extend this base
class in any way you like. Recall that it is possible to add new methods or instance
variables to a subclass. For example, the hourly
class could implement the method hours, which
calculates the number of hours an employee has worked over a period of time. Moreover, a
subclass can replace or override methods that it
inherits. For instance, the hourly class can redefine
the method salary that it inherited from the
employee class.
We usually use inheritance in two types of situations:
- when a subclass is a minor modification of its superclass.
- when many subclasses share common high-level data structures and/or methods. For
example, platypus, llama, and rhinoceros would be subclasses of a mammal class.
We are not limited to just one layer of inheritance. The inheritance tree, or class
hierarchy, can be as deep as needed. Methods and variables are inherited down through the
levels. In general, the farther down in the hierarchy a class appears, the more
specialized its behavior.
What is an Interface?
Later you will learn that in Java, interfaces are as important as classes. An
interface can be used to interact with objects
without knowing exactly which class they belong to. For example, the header files in C++
programs are called interface files because they
contain prototypes for all functions implemented in the corresponding source files. Using
these interface files, classes implemented in the program can interact with each other by
calling each other's methods or referencing each other's variables.