Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Java Alternative to Multiple Inheritance

Similar presentations


Presentation on theme: "The Java Alternative to Multiple Inheritance"— Presentation transcript:

1 The Java Alternative to Multiple Inheritance
Interfaces The Java Alternative to Multiple Inheritance Copyright © Curt Hill

2 Introduction There are two similar but different useful techniques in Java Derivation Interfaces The good Java programmer needs both Derivation is widely used to add specialization to classes Interfaces are required for Layout Managers, among others Copyright © Curt Hill

3 Derivation & Interfaces
Derivation allows a new class to inherit properties and methods from an ancestor Inheritance is discussed at length in a previous presentation Interfaces allow a class to implement common features without inheriting code or data Used to replace multiple inheritance Copyright © Curt Hill

4 Multiple inheritance C++ and Eiffel and other languages allow
The iostreams are classic examples The formatting comes from one class The I/O from another Works very nicely SmallTalk had it then removed it Unfortunately there are problems Recall the person inheritance hierarchy Copyright © Curt Hill

5 person set_name employee student set_wage set_major grad set_degree
Copyright © Curt Hill

6 Multiple Inheritance Problems
Suppose that we wanted a student employee We could then derive a class from both student and employee The class would have two name properties One from person through student One from person through employee How do we distinguish between the two Copyright © Curt Hill

7 Multiple Inheritance Problems like this have not been resolved well in any programming language Eiffel comes closest Thus Java. C# and Smalltalk disallow multiple inheritance Java and C# substitute the Interface This gives many of the advantages None of the problems Copyright © Curt Hill

8 Interfaces An interface is somewhat like a pure abstract base class
It defines no code and only constant data It defines what methods are needed, without giving any indication at all about the implementation Provides signatures only If a class claims that it implements an interface it must implement these defined methods Otherwise compile errors Copyright © Curt Hill

9 Using an interface Using an interface requires the implements reserved words: class xyz implements abc {…} abc must be a defined interface A class may implement several interfaces but only be derived from one class Derivation uses the extend reserved word Copyright © Curt Hill

10 Defining an interface Similar to a class:
public interface Employee { void set_wage (double w); } Notice this does not set the wage field This is only the signature – no code Multiple methods are possible An interface may also extend another interface Copyright © Curt Hill

11 Creating the employee Here we have both derivation and interface class employee extends person implements Employee{ class stu_emp extends student implements Employee{ Either one will be abstract if it does not implement set_wage An abstract class may not be instantiated Like an abstract base class Useful but not as common as instantiable classes Copyright © Curt Hill

12 Why use interfaces? Several common examples that we will use
Layout Managers Event handlers in AWT 1.1 (or later) event model Image handlers Especially in Applets Multi-threaded classes Each of these that we need will be considered in a separate presentation Copyright © Curt Hill

13 Abstract Base Class or Interface?
An interface tells us all that we need to know: the method signature There is no common code or data An abstract base class usually adds common code and data A class may only have one base class, but many interfaces Copyright © Curt Hill

14 Finally We have looked at this in a somewhat formal sense
What we next need is both the usage of an interface and later the definition of a worth while interface Copyright © Curt Hill


Download ppt "The Java Alternative to Multiple Inheritance"

Similar presentations


Ads by Google