From the course: Programming Foundations: Design Patterns

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Trying interfaces

Trying interfaces

From the course: Programming Foundations: Design Patterns

Start my 1-month free trial

Trying interfaces

- [Instructor] Now if you're a well-schooled Java developer, you might be saying, hey, let's use interfaces instead. You might remember that an interface defines the methods an object must implement in order to be a particular type. For instance, if a duck implements a flyable interface with one method fly, then duck must implement the method fly to be considered the flyable type. So interfaces allow different classes to share similarities like ducks sharing a fly behavior. Interfaces also allow for having two classes that are alike, but don't have the same behavior, for some ducks being able to fly and some not. So interfaces actually sound perfect for our design. Let's rework our duck interfaces and see how well this works out. We'll begin by creating two interfaces, flyable and quackable, if I'm a duck and I fly, I implement the flyable interface, and if I'm a duck and I quack, I implement the quackable interface. And our duck superclass now just needs a swim method since all ducks…

Contents