Presentation is loading. Please wait.

Presentation is loading. Please wait.

40-244 – Advanced Programming P ROGRAMMING IN Lecture 16 Interfaces.

Similar presentations


Presentation on theme: "40-244 – Advanced Programming P ROGRAMMING IN Lecture 16 Interfaces."— Presentation transcript:

1 40-244 – Advanced Programming P ROGRAMMING IN Lecture 16 Interfaces

2 What We Will Learn Typing Viewpoint Abstract Classes as types The problem with multiple inheritance Interfaces

3 Typing Viewpoint Recall that every object has Interface Implementation Interface is the type Implementation is the code

4 Inheritance and Typing Inheritance decouples type from code Same code works for several types Objects decide how to do their own work

5 An Example List is a type Its operations include: adding an object removing an object accessing a specific element May be implemented using linked-lists or arrays, etc.

6 The List Abstract Class public abstract class List { public abstract void add(Object o); public abstract void remove(int index); public abstract Object get(int index); } public class LinkedList extends List { public void add(Object o) {...}... }

7 Having Multiple Types What if we want to view a class from different perspectives? Example: We may want to define a ListView class It is both a List and a View (something that can be drawn on screen)

8 Multiple Inheritance From the previous example, there is a need for multiple inheritance Multiple inheritance means having multiple superclasses ListView ListView

9 No Multiple Inheritance! Java like some other languages doesn't support multiple inheritance Suppose A has a method m which B and C override it What happens to the m that D inherits? interface of m is the same in all of them But the implementation differs BC D A

10 The Problem Subclassing is the implementation or code inheritance Since the superclass may define body of some methods What we needed from the ListView example was the interface inheritance

11 The Solution Having something that just defines interface public interface List { void add(Object o); void remove(int index); Object get(int index); } public class LinkedList implements List { public void add(Object o) {...}... }

12 Interfaces Can be used as a type name void printAll(List l) {...} All methods are public by default No method can have implementations All fields are considered static final So there is no implementation in it

13 Some Examples java.util.List java.awt.event.MouseListener java.awt.event.KeyListener public class ListView implements List, View {... }

14 Interface Inheritance Can be extended using inheritance Can have multiple superinterfaces


Download ppt "40-244 – Advanced Programming P ROGRAMMING IN Lecture 16 Interfaces."

Similar presentations


Ads by Google