Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces and Polymorphism CS 162 (Summer 2009).

Similar presentations


Presentation on theme: "Interfaces and Polymorphism CS 162 (Summer 2009)."— Presentation transcript:

1 Interfaces and Polymorphism CS 162 (Summer 2009)

2 Interfaces A formal contract that specifies the similarities between two or more classes Specifies the methods that a class implementing that interface must implement

3 Interfaces All methods are abstract (no implementation) All methods are automatically public No instance fields (except public static final constants)

4 Example public interface OpenableByKids { void Open(); void Find(); void DestroyContents(); }

5 Interfaces Interface cannot be instantiated: cannot call Openable dresser = new Openable(); Instead, interfaces must be implemented: – public class Dresser implements OpenableByKids { public void Open() {…} public void Find() {…} public void DestroyContents() {…} }

6 Interfaces Interfaces can be used as a data type: – void OutOfMySight(OpenableByKids furniture); – Can pass any class to this method that implements the OpenableByKids interface

7 Interfaces Classes can implement more than one interface public class Dresser implements OpenableByKids, OpenableByCats { Need to implement all methods from both interfaces

8 Interfaces Can convert from class to interface: – Dresser myDresser = new Dresser(); OpenableByKids openableDresser = myDresser; Can go back using casting operator: – if (openableDresser instanceof Dresser) { Dresser someDresser = (Dresser) openableDresser; }

9 Extending Interfaces Interfaces can be extended with new interfaces: – public interface ExpensiveMistake extends OpenableByMyKids, OpenableByCats { // includes all methods from OpenableByMyKids and OpenableByCats // adds new methods, too } – Multiple inheritance is possible with interfaces, but not with inheritance.

10 Polymorphism Polymorphic – “having many forms” Accomplished in Java with both interfaces and inheritance allows Java programs to be written more abstractly, and more abstraction allows for more efficiency and less redundancy.

11 Polymorphism Suppose we have a Player object called p. p.throwWeapon() might mean: – Throw Rock if p is a RockPlayer – Throw Scissors if p is a ScissorsPLayer – Throw something random if p is a Player (the parent class) Depending on what p points to, a different method might be called These references are resolved at run-time

12 Polymorphism Allows elegant re-use of higher-level code when lower-level code is different: public class Tournament { private ArrayList players; public play() { for( int i = 0; i < players.size(); i++ ) { Player p = players.get(i); p.throwWeapon(); } } }

13 Implementation public class AnimalGame { public static void main(String[] args) { System.out.println("Choose one of three doors."); int doorNumber = input.nextInt(); Animal animal = null; if (doorNumber == 1) { animal = new Cat(); } else if (doorNumber == 2) { animal = new Dog(); } else if (doorNumber == 3) { animal = new TRex(); } else { System.out.println("Not a valid door number"); System.exit(1); } System.out.println("The animal behind your door says: " + animal.talk()); }

14 Interfaces and Polymorphism using an interface can stretch the ability of polymorphism over many families of classes. Example: abstract class Electronic – one abstract method: operate(). – Imagine that you want the operate() method to be available to more than just the Electronic family of classes. You can create an interface that contains the method operate(), and tell the Electronic abstract class to implement that interface.

15 Operator interface Operator { void operate(); } public abstract class Electronic implements Operator {…}

16 Operator Example Electronic and its subclasses still uses the operate() method in the same way Other families of classes may use the operate() method as well

17 Surgeon public class Surgeon implements Operator { String selectedOrgan; public Surgeon(String organ) { selectedOrgan = organ; } public void operate() { System.out.println("Removed " + selectedOrgan); } }


Download ppt "Interfaces and Polymorphism CS 162 (Summer 2009)."

Similar presentations


Ads by Google