CSC202 NOTE
CSC202 NOTE
CSC202 NOTE
Omede]
1.0 INTRODUCTION
The category of those programming languages that support the object- oriented
programming paradigm are the main object-oriented programming languages
which are: Ada programming language, C Sharp programming language family,
C++, Fortran, Java programming language, Smalltalk programming language
family.
2.0 OBJECTIVES
1
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
3.1.1 Objects
By attributing state (current speed, 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.
2
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
To create an object of a particular class, use the new operator. For example,
now that there is a constructor for the Box class you can make specific
instances or discrete copies of a box by using the assignment operator and the
new memory allocation operator as in:
Once a class has been specified, a data type exists with the same name. You
do not need to destroy or remove an object when it is no longer needed. Java
automatically flags unused objects and applies garbage collection when
appropriate. However you may occasionally need to use finalise() method to
insure that a non-Java resource such as a file handle or a window font character
is released first. The general form is:
void finalise ( )
{
/ / cleanup code goes here
Super.finalise ( ) / / parent too! }
Object variables and methods are accessed using dot notation. Use
instance_name.variable or instance_name.method_name(args) to reference
instance objects declared with new. Use class_name.variable or
class_name.method_name(args) to reference static variables or methods.
3.1.2 Classes
3
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Where modifier(s) are used to alter the behaviour of the class and, usually, class
members consist of named constants, variable declarations, and/ or methods.
That is, a member of a class can be either a variable (to store data) or a
method. Some of the modifiers that are used are public, private, and static.
Note:
In Java, class is a reserved word, and it defines only a data type; no memory is
allocated. It announces the declaration of a class. The data members of a class
are also called fields. The members of a class are usually classified into three
categories: private, public and protected. These will be discussed later.
In the real world, one often finds out that many individual objects are 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 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. A
class is the blueprint from which individual objects are created. The following
class is one possible implementation of a bicycle:
class Bicycle {
int cadence = 0;
int speed = 0; int
gear = 1;
4
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
void printStates() {
System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);
}
}
The syntax of the Java programming language will look new to you, but the
design of this class is based on the previous discussion of bicycle objects. The
field‘s cadence, speed, and gear represent the object's state, and the methods
(changeCadence, changeGear, speedUp etc.) define its interaction with the
outside world. You may have noticed that the Bicycle class does not contain a
main method. That's because it's not a complete application; it's just the
blueprint for bicycles that might be used in an application. The responsibility of
creating and using new Bicycle objects belongs to some other class in your
application. The next example is a BicycleDemo class that creates two separate
Bicycle objects and invokes their methods:
bike2.changeCadence(50);
bike2.speedUp(10); bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10); bike2.changeGear(3);
bike2.printStates();
}
}
The output of this test prints the ending pedal cadence, speed, and gear for the
two bicycles:
5
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Once an object of a class is created, the object can access the members of the
class. The general syntax for an object to access a data member or a method is:
referenceVariableName.memberName
The class members that the class object can access depend on where the
object is created.
• If the object is created in the definition of a method of the class, then the
object can access both the public and private members.
• If the object is created elsewhere (for example, in a user‘s program) then
the object can access only the public members of the class.
A class specifies the properties (data) and methods (actions) that objects can
work with. It is a template or prototype for each of the many objects made to the
class design. The syntax for a class is:
The first optional group indicates the visibility (public) or scope of accessibility
from other objects. The default is package or visible within the current package
only. The second optional group indicates the capability of a class to be
inherited or extended by other classes. Abstract classes must be extended and
final classes can never be extended by inheritance. The default indicates that
the class may or may not be extended at the programmers‘ discretion.
6
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
{
// what are the properties or fields private int
length, width, height; // what are the actions
or methods public void setLenght (int p)
{length=p;} public void setWidth (int p)
{width=p;}
public void setHeight (int p) {height=p;} public int
displayVolume( )
{System.out.println(length*width*height);}
}
Note: There is no main method in a class defining template! Class names begin
with a capital. Use lowercase for all other names. It is a good programming
practice to write separate files for the class templates and the driver or main
user program. This allows separate compilation as well as class reuse by other
driver programmes. A class file can contain more than one associated class but
normally its filename is that of the first defined file. A driver program is named
the same as the class that contains the main(). Its file may contain other classes
as well.
3.1.2.3 Properties
Properties are sometimes called field variables or states. To declare a property, use the
following syntax:
The items in the first optional group indicate the visibility or accessibility from
other objects; public means visible everywhere (global); private indicates
accessible only to this class and nested classes; protected means visible to this
class or inherited (extended) classes only; final indicates continuous retention
and unchangeable after initial assignment (it is read only or constant). The
default is friendly or visible within the current package only. The third optional
group indicates how long a value is retained in the variable. Static indicates that
the value is shared by all members of the class and exists for all runtime. Static
properties can be referenced without creating an instance of the class. Transient
prevents the variable from being transferred during a serial operation such as
file i/o. Volatile is used in multi- threading to prevent overwrite issues. Many
programmers make all properties private and force access through public
accessors and mutators which can include validation steps.
7
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Accessibility keywords are the same as for properties. The default (omitted) is
package (friendly) or visible within the current package only. Static methods are
shared by all members and exist for all runtime. Static methods can be
referenced without creating an instance of the class. Abstract methods must be
redefined on inheritance. Native methods are written in C but accessible from
Java. The return data type defines the type of value that the calling routine
receives from the object (the reply message in object terminology). It can be any
of the primitive types or the reserved word void (default value) if no message is
to be returned. The statement returns varName; is used to declare the value to
be returned to the calling routine.
The parameter list can contain from zero to many entries of datatype varName
pairs. Entries are separated by commas. Parameters are passed by value, thus
upholding the encapsulation principle by not allowing unexpected changes or
side effects. Object references (such as arrays) can also be passed. The
projects page has some simple problems based on array passing. Some
examples of method header parameter lists are:
3.1.2.4.1Types of Methods
8
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Note: That there is no class keyword or return data type keyword. Also the
method name is the same as the class name. This is what marks the fragment
as a constructor method. If no constructor method is defined for a class, a
default constructor is automatically used to initialise all fields to 0, false or
unicode(0) as appropriate to the data type. The best programming device is to
declare the constructor with no parameters as private and use it to initialise all
properties. Then other constructors can first call it using this() and then do their
own specific property validations/initialisation.
Accessor (or observer) methods read property (i.e. field variable) values and are
conventionally named getFoobar() or whatever the property is called while
Mutator (or transformer) methods set property values and are often named
setFoobar() etc. Mutators can be used to ensure that the property's value is
valid in both range and type.
It is a good programming practice to make each property in a class private and include
accessor and mutator methods for them. This is a good example of object
encapsulation. The exceptions to writing accessor/mutator methods for each property is
for those that are used only within the class itself or for properties that are set in more
complex ways. Helper methods are those routines that are useful within the class
methods but not outside the class. They can help in code modularisation. Normally they
are assigned private access to restrict use. Recursive methods are methods that are
defined in terms of it. A classic recursion is factorials where n factorial is the product of
positive integer n and all the products before it down to one. In Java this could be
programmed as:
Class Factorial
{
Int factorial ( int n)
{
If ( n= =1 ) { return 1};
Return ( n * factorial (n-1 ) ) ; }
}
Note: This short method is not very well written as negative and floating calling
parameters are illegal in factorials and will cause problems in terminating the
loop. Bad input should always be trapped.
Inner classes are classes nested inside another class. They have access to the
outer class fields and methods even if marked as private. Inner classes are used
primarily for data structures, helper classes, and event handlers. A brief
example of how an inner class can be used as a data structure is:
9
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
{
// inner class defines the required structure
String first;
String last;
}
// outer class creates array of person objects with specific properties
// the objects can be referenced by personArray[1].last for example
Person personArray[]={new Person(), new Person(), new Person()};
}
4.0 CONCLUSION
A class normally consists of one or more variables that represent the attributes
of a particular object of a class. These variables are called instance variables.
Methods within the class manipulate these variables. Most instance variable
declarations are preceded with the private access modifier. Variables or
methods declared with access modifier private are accessible only to methods
of the class in which they are declared. Classes often provide public methods
that allow clients of the class to set or get private instance variables. The names
of these methods need not begin with set or get, but this naming convention is
highly recommended in Java.
5.0 SUMMARY
10
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
28.25999. Once it is working, see if you can factor it into two files (Circle and
MakeCircle) and compile separately, (the Circle class file first). As an
enhancement, you may want to add a command line interpreter to get the radius
for the driver to use.
7.0 REFERENCES/FURTHER READING
Malik, D.S. (2006). Java Programming: From Problem Analysis to Design (2nd
ed.).
11
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
2.0 Objectives
3.0 Main Content
3.1 The Model
3.2 Influences on other Programming Models
3.3 Encapsulation
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
2.0 OBJECTIVES
Messages are also commonly used in the same sense as a means of inter-
process communication; the other common technique being streams or pipes, in
12
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
which data are sent as a sequence of elementary data items instead (the higher-
level version of a virtual circuit). Examples of Message passing styles are:
a. Actor model implementation
b. Amorphous computing
c. Antiobjects
d. Flow-based programming
e. SOAP (protocol)
3.3 Encapsulation
In OOD, objects combines data and operations on that data in a single unit, the
feature called encapsulation. The object is first identified, then the relevant data
and then the operations which are needed to manipulate the objects.
13
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
private can only be seen or modified through the use of public accessor and
mutator methods. This permits validity checking at run time. Access to other
variables can be allowed but with tight control on how it is done. Methods can
also be completely hidden from external use. Those that are made visible
externally can only be called by using the object's front door (i.e. there is no
‗goto‘ branching concept).
Encapsulation is the concept of hiding the implementation details of a class and
allowing access to the class through a public interface. For this, there is need to
declare the instance variables of the class as private or protected. The client
code should access only the public methods rather than accessing the data
directly. Also, the methods should follow the Java Bean's naming convention of
set and get. Encapsulation makes it easy to maintain and modify code. The
client code is not affected when the internal implementation of the code changes
as long as the public method signatures are unchanged. For instance:
4.0 CONCLUSION
14
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
2.0 Objectives
3.0 Main Content
3.1 What is Inheritance?
3.2 Building Inheritance Hierarchies
3.3 Inheriting Interface and Implementation
3.4 Hiding Fields
3.4.1 Abstract Classes and Methods
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
15
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
1.0 INTRODUCTION
In Java, the mechanism that allows us to extend the definition of a class without
making any physical changes to the class is the principle of inheritance.
Inheritance has to do with relationship. It helps in the creation of new classes
from existing classes.
2.0 OBJECTIVES
• define inheritance
• differentiate between a subclass and a super class outline the uses of a super
class
• differentiate between inheritance and overridden.
For instance, once the problem domain is partitioned into types, one will likely
want to model relationships in which one type is a more specific or specialised
version of another. For example you may have identified in your problem
domain two types, Cup and CoffeeCup, and you want to be able to express in
your solution that a CoffeeCup is a more specific kind of Cup. In an object-
oriented design, you model this kind of relationship between types with
inheritance.
16
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
class Cup {
}
class CoffeeCup extends Cup {
}
class CoffeeMug extends CoffeeCup {
}
17
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
An inheritance hierarchy, such as the one shown in Figure 3.1, defines a family
of types. The most general class in a family of types, the one at the root of the
inheritance hierarchy is called the base class. In Figure 3.1, the base class is
Cup. Because every class defines a new type, you can use the word ―type‖ in
many places you can use ―class.‖ For example, a base class is a base type, a
subclass is a subtype, and a direct superclass is a direct supertype. In Java,
every class descends from one common base class: Object. The declaration of
class Cup above could have been written:
This declaration of Cup has the same effect as the earlier one that excluded the
"extends Object" clause. If a class is declared with no extends clause, it by
default extends the Object class. (The only exception to this rule is class Object
itself, which has no superclass.) The inheritance hierarchy of Figure 3.1 could
also have shown the
Object class hovering above the Cup class, in its rightful place as the
most super of all superclasses. In this case, class Object remained invisible,
because the purpose of the figure was to focus on one particular family of types,
the Cup family.
Modeling is-a relationship is called inheritance because the subclass inherits the
interface and, by default, the implementation of the superclass. Inheritance of
interface guarantees that a subclass can accept all the same messages as its
superclass. A subclass object can, in fact, be used anywhere a superclass
object is called for. For example, a CoffeeCup as defined in Figure 3.1 can be
used anywhere a Cup is needed. This substitutability of a subclass (a more
specific type) for a superclass (a more general type) works because the
subclass accepts all the same messages as the superclass. In a Java program,
this means you can invoke on a subclass object any method you can invoke on
the superclass object.
18
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
When you override a method, you can make the access permission more public,
but you cannot make it less public. So far, you have only been introduced to two
access levels, public and private. There are, however, two other access levels
that sit in-between public and private, which form the two ends of the access-
level spectrum. In the case of the addLiquid() method, because class Cup
declares it with public access, class CoffeeCup must declare it public also. If
CoffeeCup attempted to override addLiquid() with any other access level, class
CoffeeCup would not compile.
19
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
checks to see whether the coffee cup is in the northern or southern hemisphere
of the planet, and based on that information, decide which way to swirl.
If you define a field in a subclass that has the same name as an accessible field
in its superclass, the subclass‘s field hides the superclass‘s version. (The type
of the variables need not match, just the names.) For example, if a superclass
declares a public field, subclasses will either inherit or hide it. (You can‘t
override a field.) If a subclass hides a field, the superclass's version is still part
of the subclass's object data; however, the subclass doesn‘t ―inherit‖ the
superclass‘s version of the field, because methods in the subclass can‘t access
the superclass‘s version of the field by its simple name. They can only access
the subclass‘s version of the field by its simple name. You can access the
superclass‘s version by qualifying the simple name with the super keyword, as
in super.fieldName.
20
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Java permits you to declare a field in a subclass with the same name as a field
in a superclass so you can add fields to a class without worrying about breaking
compatibility with already existing subclasses. For example, you may publish a
library of classes that your customers can use in their programmes. If your
customers subclass the classes in your library, you will likely have no idea what
new fields they have declared in their subclasses. In making enhancements to
your library, you may inadvertently add a field that has the same name as a field
in one of your customer's subclasses. If Java didn't permit field hiding, the next
time you released your library, your customer's program might not run properly,
because the like- named field in the subclass would clash with the new field in
the superclass from your library. Java's willingness to tolerate hidden fields
makes subclasses more accepting of changes in their superclasses.
Java provides a way to declare a class as conceptual only, not one that
represents actual objects, but one that represents a category of types. Such
classes are called abstract classes. To mark a class as abstract in Java, you
merely declare it with the abstract keyword. The abstract keyword indicates the
class should not be instantiated. Neither the Java compiler nor the Java Virtual
Machine will allow an abstract class to be instantiated. The syntax is
straightforward:
// In Source Packet in file inherit/ex6/Liquid.java
abstract class Liquid { void swirl(boolean clockwise) {
System.out.println("One Liquid object is swirling.");
}
static void gurgle() {
System.out.println("All Liquid objects are gurgling."); }
}
The above code makes Liquid a place holder in the family tree, unable to be an
object in its own right.
Note that the Liquid class shown above still intends to implement a default
behaviour for swirling and gurgling. This is perfectly fine; however, classes are
often made abstract when it doesn't make sense to implement all of the
methods of the class's interface. The abstract keyword can be used on methods
as well as classes, to indicate that the method is part of the interface of the
21
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
class, but does not have any implementation in that class. Any class with one or
more abstract methods is itself abstract and must be declared as such. In the
Liquid class, you may decide that there is no such thing as a default swirling
behaviour that all liquids share. If so, you can declare the swirl() method
abstract and forgo an implementation, as shown below:
In the above declaration of Liquid, the swirl() method is part of Liquid's interface,
but doesn't have an implementation. Any subclasses that descend from the
Liquid class shown above will have to either implement swirl() or declare
themselves abstract. For example, if you decided there were so many varieties
of coffee that there is no sensible default implementation for Coffee, you could
neglect to implement swirl() in Coffee. In that case, however, you would need to
declare Coffee abstract. If you didn't, you would get a compiler error when you
attempted to compile the Coffee class. You would have to subclass Coffee (for
example: Latte, Espresso, CafeAuLait) and implement swirl() in the subclasses,
if you wanted the Coffee type to ever see any action.
Most often you will place abstract classes at the upper regions of your
inheritance hierarchy, and non- abstract classes at the bottom. Nevertheless,
Java does allow you to declare an abstract subclass of a non- abstract
superclass. For example, you can declare a method inherited from a non-
abstract superclass as abstract in the subclass, thereby rendering the method
abstract at that point in the inheritance hierarchy. This design implies that the
default implementation of the method is not applicable to that section of the
hierarchy. As long as you implement the method again further down the
hierarchy, this design would yield an abstract class sandwiched in the
inheritance hierarchy between a non-abstract superclass and non- abstract
subclasses.
4.0 CONCLUSION
22
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
5.0 SUMMARY
• The generic class is known as the superclass or base class and the
specific classes as subclasses or derived classes. The concept of
inheritance greatly enhances the ability to reuse code as well as making
design a much simpler and cleaner process.
• In Java terminology, a more general class in an inheritance hierarchy is
called a superclass. A more specific class is a subclass.
• When a class has only one direct super class, it is referred to as single
inheritance. In contrasts with a multiple inheritance, in which a class can
have multiple direct superclasses.
• Overriding methods is a primary way a subclass specialises its
behaviour with respect to its superclass.
• Well-designed classes most often refuse other classes direct access to
their non-constant fields, and this policy generally extends to subclasses
as well. If a super class has private fields, those fields will be part of the
object data in its subclasses, but they will not be "inherited" by the
subclass.
• Java permits one to declare a field in a subclass with the same name as
a field in a superclass so that one can add fields to a class without
worrying about breaking compatibility with already existing subclasses.
23
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
24
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
1.0 Objectives
3.0 Main Content
3.1 What is Polymorphism?
3.2 Types of Polymorphism
3.3 Modularity
3.3.1 Methods
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
Polymorphism is the capability of an action or method to do different things
based on the object that it is acting upon. This is the third basic principle of
object-oriented programming. Overloading and overriding are two types of
polymorphism.
2.0 OBJECTIVES
At the end of this unit, you should be able to:
• describe polymorphism
• state types of polymorphism
• define modular programming
• list the functions of modular programming.
25
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
The statement in line 5 makes nameRef point to the object employee. After
executing the statement in line 5, the object nameRef is treated as an object of
the class PartTimeEmployee. The statement in line 6 outputs the value of the
object nameRef. The output of the statement in line 6 is:
nameRef: Susan Johnson’s wages are : $ 224.0 //Line 7
Notice that even though nameRef is declared as a reference variable of the type
Person, when the program executes, the statement in line 7 outputs the first
name, the last name, and the wages. This is because when the statement in line
6 executes to output nameRef, the method toString of the class
PartTimeEmployee executes, not the methodtoString of the class Person. This
is called late binding, dynamic binding, or run-time binding; that is, the method
that gets executed is determined at execution time, not at compile time.
In a class of hierarchy, several methods may have the same name and the
same formal parameter list. Moreover, a reference variable of a class can refer
to either an object of its own class or an object of its subclass. Therefore, a
reference variable can invoke, that is, execute, a method of its own class or of
its subclass(es). Binding means associating a method definition with its
invocation, that is, determining which method definition gets executed. In early
binding, a method‘s definition is associated with the method‘s invocation at
execution time, that is, when the method is executed. Except for a few cases,
java uses late binding for all methods. Furthermore, the term polymorphism
means assigning multiple meanings to the same method name.
The reference variable name or nameRef can point to any object of the class
Person or the class PartTimeEmployee. Loosely speaking, one could say that
these reference variables have many forms, that is, they are polymorphism
reference variables. They can refer to either objects of their own class or objects
of the subclasses inherited from their class.
The following example further explains polymorphism.
Example 4.1
26
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
public rectangleFigure ( )
{
length = 0;
width = 0;
}
public RectangleFigure ( double 1, double w );
{ setDimension ( 1, w );
}
public void setDimension ( double 1, double w)
{ if ( 1 >= 0 ) length = 1 ;
else length = 0 ;
if ( w <= 0 ) width
= w; else
}
public double getwidth ( )
{ return length ;
}
public double getwidth ( )
{ return;
}
public double area ( )
{
return length * width;
} public double perimeter( )
{
return 2 * ( length + width );
}
public void print( )
{
System.out.print(“ Length = “ + length + “ ; Width = “ + width
+” \n” + “ Area = “ + area ( ));
}
}
The two main types of polymorphism are overloaded method and overridden
methods. Overloaded methods are methods with the same name signature but
either a different number of parameters or different types in the parameter list.
For example 'spinning' a number may mean increase it, 'spinning' an image may
mean rotate it by 90 degrees. By defining a method for handling each type of
parameter you achieve the effect that you want. Overridden methods are
methods that are redefined within an inherited or subclass. They have the same
signature and the subclass definition is used. The third type is dynamic method
binding. These will be discussed in details in module two.
27
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
3.3 Modularity
Methods in Java are like automobile parts‘, they are building blocks. Methods
are used to divide complicated program into manageable pieces. They are both
predefined methods, methods that are already written and provided by java, and
user-defined methods, methods that you create. Using methods has several
advantages:
• While working on one method, you can focus on just that part of the
program and construct it, debug it and perfect it.
• Different people can work on different methods simultaneously.
• If a method is needed in more than one place in a program, or in
different program, you can write it once and use it many times.
• Using methods greatly enhance the programmes readability because it
reduces the complexity of the main method.
• Methods are often called modules. They are like miniature programmes;
you can put them together to form a larger program. The ability is less
apparent with the predefined methods because their programming code
is not available to us. However, because predefined methods are
already written for us, this will be learnt first, so that it could be used
when needed. To use a predefined method in your program (s), you
need to know only how to use it.
Figure 4.1 gives a picture of what modular programming is all about. The main
program coordinates calls to procedures in separate modules and hands over
appropriate data as parameters.
28
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
through procedure calls and which form the whole program. Each module can
have its own data. This allows each module to manage an internal state which is
modified by calls to procedures of this module. However, there is only one state
per module and each module exists at most once in the whole program.
3.3.1 Methods
A car can be seen as an object of a class vehicle. When you drive a car,
pressing its accelerator sends a message to the car to perform a task i.e. make
the car move faster. Similarly, messages are sent to an object of a class. Each
message is known as a method call and tells a method of the object to perform
its task. Methods are used to facilitate the design, implementation, operation
and maintenance of large programmes. A method is invoked by a method call,
and when the called method completes its task, it either returns a result or
simply the control to the caller.
In the above syntax, accessmodifier describes how other methods in the class
will be able to access this method. The accessmodifier can either be public or
private. A private accessmodifier makes the method only accessible to the
members of the class; it cannot be used outside the class. A public
accessmodifier makes the method accessible by other members of the class
and it can also be used outside the class. The returntype describes the data
type the method will return to its caller. Return type can be any valid data type.
A method that has a return type of int returns an integer to its caller, a method
that has a return type of String value returns a String value to its caller.
Sometimes a method may not return any value to its caller. Such methods have
a return type of void.
29
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
The methodname is any valid variable name. The method may or may not have
parameters depending on the method action. If a method has parameters, when
such method is to be called, the signature of the arguments must be equal to the
specified signature in the called method. Note that signature refers to the
number of arguments and the data type of the arguments. Just like classes,
methods have a method body that begins and ends respectively with a left brace
―{―and a right brace ―}‖. A method may or may not return a value. A method
that has a return type of void does not return any value to its caller, thus, the
second to the last line is absent i.e. return value. It is important to Note that for
methods that do not have a return type of void, the last line before the closing
brace in the method should be the return value.
4.0 CONCLUSION
Polymorphism in java is implemented using late binding. The two main types of
polymorphism are overloaded method and overridden methods. Overloaded
methods are methods with the same name signature but either a different
number of parameters or different types in the parameter list.
Methods are often called modules. They are like miniature programmes; you
can put them together to form a larger program. Modular programming involves
the procedures of a common functionality which are grouped together into
separate modules. A program therefore no longer consists of only one single
part. It is now divided into several smaller parts which interact through
procedure calls and which form the whole program.
5.0 SUMMARY
In this unit, the following have been discussed:
• Several methods may have the same name and the same formal
parameter list in a class of hierarchy.
• A reference variable can invoke, that is, execute, a method of its own
class or of its subclass(es). Binding means associating a method
definition with its invocation, that is, determining which method definition
gets executed.
• Overloaded methods are methods with the same name signature but
either a different number of parameters or different types in the
parameter list.
• Methods are often called modules. They are like miniature programmes;
you can put them together to form a larger program. With modular
programming procedures, common functionality is grouped together into
separate modules.
• Although, most methods execute in response to method calls on specific
objects, this is not always the case. Sometimes a method performs a
task that does not depend on the contents of any object. Such a method
is called a static method.
30
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
2.0 Objectives
3.0 Main Content
3.1 Composition
3.2 Abstraction
3.2.1 Abstract Classes
3.2.2 Interfaces
3.2.3 Abstract Data Types (ADT)
3.2.4 Importance of Data Structure Encapsulation
3.2.5 Generic Abstract Data Types
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
Volumes have been written regarding the application layers, web sites. One of
the fundamental activities of any software system design is establishing
relationships between classes. Two fundamental ways to relate classes are
inheritance and composition. Although the compiler and Java virtual machine
(JVM) will do a lot of work for you when you use inheritance, you can also get at
the functionality of inheritance when you use composition. This unit discusses
and compares these two approaches to relating classes and will also provide
guidelines on their use.
2.0 OBJECTIVES
31
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
• define composition
• describe abstraction define an abstract class
list abstract data types.
3.0 MAIN CONTENT
3.1 Composition
Examples 1.5.1: Imagine you create a Student class that holds information
about individual students at a school. One piece of information stored is the
student's date of birth. It's held in a Gregorian Calendar object:
Student {
It might be useful if the coffee cup object of a program could contain coffee.
Coffee itself could be a distinct class, which your program could instantiate. One
would recognise coffee with a type if it exhibits behaviour that is important to
your solution. Perhaps it will swirl one way or another when stirred, keep track of
32
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
a temperature that changes over time, or keep track of the proportions of coffee
and any additives such as cream and sugar.
To use composition in Java, you use instance variables of one object to hold
references to other objects. For the CoffeeCup example, you could create a field
for coffee within the definition of class CoffeeCup, as shown below by
implementing the methods.
// No implementation yet //
(return 0 so it will compile)
return 0;
}
33
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
In the example above, the CoffeeCup class contains a reference to one other
object, an object of type Coffee. Class Coffee is defined as a separate source
file. The relationship modeled by composition is often referred to as the "has-a"
relationship. In this case a CoffeeCup has Coffee. As you can see from this
example, the has-a relationship doesn't mean that the containing object must
have a constituent object at all times, but that the containing object may have a
constituent object at some time. Therefore the CoffeeCup may at some time
contain Coffee, but it need not contain Coffee all the time. (When a CoffeeCup
object does not contain Coffee, its innerCoffee field is null.) In addition, note that
the object contained can change throughout the course of the containing
object's life.
3.2 Abstraction
In the process of abstraction, the programmer tries to ensure that the entity is
named in a manner that will make sense and that it will have all the relevant
aspects included and none of the extraneous ones. A real- world analogy of
abstraction might work like this: You (the object) are arranging to meet a blind
date and are deciding what to tell them so that they can recognise you in the
restaurant. You decide to include the information about where you will be
located, your height, hair colour, and the colour of your jacket. This is all the
data that will help the procedure work smoothly. You should include all that
information. On the other hand, there are a lot of bits of information about you
34
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
that aren't relevant to this situation: your social security number, your admiration
for obscure films, and what you took to "show and tell" in fifth grade are all
irrelevant to this particular situation because they won't help your date find you.
However, since entities may have any number of abstractions, you may get to
use them in another procedure in the future.
From the previous example, the superclass is more general than its
subclass(es). The superclass contains elements and properties common to all of
the subclasses. The previous example was of a concrete superclass that
instance objects can be created from. Often, the superclass will be set up as an
abstract class which does not allow objects of its prototype to be created. In this
case, only objects of the subclass are used. To do this, the reserved word
abstract is included in the class definition.
3.2.2 Interfaces
Interfaces are similar to abstract classes but all methods are abstract and all
properties are static final. Interfaces can be inherited (i.e. you can have a sub-
interface). As with classes, the extend keyword is used for inheritance. Java
does not allow multiple inheritances for classes (i.e. a subclass being the
extension of more than one superclass). An interface is used to tie elements of
several classes together. Interfaces are also used to separate design from
coding as class method headers are specified but not their bodies. This allows
compilation and parameter consistency testing prior to the coding phase.
Interfaces are also used to set up unit testing frameworks.
35
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
When you create a class that uses an interface, you reference the interface with
the phrase implements interface list. Interface list consists one or more
interfaces, as multiple interfaces are allowed. Any class that implements an
interface must include code for all methods in the interface. This ensures
commonality between interfaced objects.
1. It exports a type.
2. It exports a set of operations. This set is called interface.
3. Operations of the interface are the one and only access mechanism to
the type's data structure.
4. Axioms and preconditions define the application domain of the type.
With the first property, it is possible to create more than one instance of an ADT
as exemplified with the employee example. You might also remember the list
36
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
“Let l=(d1, d2, d3, ..., dN) be a list. Then l.append(dM) results in l=(d1, d2, d3, ...,
dN, dM).”
“The first element of a list can only be deleted if the list is not empty.”
However, all of these properties are only valid due to our understanding of and
our discipline in using the list module. It is in our responsibility to use instances
of List according to these rules.
The principle of hiding the used data structure and to only provide a well-defined
interface is known as encapsulation. Why is it so important to encapsulate the
data structure?
37
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
you can use it in the same way like other well-known data types such as
integers.
ADTs are used to define a new type from which instances can be created. As
shown in the list example, sometimes these instances should operate on other
data types as well. For instance, one can think of lists of apples, cars or even
lists. The semantical definition of a list is always the same. Only the type of the
data elements change according to what type the list should operate on. This
additional information could be specified by a generic parameter which is
specified at instance creation time. Thus, an instance of a generic ADT is
actually an instance of a particular variant of the ADT. A list of apples can
therefore be declared as follows:
List<Apple> listOfApples;
The angle brackets now enclose the data type for which a variant of the generic
ADT List should be created. listOfApples offers the same interface as any other
list, but operates on instances of type Apple.
4.0 CONCLUSION
Abstract classes and methods force prototype standards to be followed, that is,
they provide templates. As ADTs provide an abstract view to describe properties
of sets of entities, their use is independent from a particular programming
language. Each ADT description consists of two parts:
• Data: This part describes the structure of the data used in the ADT in an
informal way.
• Operations: This part describes valid operations for this ADT, hence, it
describes its interface. We use the special operation constructor to
describe the actions which are to be performed once an entity of this
ADT is created and destructor to describe the actions which are to be
performed once an entity is destroyed. For each operation, the provided
arguments as well as preconditions and post-conditions are given.
The separation of data structures and operations and the constraint to only
access the data structure via a well-defined interface allows you to choose data
structures appropriate for the application environment.
5.0 SUMMARY
• Composition takes the relationship one step further by ensuring that the
containing object is responsible for the lifetime of the object it holds.
38
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Create a class called Employee that has fields that stores employee‘s first
name, last name, identification number, department name and salary. Provide
the necessary methods that provide meaningful output when the class is used.
Ensure data hiding. Test your class with an executable class.
Denis Caromel (1990). ―Programming Abstractions for Concurrent Programming, Pacific '90,
pp. 245-253, November 1990.
39
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
2.0 Objectives
3.0 Main Content
3.1 Classes and Objects
3.2 Characteristics of a Class
3.3 Predefined Classes and User Defined Classes
3.4 Creating a Simple Class
3.5 Object Instantiation
3.6 Local Variables and Instance Variables
3.7 The Get and Set Methods
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
This unit discusses how classes are created in Java (one of the most prominent
and widely used Object-Oriented Programming Language). The properties of
classes and how to control access to members of a class are also discussed.
Object- oriented design (OOD) models software in terms similar to those that
people use to describe real world objects. It takes advantage of class
relationships, where objects of a certain class, such as a class of vehicles have
the same characteristics, for example, cars, lorries and trailers have many
characteristics in common.
A class is the fundamental building block of code when creating object- oriented
software. A class describes in abstract all of the characteristics and behaviour of
a type of object. Once instantiated, an object is generated that has all of the
methods, properties and other behaviours defined within the class. A class
should not be confused with an object.
The class is the abstract concept for an object that is created at design- time by
the programmer. The objects based upon the class are the concrete instances
of the class that occur at run-time.
2.0 OBJECTIVES
40
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Classes are to objects as blueprints are to houses, just as a house can be built
from a blueprint. You cannot sleep in the room of a blueprint but you can sleep
in the room of a house. Many objects can be instantiated from a class just like
many houses can be built from a blueprint. OOP programmers concentrate on
creating classes and specify how those classes will behave; they later
instantiate objects of such classes to act as it has been specified in the general
class.
41
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
With software reuse, each new class you create will have the ability to become
an asset that you and other programmers can use to speed and enhance the
quality of future software development efforts. Two major aspects of Object-
Oriented Programming that promotes software reuse are Inheritance and
Polymorphism. These two concepts will be discussed later in this module.
A great strength of Java is its rich set of predefined classes that can be reused
rather than creating new classes. This is an aspect of software reuse discussed
earlier. Related classes are grouped into packages and collectively referred to
as the Java Class Library or the Java Application Programming Interface (API).
These predefined classes are the building blocks for constructing new classes.
Programmers use the import declaration to identify the predefined classes used
in a Java Program.
The syntax of the import statement is:
import packagename.classname;
If there are many classes in a package that programmers want to use, each class will
be imported individually as follows:
This will be time consuming if there are many classes to be imported, in such
instance; Programmers prefer to import all the classes in the package at once.
When this is done they will be able to access whichever class they want to use.
The syntax for importing all the classes in a package is:
import packagename.*;
Note: In Computer terminology, “*” means all.
Some packages are implicitly imported into every Java Program. Thus when the
classes contained in such packages are to be used, there‘s no need to import
such packages. An example of this is the package Java.lang which contains the
42
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
classes System and String. User defined classes are the classes newly created
by a programmer to assist in achieving his desired task. Every Java program
consist of at least one user defined class declaration that is defined by the
programmer. User defined classes are also known as Programmer defined
classes. The class keyword introduces a user defined class declaration in Java
and it is followed immediately by the class name. A Java class name is a valid
identifier i.e. a series of characters consisting of letters digits, underscores and
dollar sign that does not begin with a digit neither does it contain spaces.
The class body contains methods that specify the actions that the objects of the
class can exhibit. The class body begins and ends respectively with a left brace
―{―and a right brace ―}‖.
Example 2.1.1
Program Input:
public class FirstProgram
{ public static void main(String[] args)
{
System.out.println("I love Java Programming");
}
}
// A program to display a line of text.
Program Output:
run:
Program Analysis:
43
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
class that does not have a main method. Such classes cannot execute on their
own unless they are used inside an executable class. The public static void
main (String[]args) is the header of the main method where program execution
begins. Inside this main method we have the line of action to print out the text
―I love Java Programming‖.
Example 2.1.2
Create a non executable class that has a method. This method prints a line of text
that reads ―Object- Oriented Programming is fun‖.
Program Input:
Program Analysis:
44
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
In the syntax, Classname is the name of the class whose object is to be created
and objectname is the name of the object, new represents a call to initialise the
object. This is done by a constructor. Constructor will be discussed in details in
unit 2. The new keyword is again followed by the class name. The parenthesis
following the class name may or may not be empty depending on the
constructor. Our next example creates an executable class to instantiate the
object of the class MessageClass created earlier. The method
displayMessage() will then be called on the object of the class and the effect will
be seen.
Example 2.1.3
Program Input:
Program Output:
run:
Object- Oriented Programming is fun
BUILD SUCCESSFUL (total time: 3 seconds)
Program Analysis:
MessageClassTest is an executable class, the object of MessageClass was
created in this executable class and it is given the name msgclass. The method
displayMessage() in MessageClass is called msgclass, this makes a line of text
Object-Oriented Programming is fun to be displayed in the output.
45
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Local variables are variables that are declared in the body of a particular
method, such variables can be used only in that method. When that method
terminates, the values of its local variables are lost. Instance variable must be
initialised to their default value before they are used. The syntax for declaring an
instance variable within a method is:
Datatype variablename;
Datatype is any valid data type and variablename is any valid variable name.
Instance variables are the attributes of a class. They are declared inside the
body of a class but outside the bodies of the class‘s method declarations. They
are also called Fields. Each object of a class has its own copy of attribute.
Unlike local variables, instance variables are initialised by default by a
constructor. Constructors will be discussed in details in Unit 2. The syntax for
declaring an instance variable within a class is:
private makes the variable accessible only within the class, public makes it
accessible within and outside the class, protected makes it accessible within the
class and other subclasses of the class. You will understand how to use
protected access modifier in unit 4 (inheritance).
Example 2.1.4
Create a non executable class that has a method that computes factorial.
Program Input:
Program Analysis:
46
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
integer and the name of the argument is num. The Factorial method also has a
local variable result of integer data type. It is this variable that accumulates the
value generated by each iterative process.
Classes often provide public methods to allow clients (users) of a class to set
(assign values to) or get (obtain values of) private instance variables. These
methods are used to manipulate the instance variables of a class to respectively
assign values to them (set) and retrieve values from them (get). When this is
done, the instance variables of a class can be made private because these
methods are used to internally manipulate them within the class. Other clients of
the class do not have direct access to these private instance variables but they
can be accessed through the get and set method. This is an important OOP
concept and it is called data hiding. Another name for the get and set methods
are mutator and accessor methods.
The names of these methods need not begin with set or get but this naming
convention is highly recommended in Java. The next two examples explain how
the mutator and the accessor methods can be used to manipulate the private
instance variables of a class.
Example 2.1.5
Create a non executable class that has a private instance variable that can hold
the first name of a student. Your class should contain methods to assign values
to and retrieve values from the instance variable.
Program Input:
Program Analysis
47
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
The instance variable FirstName is a String data type because it accepts the
first name of students and it is set to be private because it will not be used
directly outside the class, it will be used by the public methods that are within
the class. Method setFirstName stores a student‘s first name. It does not return
any data when it completes its task, so its return type is void. The method
accepts one parameter name which is the first name that will be passed to the
method as an argument. In the body of this method, the value name in the
parameter is assigned to the instance variable FirstName. i.e. this method sets
the instance variable.
Example 2.1.6
Create an executable class that shows how the instance variables of the SetGetClass2 can be
manipulated by the methods of the class.
import java.util.Scanner;
class SetGetClass2Test
{ public static void main(String[] args)
{
Scanner myinput=new Scanner(System.in);
SetGetClass2 setgetclass2=new SetGetClass2();
String myname; int
myscore;
System.out.println("Unassigned first name is
"+setgetclass2.getFirstName());
System.out.println("Unassigned score is "+setgetclass2.getScore());
System.out.println("Enter your First name");
myname=myinput.nextLine(); System.out.println("Enter your
Score"); myscore=myinput.nextInt();
setgetclass2.setFirstName(myname);
setgetclass2.setScore(myscore);
System.out.println(setgetclass2.getFirstName()+" scored
"+setgetclass2.getScore());
}
}
// A Program that accepts user’s input and uses the methods of SetGetClass to
manipulate the Input.
Program Output:
48
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
run:
Unassigned first name is null
Unassigned score is 0
Enter your First name
Rebecca
Enter your Score
90
Rebecca scored 90
BUILD SUCCESSFUL (total time: 12 seconds)
Program Analysis:
The new thing you will discover in the output is that the default value of an
integer instance variable is 0. A careful examination of the lines of codes will
make you understand it better.
4.0 CONCLUSION
The instance of a class is called an object. This is one of the reasons Java is
known as an Object- Oriented Programming Language. A class may contain
one or more methods that that are designed to carry out the class‘s tasks. A
method can perform a task and return a result. Inside the method, you put the
mechanisms that make the method do its tasks. Each message sent to an
object is known as a method call and tells a method of the object‘s class to
perform its task. A method declaration that begins with keyword public indicates
that the method is ―available to the public‖- that is, it can be called by other
classes declared outside the class declaration. Keyword void indicates that a
method will perform a task but will not return any information when it completes
its task.
When you attempt to execute a class, Java looks for the class‘s main method to
begin execution. Any class that contains the main method can be used to
execute an application. A method that belongs to another class may not be
called until an object of the class is created. Variables declared in the body of a
particular method are known as local variables and can be used only in that
method. Variables declared in the body of a class are known as instance
variables.
5.0 SUMMARY
49
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
objects exist in the specific and automatically possess all the attributes
of the general class.
• Classes contain methods (class behaviours) which implement
operations and fields which implement attributes. The methods of a
class help in manipulating the fields of the class and thus provide
services to the clients of the class.
• Related classes are grouped into packages and collectively referred to
as the Java Class Library or the Java Application Programming Interface
(API).
• The methods of a non executable class will be useless if specific
instances of such classes are not created.
Review Exercise
Programming Exercise
50
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
2.0 Objectives
4.0 Main Content
4.1 Constructors
4.1.1 Default Constructors
4.2 Creating Constructors
4.2.1 Creating a No Argument Constructor
3.2.2 Making a Constructor Behave Like a Default Constructor
3.2.3 Creating an Argument Constructor
3.3 Constructor Overloading
3.4 Destructors (Tidying up)
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
In this unit, you will learn how the object of a class is been initialised. Any time
the object of a class is created, the object is implicitly initialised, and this makes
the object have a default value. Constructors are used for such initialisations.
Java provides a special kind of method, called a constructor that executes each
time an object of a class is created. The constructor is used to initialise the state
of an object. Apart from the constructor provided by Java, Programmers can
also create their constructors, In such an instance, the mode of constructor
creation looks like that of a method but care must be taken not to mistake
methods for constructors. In this unit, you will learn how to create your own
constructor and you will be able to differentiate between a method and a
constructor.
There can be more than one constructor in a class, in such an instance we have
overloaded constructions. When objects of that class are created, the default
values of the objects depend on the kind of the constructor invoked on the
object.
2.0 OBJECTIVES
At the end this unit, you should be able to:
• define a constructor
• explain the concept of object initialisation
• create a constructor
51
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
3.1 Constructors
Constructors are special methods that are used to initialise a new object of a
class. Constructors have the same name as the class; the name of the Student
class's constructor is Student, the name of the Lecturer class's constructor is
Lecturer, etc. If you don‘t define any constructor for your class, the compiler will
supply a default constructor to the class that assigns a default value to the
instance variable of a class‘s object. This was seen in unit 1, where the default
value of a String instance variable was seen to be null and that of integer was
seen to be 0. During object instantiation, the new keyword is used to invoke the
constructor, in order for it to assign default values to the object‘s instance
variable. If you don‘t want the instance variables of an object to take on default
values, you must create your own constructor.
The empty parenthesis after the Classname indicates a call to the class‘s
constructor without argument which is often the default constructor. The default
value a constructor provides depends on the data type of an object‘s instance
variables. Data types are divided into two categories; primitive types and
reference types. The primitive types are Boolean, byte, char, short, int, long,
float and double. All non primitive types are reference types, so classes which
specify the types of objects are reference types. A String datatype is also a
reference type. Primitive type instance variables of char, short, int, long, float
and double are initialised to 0 and variables of type Boolean are initialised to
false by the default constructor. Reference type instance variables are initialised
to null. This is why the FirstName and score instance variables of the
SetGetClass are respectively initialised to null and 0 in the program output.
52
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Example 2.2.1
Create a class that has instance variables of String and Integers which can
respectively store a student‘s first name and age. In the class, create a
constructor that assigns non default values to these variables.
Program Input:
53
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Analysis
Two private instance variables of type String and int with names firstname and
age respectively were used. The non default no argument constructor was
created immediately after the instance variable declaration. The instance
variables are initialised in the constructor‘s body with values Grace and 25. With
this, it is expected that when the constructor is invoked, the default value of
firstname and age will be Grace and 25 respectively. Other methods were also
created to assign new values to and retrieve values from the instance variables
(the set and get methods). This allows the user to enter inputs for firstname and
age.
Example 2.2.2
Modify the StudentClass to make its constructor behave like a default constructor.
54
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Input:
}
public void setValues(String myname,int myage)
{
firstname=myname;
age=myage;
}
public String getFirstname()
{
return firstname;
}
public int getAge()
{ return age;
}
}
// A class that has a non default constructor that behaves like a default constructor.
Program Analysis
Example 2.2.3
Program Input:
55
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
"+stdclass2.getFirstname());
System.out.println("The initial students's age is:
"+stdclass2.getAge());
System.out.println("Enter your name"); name=myinput.nextLine();
System.out.println("Enter your age"); age=myinput.nextInt();
stdclass2.setValues(name,age);
System.out.println("The real name is: "+stdclass2.getFirstname());
System.out.println("The real age is: "+stdclass2.getAge());
}
}
// A program to test StudentClass2.
Program Output:
run:
The initial students's name is: null
The initial students's age is: 0
Enter your name
Grace
Enter your age
25
The real name is: Grace
The real age is: 25
BUILD SUCCESSFUL (total time: 16 seconds)
Program Analysis:
The new thing that will be discovered in this program when compared to
StudentClassTest is in the output. Here, the initial values of the instance
variables firstname and age are respectively null and 0. This is because of the
non default constructor created in the StudentClass2 that behaves like a default
constructor.
Example 2.2.4
Create a class that has a String instance variable and an int instance variable,
in the class, create a constructor that has parameters that uses these instance
variables as its data.
56
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Input:
return age;
}
}
// A class that shows how an argument constructor is created.
Program Analysis
The constructor of this class has a parameter that accepts a String value and an
int value as data, the constructor body sets the instance variable of the class to
the values in the parameter. This works like the set method, so there may not be
any need to include the set method. That is why the set method is not included
in the program. The get methods retrieve the values of the instance variables.
The next example creates an object of Student3Class to see how the class‘s
constructor works.
Example 2.2.5
Create an executable class and instantiate two objects of the StudentClass3 and
display the values of their instance variables.
Program Input:
public class StudentClass3Test
{ public static void main(String[] args)
{
StudentClass3 std1class3=new StudentClass3("Grace",25); StudentClass3
std2class3=new StudentClass3("Rebecca",38); System.out.println("The first
students's name is:
"+std1class3.getFirstname());
System.out.println("The first students's age is:
"+std1class3.getAge());
System.out.println("The second students's name is:
"+std2class3.getFirstname());
System.out.println("The second students's age is:
57
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
"+std2class3.getAge());
}
}
// An executable class that tests the StudentClass3.
Program Output:
run:
The first students's name is: Grace
The first students's age is: 25
The second students's name is: Rebecca
The second students's age is: 38
BUILD SUCCESSFUL (total time: 6 seconds)
Program Analysis
This is the first time we will be creating more than one object of a class. Two
objects of StudentClass2 were instantiated and a constructor call was invoked
on them. The constructor has two arguments that accept a first name and an
age. Because each object has its own instance variable in memory, that is why
we are able to create two objects with different instance variable. The instance
variables for the first object are Grace and 25 while the instance variables for
the second object are Rebecca and 38 as displayed in the output. These
instance variables are set by the constructor during the constructor call.
As you know, you can declare your own constructor to specify how objects of a
class should be initialised. In this section, we will explain how multiple
constructors can be used to initialise objects of a class in different ways. These
constructors are called overloaded constructors. To overload constructors,
simply provide multiple constructor declarations with different signatures. The
compiler differentiates signatures by the number of parameters, the types of
parameters and the order of the parameter types in each signature. This will be
further explained in the next example.
Example 2.2.6
Create a time class that has 3 private instance variables hour, minute and second.
Your class should have overloaded constructors that can be used to manipulate
these instance variables.
Program Input:
58
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
second; public
TimeClass()
{
}
public TimeClass(int thehour)
{
hour=thehour;
}
public TimeClass(int thehour,int theminute)
{
hour=thehour;
minute=theminute;
} public TimeClass(int thehour, int theminute, int thesecond)
{
hour=thehour; minute=theminute;
second=thesecond;
} public int
getHour()
{ return hour;
}
public int getMinute()
{ return minute;
}
public int getSecond()
{ return second;
}
public String displayTime()
{ return
String.format("%02d:%02d:%02d",getHour(),getMinute(),getSecond());
}
}
// A time class that three time instance variables with constructors to manipulate
these variables differently.
Program Analysis
The time class has four constructors; the no argument constructor that does not
accept any time parameter, the one argument constructor that accepts only the
hour time parameter, the two argument constructors that accept the hour and
the minute time parameters and the three argument constructor that accepts the
hour, minute and the second time parameters. All these constructors assign
values to the time instance variables in the constructor‘s body. The getMethods
retrieve the values of the three time instance variables. The displayTime method
returns a String value that shows each of the time parameter in two digits
separated by ―:‖
59
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Destructors are used to tidy up the computer‘s memory. They are used to delete
unused objects from the memory. The local variables created within the body of
a method are automatically deleted once the control is outside the method. This
is not so for dynamically created objects. Objects remain in memory until they
are explicitly deleted. A destructor is a special method typically used to perform
cleanup after an object is no longer needed by the program. C++ supports
destructors, but JAVA does not support destructors. JAVA supports another
mechanism for returning memory to the operating system when it is no longer
needed by an object. This mechanism is called garbage collection.
The garbage collector is a part of the runtime system that runs in a low- priority
thread reclaiming memory that is no longer needed by objects used by the
program. An object becomes eligible for garbage collection in JAVA when there
are no reference variables that reference the object. The garbage collector
makes use of a method finalise to perform its housekeeping. The finalise
method is called by the garbage collector to perform termination housekeeping
on object just before the garbage collector reclaims the object‘s memory.
Method finalise does not take parameters and has return type void. A problem
with method finalise is that the garbage collector is not guaranteed to execute at
a specified period. Infact, the garbage collector may never execute before a
program terminates. Thus, it is not certain that method finalise will be called all
the time. For this reason, the finalise method is rarely used.
4.0 CONCLUSION
7.0 SUMMARY
• Constructors are special methods that are used to initialise a new object
of a class. During object instantiation, the new keyword is
used to invoke the constructor, in order for it to assign default values to
the object‘s instance variable.
60
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
• All non primitive types are reference types, so classes which specify the
types of objects are reference types. A String datatype is also a
reference type. Primitive type instance variables of char, short, int, long,
float and double are initialised to 0 and variables of type Boolean are
initialised to false by the default constructor.
• A constructor is like a method but it differs from a method with the
following two attributes:
Review Exercise
i. What is the purpose of the keyword new? Explain what happens when
this keyword is used in an application.
ii. What is a default constructor? How is an object‘s instance variables
initialised if a class has only a default constructor?
iii. Can a non default constructor and a default constructor be used in a
class?
iv. Differentiate between a default constructor and a no argument
constructor? Differentiate between a non default constructor and a
method?
v. What is constructor overloading?
vi. Explain the concept behind constructor overloading.
vii. What are destructors?
viii. Explain the concept behind garbage collection.
Programming Exercise
61
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
and day separated by forward slashes (/). Write a test application name
DateClasssTest that demonstrates class Date‘s capabilities.
ii. Create a class called FreshStudentClass that includes four private
instance variables- firstname that can accept a student‘s first name,
matricno that can accept a student‘s matric number, deptname that can
accept a students‘ department name and paid that states whether a
student have paid his bills or not. Instance variable paid is a Boolean
type that accepts true or false. Create four constructors in this class that
manipulates this instance variable differently.
The FreshStudentClass should be tested to demonstrate the constructors‘ capabilities.
CONTENTS
1.0 Introduction
2.0 Objectives
3.0 Main Content
3.1 Static Behaviours
3.2 Static Methods
3.2.1 Creating Static Methods
3.2.2 Static Predefined Methods
3.2.3 The Main Method
3.3 Static Fields
3.4 Static Imports
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
The methods, attributes and constructors that have been described so far are all
related to instantiated objects of classes. In each case, these members have
defined how the individual objects behave and how they maintain and
manipulate their own state. Sometimes you will want to create behaviour that is
not linked to any individual object; instead it will be available to all instances and
to objects of other classes. This is where static members become useful.
62
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
In this unit, we will discuss the static behaviour of some class members –
methods and instance variables. You will see that at times, you don‘t need to
create an object of a class before you invoke the class‘s method, the method
can be directly called on the class and not the object. Such methods are called
static methods. You will also see that some predefined classes have static
methods which can be invoked directly on the classes. It is common for some
predefined classes to contain convenient static methods to perform their tasks.
We will also discuss why method main is declared static. We discussed in unit
one that every object of a class has its own copy of all the class‘s instance
variable in memory i.e. there‘s a one-one relationship between the object of a
class and its instance variables. In this unit, you will learn that in certain cases,
only one copy of a particular variable should be shared by all objects of a class.
A static field called a class variable (not an instance variable) is used in such a
case. You will also learn in this unit, how the import statement can be used to
import static members of a predefined class.
2.0 OBJECTIVES
Two members of a class can have the same static properties and they are the
methods of the class and the fields of the class. When the fields of a class have
a static behaviour, it is called a class variable. When these static members of a
class are used, the object of the class need not be instantiated to make
members carry out their work.
63
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
The way a static method is created is not too different from the way normal
methods are created. The only difference is the inclusion of the static keyword.
The syntax for creating a static method is shown below:
The keyword static usually comes before the return type in the method‘s declaration and the
method may or may not accept parameters. Static methods are usually called by specifying
the name of the class in which the method is declared, followed by a dot (.) and the method
name as shown below:
Classname.methodname(arguments);
This saves the programmer the stress of instantiating the object of a class
before the class‘s methods can be called. Our next examples explain how static
methods can be created and called in programmes.
Example 2.3.1
Create a non executable class called FactorialClass that has a one argument
method and two argument methods respectively called computeFactorial and
computePermutation. The computeFactorial method computes the factorial of a
number and the computePermutation method computes the permutation of two
numbers. The two methods should be declared as static, the computeFactorial
method should be used in the computePermutation method, thus it should be
declared as private.
Program Input:
64
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
}
// A program that has two static methods that compute the factorial and
permutation of numbers.
Program Analysis:
The class has a static private method computeFactorial that computes the
factorial of a number. The method is private because it is only been used within
the class to compute the permutation of two number. The computePermutation
method uses the static factorial to compute the permutation of two numbers.
Note the way the computeFactorial method was used; it was prefixed with the
class name and a dot (.).
Our next example uses the static computePermutation method to compute the
permutation of two numbers.
Example 2.3.2
Create an executable class that accepts two numbers from the user; the
computePermutation method should be called to compute the permutation of two
numbers.
Program Input:
import java.util.*;
public class PermutationClassTest
{ public static void main(String[] args)
{
Scanner myinput=new Scanner(System.in); int
num1,num2, result;
System.out.println("Enter the first number"); num1=myinput.nextInt();
System.out.println("Enter the second number"); num2=myinput.nextInt();
result= PermutationClass.computePermutation(num1,num2);
System.out.println(num1+" permutation "+num2+" is "+result);
}
}
// A program that uses the static computePermutation method of class PermutationClass to
compute the permutation of two numbers
65
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Analysis:
An object of Scanner class was called to accept user‘s input, the static
computePermutation method of the PermutationClass was called to compute
the permutation of the two entered numbers. This was done without instantiating
an object of PermutationClass.
There are some readymade static methods that can be used to achieve special
tasks. To use such methods, the import statement will be used to import the
class (if necessary) and the class‘s name will then be called directly on the
object when the method is used. Some classes do not need to be imported
when their static methods are to be accessed. The packages of such classes
are implicitly imported by the compiler. A common example of such packages is
the Java.lang package. The package has a class Math that has some static
methods that enable you to perform common mathematical calculations. Some
of these static methods are discussed below and our next programming
example shows how they work.
The first category of methods accepts only one argument. The second category
has two arguments. Care must be taken in the way the arguments are placed
when the pow method is used. If the arguments are not placed in their correct
66
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
position, a wrong answer may be returned. From the table above, you can see
that pow(2,5) and pow(5,2) returns different values. The first argument is the
base and the second one is the index. This is further explained in the next
example.
Example 2.3.3
Create an executable class that asks the user to input a number, the number
should be passed as argument to all the methods discussed and the value of
the computation should be displayed.
Program Input:
Program Output:
Enter a number
1.5
The absolute value of 1.5 is 1.5
The cosine of 1.5 in radians is 0.0707372016677029
The sine of 1.5 in radians is 0.9974949866040544
The tangent of 1.5 in radians is 14.101419947171719
The square root of 1.5 is 1.224744871391589
The ceiling of 1.5 is 2.0
67
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Analysis:
An object of the Scanner class accepts user input and each of the static
methods of class Math was called to make some computations. No object of the
Math class was created to do this but the class name and a dot was prefixed to
all these methods because they are static methods.
You may be wondering why the main method is always declared as static
whenever it is used, you will know why it is always like that in this section.
Whenever Java programmes are written, the Java compiler translates the Java
source code into bytecodes, which are the codes understandable by the Java
Virtual Machine (JVM). These bytecodes are executed by the Java Virtual
Machine. A virtual machine is a software application that simulates a computer,
but hides the underlying operating system and hardware from programmes that
interact with the virtual machine. When the Java Virtual Machine (JVM) is
executed with the Java command, the JVM attempts to invoke the main method
of the class you specify; when objects of the class have been created. Declaring
method main as static allows the Java Virtual Machine to invoke the main
method without creating an instance of the class.
Every object has its own copy of the entire instance variables of the class. In
certain cases, only one of a particular variable should be shared by all objects of
a class. A static field is used in such an instance and it is usually called a class
variable. A static field or a class variable has general information that is to be
shared by all objects of the class. The syntax for creating a static field is;
accessmodifier static datatype fieldname;
There are two major static fields in the Math Class of Java.lang, these fields are
PI and E. These fields represent commonly used mathematical constants, to
68
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
use them, prefix their names with the class name which is Math and a dot (.).
Thus, we have Math.PI and Math.E. The constant Math.PI is the ratio of a
class‘s circumference to its diameter and its value is
3.14159265358979323846. The constant Math.E is the base value for natural
logarithms and its value is 2.7182818284590452354. These fields are declared
in the class Math with the modifier public, final and static. Making them public
allows other programmers to use these fields in their own classes. They are
declared final because their values never change. Making them static allows
them to be accessed through the class name Math and a dot (.) separator.
The difference between an instance variable and a class variable is that with an
instance variable, the object of a class maintains its own copy of attribute, each
object (instance) of the class has a separate instance of the variable in memory.
Class variables are fields for which each object of a class does not have a
separate instance of the field and such variables are declared as static and they
are thus called static variables. When objects of a class containing static fields
are created, all the objects of that class share one copy of the class‘s static
fields. Class variables and instance variables make up the fields of a class. The
next examples explain how static variables are used.
Example 2.3.4
Create an executable class that allows a user to enter a number that represents
the radius of a circle, use the Math.PI static field of the Math class to compute
the perimeter and the area of the circle.
Program Input:
import java.util.*;
public class CircleTest
{ public static void main(String[] args)
{
Scanner myinput=new Scanner(System.in);
double radius;
System.out.println("Enter the radius of a circle");
radius=myinput.nextDouble();
System.out.println("The perimeter of a circle with radius "+radius+" is
"+(2*Math.PI*radius));
System.out.println("The area of a circle with radius "+radius+" is
"+(Math.PI*radius*radius));
}
}
// A program that uses the static field PI of the Math class to calculate the
perimeter and the area of a circle.
Program Output:
run:
69
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Analysis:
An object of Scanner class was instantiated to accept input that represents the
radius of a circle from a user and the Math.PI was used to calculate the area
and perimeter of the circle.
In the syntax, the packagename and classname represents the package of the
class and the name of the class respectively. The asterisk (*) denotes that all
static members of the specified class should be available for use in the program.
It should be noted that static import declarations import only static class
members. The next example shows how the static import is used and its effect
on a class.
Example 2.3.5
Import all static methods of class Maths explicitly and use some of these
methods to perform some computations without the class name and the dot(.).
Program Input:
import java.util.*;
import static java.lang.Math.*;
class StaticImportClassTest
{ public static void main(String[] args) {
70
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Output:
run:
Enter a number
49
Enter another number
2
Program Analysis:
All the static methods in the Math class were imported explicitly and when those
methods were called, they were no prefixed with the class name and a dot(.) as
if they were created inside the class.
4.0 CONCLUSION
A class may contain static methods to perform common tasks that do not require
an object of the class. A static method is called by specifying the name of the
class in which the method is declared followed by a dot(.) and the method name.
A static variable represents class wide information that is shared among all
objects of the class. Static members exist even when no objects of the class
exist; they are available as soon as the class is loaded into memory at execution
time. To access a private static member when no objects of the class exist, a
71
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
7.0 SUMMARY
• When methods directly called on the class and not the object, such methods are
called static methods. A static field called a class variable (not an instance variable)
is used in such a case.
• The keyword static usually comes before the return type in the method‘s declaration
and the method may or may not accept parameters. Static methods are usually
called by specifying the name of the class in which the method is declared.
• Some classes do not need to be imported when their static methods are to be
accessed. The packages of such classes are implicitly imported by the compiler. A
common example of such packages is the Java.lang package.
• A virtual machine is a software application that simulates a computer, but hides the
underlying operating system and hardware from programmes that interact with the
virtual machine.
• When the Java Virtual Machine (JVM) is executed with the Java command, the JVM
attempts to invoke the main method of the
class you specify; when objects of the class have been created.
• A class‘s static members can be public or private. A class‘s public static members
can be accessed through a reference to any object of a class, or by qualifying the
member name with the class name and a dot (.).
• The difference between an instance variable and a class variable is that with an
instance variable, the object of a class maintains its own copy of attribute, each
object (instance) of the class has a separate instance of the variable in memory.
Review Exercise
a. x=Math.abs(-7.5);
b. x=Math.floor(-2.7);
c. x=Math.ceil(6.3);
d. x=Math.flooe(4.1);
e. x=Math.ceil(-2.5);
f. x=Math.pow(4,6);
72
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
g. x=Math.pow(7,2);
h. x=Math.ceil(-Math.abs(-5+Math.floor(-3.2)));
Programming Exercise
Write a program that works like a clock. Your class should be given the name
Time and it should have three private static fields hour, minute and second that
respectively represents the hour, the minute and the second of a time.
Whenever an object of time is created, it should be assumed that 20 ticks have
occurred, thus the second should increase by 20, when it reaches 60, the
minute should be increased by 20 and when the minute reaches 60, the hour
should increase by one. Test your time class and ensure that it is working
perfectly.
7.0 REFERENCES/FURTHER READING
73
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
1.1 Objectives
3.0 Main Content
3.1 Inheritance
3.2 Superclasses and Subclasses
3.2.1 Protected Members
3.2.2 Data Hiding
3.3 Composition
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
Software development has shown that significant amounts of code deal with
closely related special cases. When the developer is preoccupied with special
cases, the details can obscure the big picture. With object- oriented
programming, the programmer focuses on commonalities among objects in the
system rather than on the special cases. New classes can inherit from classes
in class libraries and thus the development of more powerful, abundant and
economical software will be facilitated. Inheritance is of two types- single and
multiple inheritances. In single inheritance, a class is derived from one direct
superclass whereas in a multiple inheritance, a class is derived from more than
one direct superclass. Java does not support a multiple inheritance.
Another OOP concept which will be discussed in this unit is Composition.
Composition allows a class to have references to objects of other classes as
members. An example of composition in a real life sense is an object of
AlarmClock class, this object needs to know the current time and the time when
it is supposed to sound its alarm, so it is reasonable to include two references to
Time objects as members of the AlarmClock object.
74
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
2.0 OBJECTIVES
3.1 Inheritance
75
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Members can have access modifier protected in their classes in order to make
them accessible by the subclass of that class. In our previous units, it was
discussed that a class‘s public members are accessible wherever the program
has a reference to an object of that class or one of its subclasses. A class‘s
private members are accessible only from within the class itself. A superclass‘s
private members are accessible only from within the class itself. A superclass‘s
private members are not inherited by its subclasses. When the members of a
superclass are declared as protected, the members of its subclasses and
members of other classes in the same package can also access those
members.
All public and protected superclass members retain their original access
modifiers when they become members of the subclass (i.e. the public members
of the superclass become the public members of the subclass and protected
members of the superclass become the protected members of the subclass).
Subclass methods can refer to public and protected members inherited from the
superclass simply by using the members‘ names. When a subclass modifies a
superclass method, the superclass can be accessed from the subclass by
preceding the superclass method with keyword super and a dot (.) separator.
This is called method overriding.
Example 2.4.1
76
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
last names, their grades and comment whether they are bonafide member of the
institution or not. The scoring for each grade is shown below:
Note: Design the class in such a way that it will be used easily by members
offering the elective course.
Program Input:
77
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
break; case
3: case 2:
case 1: grade='F';
break;
} return grade;
}
public void getComment(char grade)
{ if (grade=='A'||grade=='B'||grade=='C')
System.out.println(firstname+" "+lastname+": "+"A bonafide
member"); else
System.out.println(firstname+" "+lastname+": "+"Not a bonafide member");
}
}
// A superclass Member that has five protected members.
Program Analysis:
Example 2.4.2
From the example above, rewrite the Member class in a way that promotes data
hiding.
Program Input:
78
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
{
double avg; int
approx; char
grade=0; avg
=getAverage();
approx=(int)(avg);
switch(approx/10)
{ case 10:
case 9:
case 8:
case 7:
grade='A'; break;
case 6:
grade='B'; break;
case 5: grade='C';
break;
case 4:
grade='D';
break; case 3:
case 2:
case 1: grade='F';
break;
}
return grade;
}
public void getComment(char grade)
79
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
{
if (grade=='A'||grade=='B'||grade=='C') System.out.println(firstname+" "+lastname+": "+"A
bonafide member"); else
System.out.println(firstname+" "+lastname+": "+"Not a bonafide
member");
}
}
// A modified version of the Member class that promotes data hiding.
Program Analysis:
The only new thing introduced is that all the fields that were declared as
protected before are now declared as private. This is to enhance data hiding.
Example 2.4.3
Using the Member2 class as subclass, create a subclass that provides the
functions that can handle the necessary computations for students taking the
elective course.
Program Input:
Program Analysis:
The class has a new name that inherits from the modified Member class. In the
getAverage method, a call was made to the public method of the superclass to
return the average of scores in maths, English and science, a quarter of the
score in vocational studies is then added. This is because this class no longer
has a direct access to these three fields just because they have been declared
as private.
3.3 Composition
People often confuse composition for inheritance and vice versa. This is
because these two concepts allow class association. However, there is a
80
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Example 2.4.4
Create a class Date that has all the three date parameters- day, month and
year. This class should have a method that displays date in a meaningful
format. Program Input:
public class Date { private
int day; private int
month; private int year;
public Date(int theday, int themonth, int theyear)
{
day=theday; month=themonth;
year=theyear;
}
public String toString()
{ return
String.format("%02d%s%02d%s%d",day,"/",month,"/",year);
}
}
// A date class that displays date.
Program Analysis:
The three date parameters – day, month and year are declared private to
promote data hiding. A new method toString was called to display the date in a
meaningful format. This method is inherited from the implicit direct superclass of
Date called Object. Like it was said earlier all classes have a direct or indirect
superclass called Object. Method toString is one of the most prominent methods
of the Object class. The next example shows how the object of a class can
reference the variables of a class. You will create a Student class, since every
81
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
student is expected to have a date of birth; you will reference the Date class in
the class.
Example 2.4.5
Create a class Student that has a field birthdate which is of the date type. The
class should have methods that can display the first name, last name and the
birth date of students.
Program Input:
Program Analysis:
The field birthdate is of the Date class, thus, it is a date datatype. The
constructor also takes as part of its argument a date data type dateOfbirth since
every student is expected to have a date of birth which is a date data type.
When the toString method is called on a date object, the date is displayed in the
format specified by the Date class in its toString method.
Example 2.4.6
Test the Student class created in the previous example with an executable class.
Program Input:
82
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
// A program to test the Student class that has a data type variable.
Program Output:
run:
Student's name: Grace Omotoso
Birth Date: 13/01/1990
BUILD SUCCESSFUL (total time: 1 second)
Program Analysis:
An object of Date class was first created and later passed to the constructor
during object instantiation. The getComment method of the Student class was
then called to display the first name, last name and birth date of students.
4.0 CONCLUSION
The direct superclass of a subclass (specified by the keyword extends in the first
line of a class declaration) is the superclass from which the subclass inherits. An
indirect superclass of a subclass is two or more levels up in the class hierarchy
from that subclass. Thus, the object class is either a direct superclass for a
class (a class that does not explicitly inherit from another class) or an indirect
superclass for a class (a class that explicitly inherits from another class). A
subclass is more specific than its superclass and represents a smaller group of
objects. Every object of a subclass is also an object of that class‘s superclass.
However, a superclass object is not an object of its class‘s subclasses.
5.0 SUMMARY
83
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Review Exercise
Programming Exercise
84
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
2.0 Objectives
4.0 Main Content
4.1 Polymorphic Behaviours and Examples
4.2 Abstract Classes and Concrete Classes
4.3 Abstract Methods
4.4 Downcasting and Dynamic Binding
4.5 Final Methods and Classes
3.6 Interfaces
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
In this unit, we will discuss a very useful OOP concept called polymorphism that
allows software reuse. It enables programmers to program in the general rather
than in the specific. Polymorphism enables programmers to write programmes
that process objects that share the same superclass in a class hierarchy as if
they were objects of the superclass. This significantly helps to simplify
programming. With polymorphism, programmers can design and implement
systems that are easily extensible, new classes can be added with little
modification to the general portions of the program.
People often find difficulty in differentiating between polymorphism and
inheritance, at the end of this unit; you will understand the difference between
these two OOP concepts. You will also learn some OOP terminologies and
concepts like interfaces, abstract classes and abstract methods.
2.0 OBJECTIVES
85
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Polymorphism is the act of designing a class in a way that the objects of the
class behave in different ways. With polymorphism, programmers can rely on
various objects of a class to do the right thing (i.e. do what is appropriate for that
type of object) in response to the same method call. Just like in our example,
the same message (in this case, move) sent to various objects have ―many
forms‖ of results; this is where the term polymorphism is derived.
With polymorphism, the same method and signature can be used to cause
different actions to occur, depending on the type of object on which the method
is invoked.
Not all classes can be used for object instantiations, most times, such classes
acts as a superclass and their main work is to make other classes inherit their
methods and to share a common design. Such classes are called abstract
classes. While abstract classes cannot be used for object instantiation, concrete
classes are classes that can be used for object instantiation. The classes
discussed so far in all our earlier examples are concrete classes. Concrete
86
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
An abstract method is a method that does not provide implementations for its
action. Such a method is declared like a normal method in addition to the
keyword abstract. A class that contains any abstract method must be declared
as an abstract class even if the class contains some concrete (non abstract)
methods. When a concrete subclass inherits from an abstract superclass, the
subclass must provide concrete implementations of each of the superclass‘s
abstract methods, failure to do this, results in compilation errors.
Case Study:
A company pays its employees on a weekly basis. The employees are hourly-
employees, salaried-employees and commission-employees. Hourly-employees
are paid by the hour and receive overtime pay for all hours worked in excess of
forty hours. The overtime pay per hour is one – half of their normal pay per hour.
Salaried-employees are paid a fixed weekly salary regardless of the number of
hours worked. The commission-employees are only paid a percentage of their
87
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
sales. The company wants you to implement a Java application that performs its
payroll calculation polymorphically.
Example 2.5.1
Create an abstract superclass called Employee that each of the employee class can
inherit to perform its payroll calculations.
Program Input:
Program Analysis:
The class is declared as abstract in the first line, this tells that it has at least one
method that has no implementation. The class contains three private instance
variables firstname, lastname and employee_id, since an employee is expected
to have a first name, a last name and an identity number. The constructor body
sets all these three instance variables. The toString method is inherited from the
implicit direct superclass Object and it returns a string value that displays the
first name, last name and employee id of all employees. Method earnings is
declared abstract since it does not have implementation code, this is because
different categories of employee have earn different amounts. When subclasses
of employee extend the class, they must all provide the implementation code for
the method earning because this is part of the contract they will sign with the
abstract superclass before inheriting it.
Example 2.5.2
Create a class for the categories of employees that are paid hourly.
88
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Input:
public class HourlyEmployee extends
Employee{ double hours; double rate;
public HourlyEmployee(String first, String last, String id, double
thehours, double therate)
{ super(first,last,id);
hours=thehours;
rate=therate;
}
public double earnings()
{
if(hours<=40) return
hours*rate;
else return (40*rate)+((hours-40)*1.5*rate);
}
public String toString()
{
return String.format("\nHourly employee: %s \n%s%.2f\n%s%.2f",
super.toString(),"Hours worked: ",hours,
"Wages per hour: ",rate);
}
}
// A class for the hourly paid employees.
Program Analysis:
The class has two private variables hours and rate, since an hourly- employee is
paid based on the hours worked and the rate of wages per hour. The
constructor has five arguments, three of the arguments are set by calling the
superclass constructor and the remaining two are set newly. Method earning
returns the amount an hourly paid employee will earn, overtime is also
considered. The overtime is one-half the wages per hour for each overtime
hour. The method toString has a call to the superclass, this displays the first
name, last name and employee id, and other parameters are also included to
make the displayed result complete.
Example 2.5.3:
Program Input:
89
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
{ super(first,last,id);
weeklysalary=weeklysal;
} public double
earnings()
{ return weeklysalary;
}
public String toString()
{ return String.format
("\nSalaried employee: %s\n%s %.2f",super.toString(),"Weekly
Salary:",weeklysalary);
}
}
// A class for the salaried employee
Program Analysis:
The class has a private instance variable salary, since a salaried- employee is
paid a fixed salary irrespective of the hours worked. The constructor has four
arguments, three of the arguments are set by calling the superclass constructor
and the remaining one is set newly. Method earning returns the amount
salaried-employee will earn which is basically the value of the instance variable
salary. The method toString has a call to the superclass, this displays the first
name, last name and employee id, and other parameters are also included to
make the displayed result complete.
Example 2.5.4
Program Input:
90
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Analysis:
The class has two private variables grossales and commissionrate, since a
commission-employee is paid based on the gross sales and the commission
rate. The constructor has five arguments, three of the arguments are set by
calling the superclass constructor and the remaining two are set newly. Method
earning returns the amount a commission-employee will earn. The method
toString has a call to the superclass, this displays the first name, last name and
employee id, and other parameters are also included to make the displayed
result complete.
3.4 Downcasting and Dynamic Binding
A superclass reference can be used to invoke only the methods declared in the
superclass, attempting to invoke a subclass only methods through a superclass
give a compilation error. If a program needs to perform a subclass-specific
operation in a subclass object referenced by a superclass variable, the program
must first cast the superclass reference to a subclass reference through a
technique known as downcasting. Downcasting enables a program to invoke
subclass methods that are not in the superclass. The syntax for downcasting is:
An object of a subclass is an object of its superclass just like all cars are
vehicles. An object of a superclass may not be an object of a subclass just like
all vehicles are not cars. Downcasting allows an object of a superclass to
behave like an object of a class, thus the methods of a subclass can be called
through a superclass reference. Dynamic binding is the process through which
the type (class) of an object is determined at execution time rather than at
compilation time. It is also called late binding. Next examples explain the
concept of downcasting and dynamic binding.
Example 2.5.5
For the current pay period, the company has decided to reward the salaried-
commission employees by adding 5% to their base salaries. The company
wants you to implement this without altering the classes you have already
because it‘s going to be a temporal process. Clue: Use an executable class
Program Input:
91
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
new
BasePlusCommissionEmployee("Rebecca","Olufunke","07/001",5000,0.
04,300);
System.out.println("Employees processed Polymorphically");
Employee employees[]=new Employee[4];
employees[0]=hourlyemployee; employees[1]=salaryemployee;
employees[2]=commissionemployee;
employees[3]=basepluscommissionemployee;
for(Employee currentemp: employees)
{
System.out.println(currentemp);
if (currentemp instanceof BasePlusCommissionEmployee)
{
BasePlusCommissionEmployee
emp=(BasePlusCommissionEmployee)currentemp;
emp.setSalary(1.05*emp.getSalary());
System.out.printf("new base salary with 5%% increase is: #%,.2f\
n",
emp.getSalary());
}
System.out.printf("earned #%,.2f\n",currentemp.earnings());
}
// An executable class that processes the objects of Employee subclasses
polymorphically.
Program Output:
run:
Employees processed Polymorphically
Hourly employee: Grace Omotoso
employee_id: 08/021 Hours worked: 16.75
Wages per hour: 40.00
earned #670.00
92
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Analysis:
It was discussed in unit three that a variable declared final cannot be modified
after they are initialised- Such variables represent constant values. It is also
possible to declare methods, method parameters and classes with the final
modifier. A method that is declared final in a superclass cannot be overridden in
a subclass. Methods that are declared private are implicitly final because they
cannot be used outside the class. A final method‘s declaration can never
change, so all subclasses inheriting the final methods of a superclass use the
same implementation and calls to final methods are resolved at compile time.
This concept is known as static binding.
3.6 Interfaces
93
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
All objects of a class that implement multiple have the is-a relationship with each
implemented interface type. Next examples explain how an interface can be
used to make objects of unrelated classes to be processed polymorphically.
Case Study:
Suppose that the company involved in our first case study wishes to perform
several accounting operations in a single amountPayable application- in addition
to calculating the earning paid to each employee, the company must also
calculate the payment due on each of several invoices.
Example 2.5.6
Program Input:
94
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Analysis:
Example 2.5.7
Create a class Invoice that implements the Payment interface. This class should
show the product number and description, it should also show the product
quantity and the price of product. Method getPaymentAmount should be
implemented in this class to calculate the amount on the invoice.
Program Input
}
}
// A class that implements the Payment Interface.
Program Analysis:
The first line of the program has the implement keyword. This shows that it is
making use of an interface called Payment. The getPaymentAmount of this
class is implemented because the class is not declared as abstract, it is
implicitly signing an agreement that it will provide implementations for all the
methods of the interface.
95
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Example 2.5.8
Create a class that uses the interface Payment that can be used as a
superclass for any of the categories of employees discussed earlier.
Program Input:
Program Analysis:
The class is abstract because the method getPaymentAmount has not been
used and hence no implementation is provided for it.
Example 2.5.9
Create a subclass for this new Employee2 superclass for the salaried-
employee category. This method should implement the getPaymentAmount of
the Payment interface.
Program Input:
96
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
{ return String.format
("\nSalaried employee: %s\n%s %.2f",super.toString(),"Weekly
Salary:",weeklysalary);
}
}
// A subclass for the Employee2 class that uses interface Payment.
Program Analysis:
The class is a concrete class. Even though the implement keyword was not
used in the first line, since it is a subclass of the Employee2 class that
implements the Payment interface, the class indirectly implements the interface.
It must provide implementations for the getPaymentAmount method used in the
interface since it is not declared as abstract.
Example 2.5.10
Test all your classes with an executable class by polymorphically manipulating objects of
Invoice and Employee2.
Program Input:
Program Output:
97
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
run:
Employees and invoices processed polymorphically
Invoice
Product id: 0101 (PS2)
Quantity: 5
Price per item: #500.00
Payment due: 2500.00
Invoice
Product id: 0201 (Optical)
Quantity: 7
Price per item: #700.00
Payment due: 4900.00
Program Analysis:
No new thing was introduced in the program. Careful examination of each line of
code makes you understand better.
4.0 CONCLUSION
With polymorphism, programmers can design and implement systems that are
easily extensible. New classes can be added with little or no modification to the
general portions of the program, as long as the new classes are part of the
inheritance hierarchy that the program processes generically. In some cases, it
is useful to declare classes for which you never intend to instantiate objects.
Such classes are called abstract classes. Because they are used only as
superclasses in inheritance hierarchies, they are referred to as abstract
superclasses. The primary purpose of an abstract class is to provide an
appropriate superclass from which other classes can inherit and thus share a
common design. Classes that can be used to instantiate objects are called
concrete classes and such classes declare implementations of every method
they declare.
98
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
The is-a relationship applies only between a subclass and its superclass, not
vice-versa. However, downcasting allows a superclass object behave like its
subclass object. A method that is declared final in a superclass cannot be
overridden in a subclass.
Interfaces define and standardise the ways in which things such as people and
system can interact with one another. To use an interface, a concrete class
must specify that it implements the interface and must declare each interface
method with the signatures specified in the interface declaration. A class that
does not implement all the interface‘s methods is an abstract class and must be
declared abstract.
5.0 SUMMARY
• Abstract superclasses are too general to create real objects; they specify
only what is common among subclasses and since we need to be more
specific before we can create objects, abstract classes are not used for
object instantiation.
• While abstract classes cannot be used for object instantiation, concrete
classes are classes that can be used for object instantiation.
• An abstract method is a method that does not provide implementations
for its action. Such a method is declared like a normal method in addition
to the keyword abstract.
• Although, static methods are inherited, they are not associated with
particular objects of the classes that declare the static methods. Since
abstract methods are meant to be overridden so that they can process
objects based on their types, it would not be proper to declare a static
method as abstract.
• A superclass reference can be used to invoke only the methods declared
in the superclass- attempting to invoke a subclass only methods through
a superclass gives a compilation error.
• A method that is declared final in a superclass cannot be overridden in a
subclass. Methods that are declared private are implicitly final because
they cannot be used outside the class.
99
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Review Exercise
Programming Exercise
Shape
TwoDimensionalShap ThreeDimensionalSha
100
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
MODULE 3 OVERLOADING
CONTENTS
1.0 Introduction
2.0 Objectives
3.0 Main Content
3.1 Methods
3.2 Creating and Using Methods
3.2.1 Argument Promotion and Casting
3.2.2 Random Number Generation
3.3 Method Overloading
3.4 Recursive Methods
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
Most computer programmes that solve real-world problems are much larger than
the programmes presented in module two. The best way to develop and
maintain a large program is to construct it from small, simple pieces called
modules. Although, methods was introduces in our previous module, it will be
studied at greater length in this unit. In this unit, you will see how method
declaration can be used to facilitate the design, implementation, operation and
maintenance of large programmes. You will also learn how Java is able to keep
track of which method is currently executing, how local variables of methods are
maintained in memory and how a method knows where to return after it
completes execution.
Many of the classes you will use or create while developing applications will
have more than one method of the same name. This technique is called method
overloading and it is used to implement methods that perform similar tasks for
arguments of different types or for different numbers of arguments. For some
problems, it is useful to have a method call itself; such a method is called a
recursive method. You will learn the concepts of recursion in this unit.
101
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
2.0 OBJECTIVES
3.1 Methods
Methods are one of the modules that exist in Java. The three kinds of modules
that exist in Java are methods, classes and packages. Methods (called
functions or procedures in other languages) allow programmers to modularise a
program by separating its tasks into self-contained units. The statements in the
method bodies are written only once, are reused from perhaps several locations
in a program and are hidden from other methods. The following are the
motivations for modularising a program into methods:
102
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Methods often require pieces of information to perform their tasks. The pieces of
information are called parameters. Most times, methods use more than one
parameter. When methods are created, the data type, number of parameters
and the order of parameters are to be put into consideration. These three factors
are called method signature. When methods are to be called (after creation), the
pieces of data passed into them are called arguments. The arguments used by
a called method must have the same signature as the parameters used during
the method creation. Our next examples show how methods with multiple
parameters are declared in a program and the mechanism for passing
information between methods. The syntax for creating a method is;
The accesslevel describes how other methods in the class will be able to access
this method. The access level can either be public or private. A private
accesslevel makes the method inaccessible by others methods of the class. A
public access level makes the method accessible by other methods of the class.
Returntype describes the data type the method will return to its caller. Return
type can be any valid data type. A method that has a return type of int returns an
integer to its caller, while a method that has a return type of String value returns
a String value to its caller. Sometimes a method may not return any value to its
caller. Such methods have a return type of void.
The methodname is any valid variablename. The method may or may not have
parameters depending on the method action. If a method has parameters, when
such method is to be called, the signature of the arguments must be equal to the
specified signature in the called method. Just like classes, methods have a
method body that begins and ends respectively with a left brace ―{―and a right
brace ―}‖. A method may or may not return a value. A method that has a return
type of void does not return any value to its caller, thus, the second to the last
line is absent i.e.
return value.
Note: For methods that do not have a return type of void, the last line before the
closing brace in the method should be the return value. All programming
examples in this Course book are compiled using the Java Netbeans.
Example 3.1.1
103
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Create an executable class that accepts two numbers from users, the class
should have a method that finds the difference of the two numbers; the method
should be called to display the difference of the two numbers.
Program Input:
Program Output:
run:
Enter the first number
5
Enter the second number
3
The difference between 5.0 and 3.0 is 2.0
Program Analysis:
The program uses an executable class and the java.util package was used to
import the scanner class. An object of Scanner myinput was created to accept
inputs from users. The input expected from users are of double types but in our
output we entered integer values and no error was returned, this is because the
compiler was able to implicitly convert the integer values to double values, you
can see in the output that 5 and 3 entered by the user have been respectively
104
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
changed to 5.0 and 3.0 by the compiler. This process is called argument
promotion and it will be discussed in the next section. The method getDifference
was declared as static because it was used in the static main method. The
method has two parameters number1 and number2 both are of the double data
type. When the method was called in the main method, two arguments num1
and num2 were passed into the method. It should be noted that the number of
parameters and their datatypes in the method is equivalent to the number of
arguments and datatypes when the method was called. Supplying any data
other than the one specified in the method parameters results to compilation
error.
Example 3.1.2
Program Input:
105
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Output:
run:
Enter the first number
9
Enter the second number
6
Enter the third number
4
The difference between 9.0 and the difference of 6.0 and 4.0 is 7.0 BUILD
SUCCESSFUL (total time: 22 seconds)
Program Analysis:
The program is not far different from the one in example one, only that the
getDifference methods were embedded into each other in the output segment of
the main method. The outermost method is first executed, this accepts 6 and 4
and returns 2 to the outermost method, the outermost method uses the returned
value as its second arguments. The argument for the method is now 9 and 2
which then returns 7 as its result.
• Using a method name by itself to call another methods of the same class
e.g. in our programming example 3.1.1, getDifference(num1,num2).
• Using a variable that contains a reference to an object followed by a dot
(.) and the method name to call a method of the referenced object. This
was discussed in unit one of module two.
• Using the class name and a dot (.) to call a static method of a class. This
was discussed in unit three of module two.
106
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
receive a double argument, it does not still return an error. The method
declaration parameter list makes Java to convert the int values 5 and 3 to 5.0
and 3.0 before passing the values to getDifference. Some conversions may lead
to compilation errors if Java‘s promotion rules are not satisfied. The promotion
rules specify which conversions are allowed, these are the conversions that can
be performed without losing data. An int value can be converted to a double
value without changing its value just like 9 and 9.0 have the same value.
Converting a double value to an int value truncates the fractional part of the
double value- thus part of the value will be lost.
Example 3.1.3
Develop a non executable class that can be used for converting numbers from
one base to another. Modularise your program as much as possible with
methods so that the problem will be easier to solve.
Program Input:
else
num=giveDecValue(val);
result+=num*(int)Math.pow(base, (number.length()-1-i));
}
return result;
}
public boolean isNumber(char value)
{
if(value=='0'||value=='1'||value=='2'||value=='3'||value=='4'||value=
='5'
107
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
108
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
hold+=giveHexaValues(rem);
number=number/base;
while(number>0)
{
rem=number%base; if(rem<10)
hold+=rem;
else
hold+=giveHexaValues(rem);
number=number/base;
}
for(int i=hold.length()-1;i>=0;i--)
{
result+=hold.charAt(i);
}
return result;
}
}
// A class for number conversion from one base to another.
Program Analysis:
The program was written based on the concept introduced in our discussion of
methods, careful analysis of each line of codes makes you understand better.
Example 3.1.4
Test your number conversion class by accepting three inputs from users, the
first input is the number to convert, the second input is the base of the number
and the third input is the base you want to convert the number to.
Program Input:
import java.util.Scanner;
public class NumberConversionTest {
public static void main(String[] args) {
Scanner myinput=new Scanner(System.in);
String number;
int initialbase, finalbase;
System.out.println("Enter the number you want to convert"); number=
myinput.next();
System.out.println("Enter the initial base of the number");
initialbase=myinput.nextInt();
System.out.println("Enter the base you want to convert the number to");
finalbase=myinput.nextInt();
NumberConversion nc=new NumberConversion();
System.out.println(number+" in base "+initialbase+" to
109
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Program Input:
run:
Enter the number you want to convert
110011
Enter the initial base of the number
2
Enter the base you want to convert the number to
16
110011 in base 2 to 16 is 33
BUILD SUCCESSFUL (total time: 15 seconds)
run:
Enter the number you want to convert
A4B5
Enter the initial base of the number
16
Enter the base you want to convert the number to
10
A4B5 in base 16 to 10 is 42165
BUILD SUCCESSFUL (total time: 40 seconds)
The random number generator can be used to generate random Boolean, byte,
float, double, int, long and Gaussian values but only the int values will be
discussed in this section. There is a method that can be called on the random
generator to make it generate integer values. This method is the nextInt method.
It may or may not accept arguments. If no argument is supplied, then the
method generates a random int value from - 2,147,483,648 to -2,147,483,647.
The values returned by nextInt are pseudorandom numbers – a sequence of
110
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
The range of values produced directly by method nextInt often differs from the
range of values required in a particular Java application e.g. a program that
simulates the toss of a coin might only require 0 and 1, and a method that
simulates the rolling of a die might require numbers 1 to 6. For such cases,
class Random provides another version of method nextInt that accepts integer
argument, and returns a value from 0 up to the number before the number
specified in the argument. For instance random.nextInt(4) returns generates
number one of numbers 0 to 3.
The argument 4 in the example is called the scaling factor- which represents the
number of unique values that nextInt should produce (In this case four- 0, 1, 2,
3). To use the random generator to generate numbers that can appear on a die,
we can have the following declaration;
Method overloading is the process through which methods of the name are
declared in the same class. In this process, there seems to arise some naming
conflicts, but the signature of the methods resolves this. A method signature is
the sets of parameters of the method that basically comprises the number of
parameters, the data types of parameters and the order of the parameters.
When an overloaded method is called, the java compiler is able to select the
exact method that is called by checking the number, data types and the order of
arguments supplied to the called method.
Method overloading is commonly used to create methods with the same name
that perform the same or similar tasks, but on different types or different
numbers of arguments. An important aspect to take note of in method
overloading is that method calls cannot be distinguished by their return types.
The method factors that differentiate between overloaded methods are their
signatures only. Overloaded method declarations with identical signatures cause
compilation errors even if the return types are different. The next example
explains the concept of method overloading.
Example 3.1.5
Write a program that uses a method minimum to find the minimum of three
numbers. The method should be overloaded as three methods. The first method
111
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
accepts three integer values as its arguments and returns an integer, the
second method accepts three double values as its parameters and returns
double, and the third method accepts an integer value and two double values as
its argument and returns double. Users should be asked to enter three integer
values and three double values. The overloaded methods should accept these
variables as applicable.
Program Input:
112
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
"+doublenum1+","+doublenum2+","+intnum3+" is "+
minimum(doublenum1,doublenum2,intnum3));
}
}
// A program that has overloaded methods.
Program Output:
run:
The minimum of 3,7,6 is 3
The minimum of 5.0,-2.3,-7.0 is -7.0
The minimum of 5.0,-2.3,6 is -2.3
BUILD SUCCESSFUL (total time: 2 seconds)
Program Analysis:
There are three static methods in the program; these methods are declared
static because they are used inside the static main method. The methods have
the same and different signatures, when the methods are called; the compiler is
able to call the right method by comparing the signature of the arguments of the
called method to that of the parameters of the method itself.
Example 3.1.6
Using recursion, write a method that finds the factorial of numbers. Your method
should be used in an application that finds the factorial of the first 10 numbers.
Program Input:
113
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
{
if (number<=1) return 1;
else return number*factorial(number-1);
} public static void main(String[] args) {
for (int i=1;i<=10;i++)
{
System.out.println(i+"!\t"+factorial(i)); }
}
}
// A program that uses recursion to compute factorial of numbers.
Program Output:
run:
1! 1
2! 2
3! 6
4! 24
5! 120
6! 720
7! 5040
8! 40320
9! 362880
10! 3628800
Program Analysis:
The factorial method first solves the base case by specifying that factorial of one
or any number less than one should be zero, the complex case is then solved
by dividing the problem into two pieces which later divides itself until the base
case is reached. The next example we will look at uses recursion to generate
Fibonacci series.
4.0 CONCLUSION
Experience has shown that the best way to develop and maintain a large
program is to construct it from several small, simple pieces or modules. This
technique is called divide and conquer. Methods allow programmers to
modularise a program by separating its tasks into self- contained units. The
statements in a method are written only once and hidden from other methods.
Using existing methods as building blocks to create new programmes is a form
of software reusability that prevents programmers from repeating codes within a
program. When a method is called, the program makes a copy of the method‘s
argument values and assigns them to the method‘s corresponding parameters,
114
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
which are created and initialised when the method is called. When program
control returns to the point in the program where the method was called, the
method parameters are removed from memory. A method can return at most
one value, but the returned value can be a reference to an object that contains
many values.
Where shiftvalue specifies the first number in the desired range of consecutive
integers and scaling factor specifies how many numbers are in the range. A
recursive method calls itself directly or indirectly through another method. When
a recursive method is called to solve a problem, the method is capable of
solving only the simplest case(s) or base case(s). If it is called with a base case,
the method returns a result. If a recursive method is called with a more complex
problem than a base case, it divides the problem into two conceptual pieces- a
piece that the method knows how to do and a piece that it does not know how to
do. It then further divides the latter piece into two different pieces and continually
does that until the base case is finally used to solve the large problem.
5.0 SUMMARY
• Methods are one of the modules that exist in Java and the three kinds of
modules that exist in Java are methods, classes and packages.
• Methods (called functions or procedures in other languages) allow
programmers to modularise a program by separating its tasks into self-
contained units.
• A method is invoked (i.e., made to perform its designated task) by a
method call. The method call specifies the name of the method and may
provide information (as arguments) that the called method requires to
perform its task.
• Argument promotion is the process of converting an argument‘s value to
the type that the method expects to receive in its corresponding
parameter.
• Method overloading is commonly used to create methods with the same
name that perform the same or similar tasks, but on different types or
different numbers of arguments.
• A recursive method is a method that calls itself which can be called
either directly or indirectly through another method.
115
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Review Exercise
Programming Exercise
1. Write a method IntegerPower( base, exponent ) that returns the value of base
exponent, for example, IntegerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that the
exponent is a positive integer, and base is an integer. Method IntegerPower
should use for or while to control the calculation. Do not use any Math library
methods. Use the method in an application that reads integer values for base
and exponent and performs the calculation with the method IntegerPower.
2. Implement the following methods:
a. Method Celsius returns the Celsius equivalence of a Fahrenheit temperature using the
formalar; Celsius = 5.0/9.0 * (Fahrenheit –
32);
b. Method Fahrenheit returns the Fahrenheit equivalence of a
Celsius temperature using the formular;
Fahrenheit=9.0/5.0*(Celsius + 32);
c. Use the methods from parts (a) and (b) to write an application that enables the user to
either enter a Fahrenheit temperature and displays the equivalent Celsius temperature
or to enter a Celsius temperature and display the Fahrenheit equivalence.
116
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
2.0 Objectives
5.0 Main Content
5.1 Basic Operator Overloading
5.2 Binary Operator Overloading
5.2.1 Creating the Addition (+) Operator
5.2.2 Creating the Subtraction (-) Operator
5.2.3 Creating the Multiplication (*) Operator
5.3 Unary Operator Overloading
5.3.1 Creating the Increment and Decrement Operator
5.3.2 Creating the Negation Operator
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
2.0 OBJECTIVES
117
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Java does not support operator overloading but an exception was made for
String by using the ―+‖ symbol to concatenate string literals. Although, this is
out of the control of developers as it is a built- in feature of Java. Developers
have no control over operator overloading in Java. Java does not support
operator overloading because the designers of Java wanted to keep Java codes
simple and found that operator overloading made code more complex and
difficult to read. There are other methods available to achieve the same
functionality as operator overloading, you could create methods in a class
named plus (), minus (), multiply (), etc... The designers of Java must have
decided that operator overloading in Java was more of a problem than it was
worth.
Some Object- Oriented languages allow you to overload an operator. C#/ C++
are examples of such languages. Operator overloading allows you to change
the meaning of an operator. For example, when most people see a plus sign,
they assume it represents addition. If you see the equation, you expect that X
would contain the value 11. And in this case, you would be correct.
X = 5 + 6;
However, there are times when a plus sign could represent something else. For
example, in the following code:
You would expect that Name would contain Joe Smith. The plus sign here has
been overloaded to perform string concatenation which is commonly used in
Java but it is a built- in feature of Java that cannot be controlled by
programmers.
Many languages allow the programmer to redefine, either partially, or wholly, the
definition of basic operators (+, -, etc.). This can be extremely useful as it allows
the programmer to use operators on new data types in a way that makes sense.
For scientist, the classic example is complex numbers. Most languages don‘t
support complex numbers as intrinsic data types but with operator overloading
they can be used and the resulting code is clear to read since Java doesn‘t
allow operator overloading, the programmer must use methods which makes
the code more verbose and harder to read and maintain.
The following codes create a vector class that can be used to manipulate values
that have two coordinates, e.g. 2x+6y, through operator overloading, we can
perform the basic mathematical operations on members of the vector class.
118
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
public int X
{ get { return _x; } set
{ _x = value; }
}
public int Y
{ get { return _y; } set
{ _y = value; }
}
}
The first type of operator to consider is the binary operator, so named because
they require two values to work with. These include the simple arithmetic
operators such as +, -, *, / and %. In C#, the syntax for overloading binary
operators is:
The syntax for binary operators can now be used to create a new addition
operator for the Vector class. This operator will simply add the X and Y elements
of two vectors together and return a new vector containing the result. Add the
following to the Vector class to provide this functionality. Note that a new vector
is created rather than adjusting one of the operands. This is because the
operands are reference-types and the original values should not be updated in
this case.
119
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
We can now test the vector's new operator by modifying the program's main
method. The following program instantiates two vector objects, adds them
together and outputs the values of the resultant vector's X and Y properties.
Vector v3 = v1 + v2;
Console.WriteLine("({0},{1})", v3.X, v3.Y); // Outputs "(4,19)"
}
Addition is a commutative operation. This means the order of the two operands
can be swapped without affecting the outcome. In the case of subtraction, this is
not the case so it important to remember that the first operand in the declaration
represents the value to the left of the operator and the second operand
represents the value to the right. If these are used incorrectly, the resultant
value will be incorrect. Using this knowledge we can add a subtraction operator
to the Vector class:
Vector v3 = v1 - v2;
The last binary operator that will be added to the Vector class is multiplication.
This operator will be used to scale the vector by multiplying the X and Y
120
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
properties by the same integer value. This demonstrates the use of operands of
a different type to the class they are defined within.
Vector v2 = v1 * 3;
In the operator code for the multiplication operator, the Vector is the first operand
and the integer the second. This means that the order used in the multiplication
statement must have the vector at the left of the operator and the integer value to the
right. Changing the order of the operands in the Main method will cause a compiler
error.
Vector v2 = 3 * v1;
If the class must support both variations of multiplication, both must be declared
in the code. This provides the benefit of allowing the order of operands change
the underlying function. To provide the second variation of multiplication, add
the following code to the Vector class. Afterwards, the program will execute
correctly.
Unary operators are those that require a single operand. These include the
simple increment (++) and decrement (--) operators. To declare a unary
operator, the following syntax is used:
121
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
This syntax is almost identical to that used for binary operators. The difference is
that only one operand is declared. The operand type must be the same as the
class in which the operator is declared.
Using the syntax defined above, we can now add the increment and decrement
operators to the Vector class. Note that there is only a single definition for each.
There is no way to differentiate between prefix and postfix versions of the
operator so both provide the same underlying functionality. To declare the two
operators, add the following code to the Vector class. Each increments or
decrements both affect X and Y properties for Vector objects.
public static Vector operator ++(Vector v)
{
v.X++;
v. Y++; return
v;
}
v1++;
Console.WriteLine("({0},{1})", v1.X, v1.Y); // Outputs "(5,12)"
v1--;
Console.WriteLine("({0},{1})", v1.X, v1.Y); // Outputs "(4,11)"
}
The last arithmetic unary operator to be considered in this unit is the negation
operator. This is the unary version of subtraction used to identify a negative
version of a value. We can add this operator using the following code:
122
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
}
To test the negation operator, update the Main method and run the program.
static void Main(string[] args)
{
Vector v1 = new Vector(4, 11);
Vector v2 = -v1;
Console.WriteLine("({0},{1})", v2.X, v2.Y); // Outputs
"(-4,-11)" }
4.0 CONCLUSION
In this unit, you learnt that it is possible to change the way basic operators like +,
- , *, / etc behave in order to suit the programmers work. Java does not support
this but methods can be used to make Java behave like that. Java was
designed not to support operator overloading because it makes Java codes
complex but since Java was designed to make programming easier, the feature
was embedded into Java. Some Object- oriented languages like C#/ C++
support the basic operator overloading it.
8.0 SUMMARY
123
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
www.blackwasp.co.uk/CSharpObjectOriented.aspx
124
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
2.0 Objectives
3.0 Main Content
3.1 The Logical Operators
3.2 Overloading the Binary Boolean Logical Operators
3.2.1 Creating the Boolean AND Operator (&)
3.2.2 Creating the Boolean OR (|) and XOR (^) Operators
3.3 Overloading the Unary Boolean Logical Operator
3.3.1 Creating the Boolean NOT Operator (!)
3.3.2 Enabling the Short-Circuit Operators
3.3.3 Adding the Short-Circuit Operator Pre-Requisites to the
Vector Class
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 Reference/Further Reading
1.0 INTRODUCTION
In this unit, the overloading of the logical operators is described including how to
enable short-circuit Boolean logical operators. All programmes are written in C
sharp because it enables overloading of operators.
2.0 OBJECTIVES
There are four logical operators that can be directly overloaded for a class.
These are the NOT operator (!), the AND operator (&), the OR operator (|) and
the exclusive OR or XOR operator (^). The short-circuit operators (&& and ||)
cannot be overloaded directly. However, if the class meets certain conditions
the short-circuit operators are enabled automatically.
125
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
In this unit, a Vector class will be used. The Vector class represents a two-
dimensional vector and already overloads arithmetic operators and the true and
false operators. The Vector class will be updated to include overloading of the
four logical operators for Boolean operators.
[DefaultMemberAttribute("Item")] [SerialisableAttribute()]
public class Vector : ICloneable, ISerialisable, IDisposable
At least one of the operands must be of the same type as the containing class.
This means that by providing the correct signature, you can permit logical
operations between two classes of differing types.
The three binary Boolean operators can be added to the Vector class using the
syntax described above. When determining the results of each, a vector will be
deemed to equate to true if either of the co-ordinates is non-zero. If both the X
and Y properties are zero, the vector will be considered 'false'. Add the following
operator overload to the Vector class to implement the AND function:
Analysis:
126
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Using similar code, we can add the Boolean OR and XOR operators to the
Vector class. Add the following two operator overloads to the class to implement
the two logical functions.
127
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Only one unary Boolean logical operator exists. This is the NOT operator (!) that
switches a Boolean value between true and false. When overloaded using the
unary syntax below, the value returned should be the opposite of the Boolean
representation of the object. As with other unary operators, the type of the
operand provided must be the same as the class that the declaration appears
within.
The NOT operator for the Vector class will examine the contents of the X and Y
properties. As described above, if both co-ordinates are zero, the Boolean value
for the object is false. However, as this is the NOT operator, we will perform the
check and return true only when both values are zero. Add the following code to
the class to provide the NOT operator:
To test that the operator is working correctly, modify the Main method as follows:
128
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
However, if two conditions are met in the class then the short-circuit operators
are automatically made available. The two conditions are as follows:
• The class must overload the normal logical operators (& and |) with the
operation returning a value of the same type as the containing class.
Each parameter of the operator must also be of the type of the
containing class.
• The true and false operators must be overloaded.
• When the operator is invoked, the true or false operators are used to
determine the status of the first operand. If this guarantees an outcome
from the operation, the result is returned immediately. When the state of
the first operand does not force an outcome, the AND or OR operator is
used for the two values to determine the result.
The Vector class already includes overloaded true and false operators. To
enable the short-circuit operators, one need to add the correct signature for the
AND and OR operators. As these must return a Vector result that can be
evaluated as either true or false according to the results, this will return (0,0) if
the operation equates to false and (1,1) for true. Modify the code for the existing
& and | operator overloads as follows:
129
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
You can test that the short-circuit operators are correct by modifying the
Main method of the program. However, as the operators do not return a
Boolean value, an if statement is required in order to test the results of the
operations. This is demonstrated in the following code.
else {
Console.WriteLine("v1 && v2 = false"); // Outputs "v1 && v2 = false"
} if (v1 || v2) {
Console.WriteLine("v1 || v2 = true"); // Outputs "v1 || v2 = true"
} else
{
Console.WriteLine("v1 || v2 = false"); }
}
4.0 CONCLUSION
Operator overloading refers to the ability to define a new meaning for an existing
(built-in) ―operator‖. The list of ―operators‖ includes mathematical operators
(+, -, *, /, ++, etc), relational operators ( <, >, ==, etc), logical operators (&&, ||,
etc.), access operators ([], ->), assignment operator (=), stream I/O operators
( <<, >>), type conversion operators and several others. While all of these
operators have a predefined and unchangeable meaning for the built-in types,
all of these operators can be given a specific interpretation for different classes
or combination of classes.
Vector class is used to represent a real vector. Its methods can be used to
perform vector operations and data manipulation.
8.0 SUMMARY
• There are four logical operators that can be directly overloaded for a class.
These are the NOT operator (!), the AND operator (&), the OR operator (|) and
the exclusive OR or XOR operator
(^).
130
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
• When evaluating an AND where the first operand evaluates to false, the result of
the operation will be false regardless of the value of the second operand.
• The Vector class already includes overloaded true and false operators. To
enable the short-circuit operators, one need to add the correct signature for the
AND and OR operators.
• Operator overloading refers to the ability to define a new
meaning for an existing (built-in) operator.
131
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
1.2 Objectives
3.0 Main Content
3.1 Adding True and False Operators
3.2 Overloading True and False
3.2.1 Adding True and False to the Vector Class
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
2.0 OBJECTIVES
The default behaviour of any class is to provide no support for the true and false
operators. This means that if an attempt is made to evaluate an object of such a
class as a Boolean the code will fail to compile. This can be demonstrated by
using a Vector object as the condition in an if statement. Change the Main
method of the VectorDemo program as follows and attempt to compile it to see
the error.
132
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
The syntax for overloading the true and false operators is similar to that of other
unary operators. Two limitations exist. Firstly, the return value must be a
Boolean. Secondly, it is invalid to overload only one of the two operators; if the
true operator is overloaded than so must be false and vice versa.
The Vector class can now be updated to include overloaded versions of the true
and false operators. In the case of vectors, the object will be deemed to be 'true'
when either of the vector's X or Y properties is non- zero. If the X and Y values
are both zero, the object will evaluate as false. This is simply implemented by
adding the following code:
133
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Analysis:
Now that the two operators have been added, you should be able to compile
and execute the Main method described above. As the vector's X and Y co-
ordinates are not zero, the code outputs the text "True". If you modify the
Vector's declaration as follows, the code will output
―False” instead.
The following are the examples of true and false operator overloading:
public bool b;
134
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
if ( myTrue )
{
System.Console.WriteLine( "true" );
} else
{
System.Console.WriteLine( "false" );
}
if ( myFalse )
{
System.Console.WriteLine( "true" );
} else
{
System.Console.WriteLine( "false" );
}
if ( myNull )
{
System.Console.WriteLine( "true" );
} else
{ Sys
tem.
Cons
ole.
Write
Line(
"false
" );
}
}
}
Output:
True
False
False
135
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
using System;
public struct Complex
{
public Complex( double real, double imaginary ) { this.real = real;
this.imaginary = imaginary;
}
Output:
136
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
using System;
public TwoDimension() {
x = y = 0;
} public TwoDimension(int i, int j) {
x = i; y
= j;
}
return op;
}
class MainClass {
public static void Main() {
TwoDimension a = new TwoDimension(5, 6);
137
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
if(a)
Console.WriteLine("a is true."); else
Console.WriteLine("a is false.");
if(b)
Console.WriteLine("b is true."); else
Console.WriteLine("b is false.");
if(c)
Console.WriteLine("c is true."); else
Console.WriteLine("c is false.");
Console.WriteLine();
}
}
Output:
Here is a: 5, 6
Here is b: 10, 10
Here is c: 0, 0
a is true.
b is true.
c is false.
138
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
3, 3
2, 2
1, 1
4.0 CONCLUSION
The true and false operators can be overloaded, to allow a class to represent its
own state as true or false. You can overload the true or false operators if you
are defining a specialised boolean value. Operator overloading allows us to
define/redefine the way operators work with our classes and structures. This
allows programmers to make their custom types look and feel like simple types
such as int and string. It consists of nothing more than a method declared by the
keyword operator and followed by an operator. There are three types of
overload able operators called unary, binary, and conversion. Not all operators
of each type can be overloaded.
5.0 SUMMARY
What the default behaviour of any class does is to provide no support for
the true and false operator, Which means if an attempt is made to
evaluate an object of such a class as a Boolean, the code will fail to
compile
Two limitations that exist in overloading are: first, the return value must
be a Boolean; secondly, it is invalid to overload only one of the two
operators; if the true operator is overloaded than so must be false and
vice versa.
The true and false operators can be overloaded, to allow a class to
represent its own state as true or false.
Erich Gamma, Richard Helm, Ralph Johnson & John M. Vlissides (2006).
139
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
CONTENTS
1.0 Introduction
2.0 Objectives
5.0 Main Content
5.1 Conversion of Data Types
5.2 Creating an Implicit Conversion Operator
5.2.1 Adding Double Conversion to the Vector Class
5.3 Creating an Explicit Conversion Operator
5.3.1 Adding Single Conversion to the Vector Class
5.4 Indexers
5.4.1 Creating an Indexer
5.4.2 Creating a New Array-Like Class
5.4.2.1Adding the Class Variables
5.4.2.2Adding the Constructor
5.4.2.3Adding the Indexer
5.4.3 Creating a Multidimensional Indexer
3.4.4 Creating an Indexer with no Underlying Array
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
2.0 OBJECTIVES
140
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
The following code sample shows a comparison of conversion using implicit and
explicit casts for basic numeric data types. Note the requirement for the cast
operator for the second conversion.
When you create a new class, it does not possess the capabilities to be cast to
other data types. Each possible cast must be defined within the class and
declared as either implicit or explicit. As can be seen in the above example
code, the implicit cast can be easier to read and can provide neater code.
However, it is not immediately apparent to the reader that a conversion is
occurring and so implicit casting can hide problems that would be more
apparent if an explicit cast operator were used.
As a rule of thumb, implicit casting should only be used where there is no risk of
data loss or an exception being thrown. Explicit casting should be used in all
other situations. This is demonstrated in the above code. Implicit casting from
the smaller integer to the larger integer is risk-free as all possible values can be
converted. For conversion from the large signed integer to the smaller unsigned
integer, there is a risk that the value will lose its sign or be too large to be
converted, so explicit casting is more appropriate. There are some restrictions to
overloading the conversion operators. The key restrictions are:
• You may not create operators that convert a class to the object data type.
Conversion to object is provided automatically to permit boxing and
unboxing.
• You may not create operators that convert a class to a defined interface. If
conversion to an interface is required, the class must implement the
interface.
• You may not create operators that convert from a base class into a class
derived from that base class.
This unit uses the Vector class. The Vector class represents a two- dimensional
vector and already overloads arithmetic operators, true and false operators,
logical operators and relational operators.
141
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
The implicit and explicit cast operators are unary operators and, as such, are
overridden using a similar syntax as other basic unary operators. The following
syntax is for the implicit conversion operator:
The result-type is the data type for the return value of the operation, i.e. the
target type for the cast. The op-type is the data type for the operand that is to be
converted. One of the two data types must be the same as the class in which
the declaration is made.
Using the syntax described above, we will now add an implicit conversion
operator to the Vector class. This operator will cast a vector to a double-
precision floating point number representing the length of the vector. The length
calculation is already present as a property of the class so we will reuse this
functionality. To add the new conversion operator, add the following code to the
Vector class:
The syntax for creating an explicit conversion operator is similar to that of the
implicit version. Only the implicit keyword is changed. The syntax is therefore:
public static explicit operator result-type(op-type operand)
We will use the explicit version of the conversion operator syntax to allow the
vector to be cast as a single-precision floating point value representing the
vector's length. The choice of explicit operator is due to the loss of accuracy of
142
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
the number when the length is converted from a double to a float. To add the
new conversion operator, add the following code to the Vector class:
The new conversion can be tested using the updated Main method below. Note
the loss of accuracy that occurs when the vector's length is cast.
float f = (float)v;
Console.WriteLine(f);
}
// Outputs 7.071068
Note: The use of the (float) operator is required in this example as the
conversion has been defined as explicit. Removing this cast operator causes a
compiler error.
3.4 Indexers
A class that has an indexer can be used in a similar manner to an array. Objects
of the class can use array-style notation to present multiple values. Most C#
programmers first use an indexer when working with an array. An array is used
to store a number of similar, related variables under the same name, with each
variable accessed using an index number provided in square brackets. For
example:
thirdItem = items[3];
Indexer is use often to incorporate the square bracket notation for new classes.
This may be because the class is used to store related information in a similar
manner to an array, or simply because the index number can be useful in a
calculation or lookup. Adding an indexer to a class provides this functionality.
143
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
In the syntax definition, data-type determines the type of information that will be
returned when the indexer is queried and the type that will be required when
setting a value. Index-type specifies the data type of the indexer itself. This
permits declaration of indexers that are not based upon integer values, allowing
similar functionality to that of a Hash table for example. The index-name is the
variable containing the index value that can be used during processing of the
get and set accessors. The get accessor is required for an indexer and must
return a value of the type data-type. The set accessor is defined for writeable
indexers and is omitted if a read-only variant is desired.
To demonstrate the use of an indexer, in this unit, we will create a new class
that behaves like a simple array of string variables. Unlike a standard array that
only permits zero-based indexing, the new class will provide an integer-based
array for which the programmer can specify the upper and lower boundaries
using a constructor during instantiation.
3.4.2.1 Adding the Class Variables
The new array-like class requires three private variables. Two integer values will
hold the upper and lower boundaries. An array of strings will also be required to
store the items added to the MyArray class. This will be a zero-based array with
the same length as the created MyArray object. The indexer will interpret the
index number supplied and map it against this underlying array forget and set
operations. To add the class level variables, add the following code to the
MyArray class‘ code block:
int _lowerBound;
int _upperBound;
string[] _items;
The constructor for the new class will accept two integer parameters that define
the upper and lower boundaries. These values will be stored in the two
associated class variables. Using these boundaries, the length of the underlying
array can be calculated and the array can be initialised accordingly. To create
the constructor, add the following code to the class:
144
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
_upperBound = upperBound;
_items = new string[1 + upperBound - lowerBound];
}
Note: To simplify this example validation, checks have been omitted. In a real
program, you would want to validate that the upper boundary is larger than the
lower boundary. Other validation checks in the code above have also been
removed for clarity.
Now that the preparation work is complete, we can add the indexer to the class.
For this simple array-like class, the indexer accepts a single integer parameter
containing the index of the string that is being read from or written to. This index
needs to be adjusted to correctly map to the underlying data before returning the
value from the array or writing the new value into the array. The code to add the
indexer is shown below. Note that as with property declarations, the set
accessor uses the 'value' variable to determine the value that has been
assigned by the calling function:
Analysis:
The new class can be tested using the Main method of the program. Open the
Program class and add the following code to create a new instance of MyArray
and to populate it with values.
145
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
Indexers are not limited to a single dimension. By including more than one index
variable in the square brackets of the indexer declaration, multiple dimensions
may be added. For example, to declare a two- dimensional indexer the syntax is
as follows:
146
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
4.0 CONCLUSION
Conversion operators are those that involve converting from one data type to
another through assignment. There are implicit and explicit conversions. Implicit
conversions are those that involve direct assignment of one type to another.
Explicit conversions are conversions that require one type to be casted as
another type in order to perform the conversion. Conversions that may cause
exceptions or result in loss of data as the type is converted should be handled
as explicit conversions.
5.0 SUMMARY
147
CSC202 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING [By G.C. Omede]
• For conversion from the large signed integer to the smaller unsigned
integer, there is a risk that the value will lose its sign or be too large to
be converted, so explicit casting is more appropriate.
• The implicit and explicit cast operators are unary operators and, as such,
are overridden using a similar syntax as other basic unary operators.
Erich Gamma, Richard Helm, Ralph Johnson & John M. Vlissides (2006).
Design Patterns: Elements of Reusable Object-Oriented Software.
148