Interfaces - PowerPoint PPT Presentation

About This Presentation
Title:

Interfaces

Description:

Interfaces Interfaces Generally, interface describes public members of a class Java interface allows programmer to specify method signatures for reusability An ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 7
Provided by: usfcaEdu
Learn more at: https://www.cs.usfca.edu
Category:
Tags: interfaces | milk

less

Transcript and Presenter's Notes

Title: Interfaces


1
Interfaces
2
Interfaces
  • 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 sortable

3
Syntax
  • public interface interface_name
  • //method signature
  • public return_type name(params)
  • public class class_name implements interface_name
  • //method implementation
  • public return_type name(params)

4
Casting
  • public 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(M
oo) public void milk()
...
5
Casting
  • Animal animals new Animal3
  • animals0 new Cow()
  • animals1 new Bird()
  • animals2 new Cow()
  • //milk all cows
  • for(int i 0 i lt animals.length i)
  • if(animalsi instanceof Cow)
  • Cow c (Cow)animalsi
  • c.milk()

6
Implementing Multiple Interfaces
  • public class class_name implements interface1,
    interface2, interface3
  • //implementation of ALL methods from interfaces
    1, 2, and 3
  • Example interface Student with method
    listClasses
  • interface ComputerScientist with method
    writeProgram
  • class CSStudent implements Student and
    ComptuerScientist
  • must implement methods listClasses and
    writeProgram
Write a Comment
User Comments (0)
About PowerShow.com