Unformatted text preview:

InterfacesSlide 2SyntaxCastingSlide 5Implementing Multiple InterfacesInterfacesInterfaces•Generally, interface describes public members of a class•Java interface allows programmer to specify method signatures for reusability•An interface cannot be instantiated•A class that implements and interface must implement the methods defined in the interface•Example – sortableSyntaxpublic interface interface_name {//method signaturepublic return_type name(params);}public class class_name implements interface_name {//method implementationpublic return_type name(params) {}}Castingpublic interface Animal {public void speak();}public class Bird implements Animal { public void speak() { System.out.println(“Squawk”);}public void fly() {...}} public class Cow implements Animal { public void speak() { System.out.println(“Moo”);}public void milk() {...}}CastingAnimal[] animals = new Animal[3];animals[0] = new Cow();animals[1] = new Bird(); animals[2] = new Cow();//milk all cowsfor(int i = 0; i < animals.length; i++) {if(animals[i] instanceof Cow) {Cow c = (Cow)animals[i];c.milk();}}Implementing Multiple Interfacespublic class class_name implements interface1, interface2, interface3 {//implementation of ALL methods from interfaces 1, 2, and 3}Example: interface Student with method listClassesinterface ComputerScientist with method writeProgramclass CSStudent implements Student and ComptuerScientistmust implement methods listClasses and


View Full Document

USF CS 112 - Interfaces

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Interfaces
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Interfaces and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Interfaces 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?