CS2514
Introduction to Java
Dr Derek Bridge
School of Computer Science & Information Technology
University College Cork
Client code for LibraryBook
class
LibraryBook b1 = new LibraryBook("Sir Hugh Jeegoh", "Java through dance");
LibraryBook b2 = new LibraryBook("Ms Helen Back", "Java: who gives a #*$%?");
b2.acquire();
b2.acquire();
b2.takeOut();
b2.takeOut();
b2.bringBack();
b1.acquire();
System.out.println(b1);
System.out.println(b2);
LibraryBook
class definition
class LibraryBook {
private String author;
private String title;
private int numCopiesPossessed;
private int numCopiesOnLoan;
public LibraryBook(String author, String title) {
this.author = author;
this.title = title;
}
public int getNumCopiesPossessed() {
return numCopiesPossessed;
}
public int getNumCopiesOnLoan() {
return numCopiesOnLoan;
}
public int getNumCopiesOnShelves() {
return numCopiesPossessed - numCopiesOnLoan;
}
public void acquire() {
numCopiesPossessed = numCopiesPossessed + 1;
}
public void takeOut() {
numCopiesOnLoan = numCopiesOnLoan + 1;
}
public void bringBack() {
numCopiesOnLoan = numCopiesOnLoan - 1;
}
}
Improving the LibraryBook
interface
- Suppose we want another constructor, one that allows us to specify how many copies
we initially possess
- Example:
LibraryBook b3 = new LibraryBook("Sir Hugh Jeegoh", "Java for vegetarians", 4);
- Its signature:
public LibraryBook(String author, String title, int numCopies)
- Suppose we want another method for when we acquire more than one copy of a book
at a time
Most naturally, it would have the same name as the existing method
Exercise: What do we add to the class definition?
public LibraryBook(String author, String title, int numCopies) {
}
public void acquire(int numNewCopies) {
}
Explicitly calling a constructor using this
- One constructor can call another in the same class definition
- can reduce duplicated code
- A different use for the keyword
this
- If you want to do it, it must be the first line in the constructor
- Example:
public LibraryBook(String author, String title) {
}
public LibraryBook(String author, String title, int numCopies) {
}
When to use overloading
- Overloading of constructors:
- very common, e.g.
Scanner
, String
,
Random
,...
- a good way of having 'optional' parameters
- Overloading of methods:
- less common, e.g.
indexOf
and substring
in String
- use only when the methods have very nearly identical behaviour
- saves inventing different method names
- signals to human readers of the code that the methods
do similar things
- if you use overloading for unrelated methods, your program is less
understandable
Advanced point: overloading of operators
- Java has overloaded operators too: one operator may have several definitions
/
applied to two int
s is integer division;
/
applied to at least one double
is floating-point division
+
applied to two int
s is integer addition;
+
applied to at least one double
is floating-point
addition;
+
applied to at least one String
is concatenation
- However, Java doesn't allow us to provide operators with further definitions
of our own
Advanced point: Overloading in Python
- Overloading of constructors and methods is not possible
- But it does offer default values to make parameters optional
- And it has ways of supporting functions that take a variable number of arguments
- It does have overloaded operators
- E.g. think of the different uses of
+
- And you can even overload operators yourself:
- E.g. defining an
__eq__
method in effect gives
==
an additional definition
- E.g. defining an
__add__
method in effect gives
+
an additional definition