13 interfaces

12
Working with Arrays of Objects LIS4930 © PIC Everything comes out of an ArrayList<Object> as a reference of type Object, regardless of what the actual object is, or what the reference type was when you added the object to the list. ArrayList<Object> objec t objec t objec t objec t

Upload: program-in-interdisciplinary-computing

Post on 22-Apr-2015

738 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 13 interfaces

LIS4930 © PIC

Working with Arrays of Objects

Everything comes out of an ArrayList<Object> as a reference of

type Object, regardless of what the actual object is, or what the reference type was when you added the object to the list.

ArrayList<Object>

object

object

object

object

Page 2: 13 interfaces

LIS4930 © PIC

What’s the Issue?

What type of object reference is the Practice array?

What classes are these methods in?

The compiler decides whether you can call a method based on the reference type, not the actual object type.

Page 3: 13 interfaces

LIS4930 © PIC

FamilyDoctor object

Get in touch with your inner Object.

Doctor

treatPatient()

Object

equals()getClass()hashCode()toString()

FamilyDoctor

giveAdvice()There is ONE object on the heap here. A FamilyDoctor object. But it contains both the FamilyDoctor class parts of itself, the Doctor class parts, and the Object class parts of itself.

Page 4: 13 interfaces

LIS4930 © PIC

‘Polymorphism’ means ‘many forms’FamilyDoctor D = new

FamilyDoctor();

FamilyDoctor object

Object F = D;

D

F

The object reference can see ONLY the Object parts of the FamilyDoctor object. It can access only the methods of class Object.

Page 5: 13 interfaces

LIS4930 © PIC

Casting an Object reference back to its real type.

FamilyDoctor object

F

FamilyDoctor N = (FamilyDoctor) F;

N

It’s still really a FamilyDoctor object, but if you want to call FamilyDoctor specific methods, you need a reference declared as type FamilyDoctor

Page 6: 13 interfaces

LIS4930 © PIC

InstanceofIf you’re not sure it’s a FamilyDoctor, you can use the

instanceof operator to check. Because if you’re wrong when you do the cast, you’ll get a ClassCastException at runtime and come to a grinding halt.

Page 7: 13 interfaces

LIS4930 © PIC

Look at page219 – 223

in the textbook

Turn in your Textbooks.

Page 8: 13 interfaces

LIS4930 © PIC

Interface To The RescueA Java interface solves your multiple inheritance problem by giving you much of the polymorphic benefits of multiple inheritance without the pain and suffering from the Deadly Diamond of Death (DDD).To make a Java interface you MUST make all the methods abstract!

Pet

abstract void beFriendly()abstract void play()abstract void eat()

To DEFINE an interface:

public interface Pet { … }

To IMPLEMENT an interface:

public class Dog extends Canine implements Pet { … }

Use the keyword “implements” followed by the interface name. Note that when you implement an interface you still get to extend a class

Page 9: 13 interfaces

LIS4930 © PIC

Building Interfaces

public interface Pet {public abstract void

beFriendly( );public abstract void play( );

}

public class Dog extends Canine implements Pet {

public void beFriendly( ) { … }public void play( ) { … }

public void roam( ) { … }public void eat( ) { … }

}

You say ‘interface’ instead of ‘class’ here

All interface methods are abstract, so they MUST end in semicolons.

You say ‘implements’ followed by the name of the interface.

You MUST give the methods a body here since they are abstract in the superclass.

Page 10: 13 interfaces

LIS4930 © PIC

Classes from different inheritance trees can implement the same interface.

Look at page226

in the textbook

When you use a class as a polymorphic type the objects you can stick in that type must be from the same inheritance tree.

But when you use an interface as a polymorphic type (like an array of Pets), the objects can be from anywhere in the inheritance tree. The only requirement is that the object are from a class that implements the interface.A class can implement multiple interfaces:public class Dog extends Animal implements Pet, Saveable, Paitable { … }

Page 11: 13 interfaces

LIS4930 © PIC

How do you know whether to make a class, a subclass, an abstract class, or

an interface?

Make a class that doesn’t extend anything (other than Object) when your new class doesn’t pass the IS-A test for any other type.

Make a subclass (extend a class) only when need to make a more specific version of a class and need to override or add new behaviors.Use an abstract class when you want to define a template for a group of subclasses, and you have at least some implementation code that all subclasses could use. Make the class abstract when you want to guarantee that nobody can make objects of that type.

Use an interface when you want to define a role that other classes can play, regardless of where those classes are in the inheritance tree.

Page 12: 13 interfaces

LIS4930 © PIC

Invoking Superclass Methods

The keyword super lets you invoke a superclass version of an overridden method, from within the subclass

Look at page228

in the textbook