15
Ch 23 Java in context Learning objectives By the end of this lecture you should be able to: explain the difference between a reference and a pointer; explain the term multiple inheritance; explain the problem of aliasing in Java; develop clone methods to avoid the problem of aliasing; identify immutable objects; explain the benefits of Java’s garbage collector.

Ch 23 Java in context Learning objectives By the end of this lecture you should be able to: explain the difference between a reference and a pointer;

Embed Size (px)

Citation preview

Page 1: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

Ch 23 Java in context

Learning objectives

By the end of this lecture you should be able to:

explain the difference between a reference and a pointer;

explain the term multiple inheritance; explain the problem of aliasing in Java; develop clone methods to avoid the problem of

aliasing; identify immutable objects; explain the benefits of Java’s garbage collector.

Page 2: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

Language size

Java is relatively small and compact when compared to a traditional systems language like C (and its object-oriented successor C++).

Java syntax is very similar to C++ syntax but it does not contain complex C++ features such as pointers and multiple inheritance.

Page 3: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

Pointers vs references

A pointer is a variable containing an address in memory.

Java programmers can do something very similar to this – they can create references.

The difference between a reference and a pointer is that the programmer does not have control over which address in memory is used – the system takes care of this.

In a language like C++ the programmer can directly manipulate this pointer which could lead to critical areas of memory being corrupted.

Page 4: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

Problems with multiple inheritance

In Java a class can only ever inherit from, at most, one base class.

Other languages (like C++ and Eiffel) allow multiple inheritance where a class inherits from more than one base class:

Manager Player

Player Manager

a combination of single and multiple inheritance

EmployeeThe Java developers decided not to allow multiple inheritance for two reasons:

it is very rarely required; it can lead to very complicated

inheritance trees which in turn lead to programming errors.

Although Java disallows multiple inheritance it does offer interfaces - a class can implement many interfaces.

Page 5: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

Aliases in Java

Aliasing occurs when the same memory location is accessed by variables with different names.

Copying an object reference creates an alias:

oblong1attributes of object stored here

oblong2

Oblong oblong1 = new Oblong(10, 20);

Oblong oblong2 = oblong1

Computer Memory Java Instructions

Page 6: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

The dangers of aliasing

In practice a programmer would normally create an alias only with good reason:

list[i] = list[i+1];

However, a potential problem with aliasing is that it could lead to errors arising inadvertently.

Aliases could, unintentionally, break the principle of encapsulation by allowing write access to a private attribute.

Page 7: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

An example of the dangers of aliases

class Customer{ // private attributes hold bank account details private BankAccount account1;

private BankAccount account2;

// more code here

// two access methods public BankAccount getFirstAccount()

{return account1;

}

public BankAccount getSecondAccount(){

return account2;}

}

Page 8: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

An example of the dangers of aliases

The getFirstAccount and getSecondAccount methods send back a reference to a private attribute which allows users of this class to interrogate details about the two bank accounts, with statements such as:

BankAccount tempAccount = someCustomer.getFirstAccount();System.out.println(“balance of first account = ” + tempAccount.getBalance());

The problem is that this reference can also be used to manipulate the private attributes of the BankAccount object, with a statement like this:

tempAccount.withdraw(100);

Page 9: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

A solution to the problem of aliasing

The problem of aliases arises when a copy of an object is required but instead a reference to an object is returned.

By sending back a reference, the original object can be manipulated, whereas a copy would not cause any harm to the original object.

In order to provide such a copy, a class should define a method that returns an exact copy of the object rather than a reference.

Such a method exists in the Object class but has to be overridden in any user defined class.

The method is called clone.

Page 10: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

The clone method

The clone method that needs to be redefined in each user-defined class has the following outline:

public Object clone(){

// code goes here}

Here is a clone method for the BankAccount class:

public Object clone(){ BankAccount copyOfThisAccount = new BankAccount(accountNumber, accountName); copyOfThisAccount.balance = balance; return copyOfThisAccount;}

Page 11: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

The clone method creates a copy of an object

ourAccountattributes of object stored here

tempAccountattributes of object stored here

BankAccount ourAccount = new BankAccount (“98765432”, “Charatan and Kans”);

BankAccount tempAccount = (BankAccount) ourAccount.clone();

Computer Memory Java Instructions

Page 12: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

Amending the Customer class

class Customer{ // as before here

// next two methods send back clones, not aliases public BankAccount getFirstAccount() {

return (BankAccount)account1.clone(); } public BankAccount getSecondAccount() {

return (BankAccount)account2.clone(); }}

Page 13: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

Immutable objectsObjects which have no methods to alter their state are known as immutable objects.

Aliases of an immutable objects cause no harm as there are no methods that allow the object to be altered.

Here Book objects are immutable so there is no problem with the getBook method of BookTable sending back an alias.

BookTable

books: Hashtbale

BookTable()addBook(Book)removeBook(String)getBook(String): BookgetBooks(): Enumeration

Book

isbn: Stringauthor: Stringtitle: String

Book(String, String, String)getISBN():StringgetAuthor():StringgetTitle():String

*

Page 14: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

Garbage collection

Computer memory could become exhausted when memory that is no longer needed is not released back to the system.

Java has a built in garbage collection facility to release unused memory.

This is a facility that regularly trawls through memory looking for locations used by the program, freeing any locations that are no longer in use.

Page 15: Ch 23 Java in context Learning objectives By the end of this lecture you should be able to:  explain the difference between a reference and a pointer;

class Tester{ public static void main(String[] args) { char ans; Oblong object; // reference to object created here do { System.out.print("Enter length: "); double length = EasyIn.getDouble(); System.out.print("Enter height: "); double height = EasyIn.getDouble(); // new object created each time we go around the loop object = new Oblong(length, height); System.out.println("area = "+ object.calculateArea()); System.out.print("Do you want another go? "); ans = EasyIn.getChar(); } while (ans == 'y' || ans == 'Y'); }

Without a garbage collector the following program could exhaust memory.