Learning Outcome F.1. Creating User-Defined Classes in Programs To Promote Software Reuse. - Classes and Objects
Learning Outcome F.1. Creating User-Defined Classes in Programs To Promote Software Reuse. - Classes and Objects
(PPB115D/PPG115D/PPBFD/PPGF15D)
Compiled by
V. Booi
And
V. Memani
All rights reserved. Apart from any fair dealing for the purpose of research
criticism or review as permitted under Copyright Act, no part of this
document may be reproduced or transmitted in any other form or by any
means, electronic
Learning outcome:or mechanical,
C.2. includingclasses
Using predefined photocopy and recording, without
in programs.
permission in writing from the publisher.
Page 1 of 9
Learning outcome: G.1. Creating user-defined classes in programs to promote
software reuse.
Objects are key to understanding object-oriented technology. Look around right now and you'll
find many examples of real-world objects: your dog, your desk, your television set, your bicycle.
Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).
Bicycles also have state (current gear, current pedal cadence, current speed) and behavior
(changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for
real-world objects is a great way to begin thinking in terms of object-oriented programming.
Take a minute right now to observe the real-world objects that are in your immediate area. For
each object that you see, ask yourself two questions: "What possible states can this object be
in?" and "What possible behavior can this object perform?". Make sure to write down your
observations. As you do, you'll notice that real-world objects vary in complexity; your desktop
lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn
off), but your desktop radio might have additional states (on, off, current volume, current
station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and
tune). You may also notice that some objects, in turn, will also contain other objects. These
real-world observations all translate into the world of object-oriented programming.
Software objects are conceptually similar to real-world objects: they too consist of state and
related behavior. An object stores its state in fields (variables in some programming languages)
and exposes its behavior through methods (functions in some programming languages).
Methods operate on an object's internal state and serve as the primary mechanism for object-
to-object communication. Hiding internal state and requiring all interaction to be performed
through an object's methods is known as data encapsulation — a fundamental principle of
object-oriented programming.
Page 2 of 9
By attributing state (current speed, current pedal cadence, and current gear) and providing
methods for changing that state, the object remains in control of how the outside world is
allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could
reject any value that is less than 1 or greater than 6.
Bundling code into individual software objects provides a number of benefits, including:
1. Modularity: The source code for an object can be written and maintained independently
of the source code for other objects. Once created, an object can be easily passed
around inside the system.
4. Pluggability and debugging ease: If a particular object turns out to be problematic, you
can simply remove it from your application and plug in a different object as its
replacement. This is analogous to fixing mechanical problems in the real world. If a bolt
breaks, you replace it, not the entire machine.
2. What is a class?
A class is the blueprint from which individual objects are created. In the real world, you'll often
find many individual objects all of the same kind. There may be thousands of other bicycles in
existence, all of the same make and model. Each bicycle was built from the same set of
Page 3 of 9
blueprints and therefore contains the same components. In object-oriented terms, we say that
your bicycle is an instance of the class of objects known as bicycles.
You will see throughout this course and the next that we use UML class diagrams to represent a
class.
Class Name
States/Variables/Data Members
Behavior/Methods
Bicycle
+ speed: int
+ gears: int
+ ownerName: String
+ Bicycle()
+ changeGear(int): void
+ brake(int): void
+ setSpeed(int): void
+ setGears(int): void
+ setOwnerName(String): void
+ getSpeed(): int
+ getGears(): int
+ getOwnerName(): String
Page 4 of 9
The ‘+’ means public, that is an access modifier. The other two access modifiers we use if
private (-) and protected (#). In the next lesson we will learn what access modifiers means.
We will discuss data members and methods later; let’s first look at the syntax for creating a
class.
When declaring a class you must use the class keyword and the name of the class. The coding
convention for class names:
Name starts with a capital letter and every word adding to it starts with a capital letter
as well.
No spaces in the class name
Don’t use underscores and other characters
The name of the file you save this class as must the same name as the class name.
You will for the purpose of this course define all your classes as public. You may leave out the
public keyword. The data members and methods must be defined between the opening and
closing braces.
Page 5 of 9
2.3 Class contains:
Inside the braces of the class is where you have to define the data members (state) and
behavior (methods) of the class. The following illustrate how this will look like:
//define methods
In the next lessons we will look at defining the data members and also defining your methods.
A Class in Java
Structure of a class:
A class consists of the following components:
Comments
Import statements
Class declaration
Methods and data members
Page 6 of 9
/*
Chapter 2 Sample Program: Displaying a Window
File: Ch2Sample2.java Comments
*/
import javax.swing.*;
Import
class Ch2Sample1 Statements
{
//data members
Class
Name
//methods
} Class Body
Comments:
Comments in Java have two distinct meanings. The first is to describe what the class is all about
and the second is to give description about the code. Comments to describe the code can also
be used to describe methods and as you will learn in a later lesson after we completed the
methods topic we will learn how to use comments to create Java doc files.
Class comments are written at the top of the class to describe the class. This includes
information like the author, version, and description of the class. When you create a new class
file in the Notepad++ editor you will have to add these comments. Below is an example.
Open Notepad++
Add the followings lines of comments in the new file and save the file as
JavaClassComments.java:
/**
*
* This is an example of the class comments.
*
* @author Your Student Number
* @version 1.00
Page 7 of 9
*/
public class JavaClassComments
{
public JavaComments()
{
}
}
The part in red is the class comments. It is also Java doc comments because it starts with a
forward slash, followed by two asterisks, and every line contains one asterisk. It ends with a
forward slash again. The at sign is used to add Java doc comments like the author and the
version but this will be described in a later document. It is important that you must at least add
the description of the class.
Import statement:
We will look at this section when we cover working with predefined classes in Java.
Class Declaration:
The class keyword is used to identify it to be a class declaration. You may not use the
class keyword anywhere else. Make user it is all lower case. The editor will identify it as
a keyword by changing the color of the word. This way you will also be able to identify
all the other keywords.
The class name is the name of your class. You must follow these coding conventions
when creating the names off your classes.
o Name starts with a capital letter and every word adding to it starts with a capital
letter as well.
o No spaces in the class name
o Don’t use underscores and other characters
Page 8 of 9
o The name of the file you save this class as must the same name as the class
name.
Inside the braces of the class is where you have to define the data members/fields/attributes
(state) and behavior (methods) of the class. The following illustrate how this will look like:
Class Name
Data Members
Methods
Page 9 of 9