8 - OO Multiple Inheritance, Abstract, Interfaces
8 - OO Multiple Inheritance, Abstract, Interfaces
Multiple Inheritance,
Abstract Classes, Interfaces
Written by John Bell for CS 342, Fall 2018
Frameworks
• Frameworks provide structure and support suitable
for use in a variety of ( related ) programs.
• For example, most Microsoft Office programs have
similar toolbars, menus, key commands and general
look-and-feel and behaviors.
• The Java API includes GUI classes suitable for many
apps, such as windows, buttons, menus, dialogs, …
• Specialty frameworks may apply to a particular
domain, such as e-commerce. ( shopping carts, ... )
• JUnit is a well-known framework for conducting
( automated ) unit tests of Java code. See also xUnit.
1
10/4/2018
Multiple Inheritance
• Languages such as C++ allow classes to inherit from
multiple sources, which can be quite useful:
Student Worker
StudentWorker
Student
double gpa gpa
StudentWorker gpa
int nConflicts nConflicts
2
10/4/2018
Student Worker
double gpa double salary
gpa salary
StudentWorker salary or gpa ?
int nConflicts nConflicts nConflicts
Student Worker
double gpa double salary
int hours double hours
gpa
StudentWorker salary
int nConflicts ???
nConflicts
3
10/4/2018
Student Worker
double gpa double salary
???
StudentWorker gpa
int nConflicts salary
nConflicts
4
10/4/2018
Interfaces
• Interfaces are like pure abstract classes - They
declare contractual obligations for implementers
without providing any implementations.
• “UML Distilled” by Fowler diagrams interfaces as:
5
10/4/2018
Benefits of Interfaces
• Interfaces define common required properties
( methods ) that different classes must provide,
without incurring the full overhead of inheritance.
• Classes implementing a common interface are not
related by either parent-child or sibling
relationships. They merely share some common
characteristics.
• Interfaces support polymorphism, with a common
set of method signatures for all implementing
classes.
Interfaces in Java
• Java does not allow multiple inheritance, to avoid all the
problems discussed earlier.
• However a Java class may implement as many Interfaces
as it wishes.
• Since there are now no actual methods or fields
inherited, there are no conflicts.
• An interface can extend another interface (but only 1).
• All Interface methods are public and abstract. And all the
fields are public, static, and final.
• Interfaces can’t be instantiated, but variables of an
Interface type can refer to any implementing class.
• ( someObject instanceof someInterface ) will return true if
someObject implements someInterface.
6
10/4/2018
New in Java 8
• Java 8 now allows Interfaces to define default
methods, only available when no other is provided.
• Overriding a default method makes the default
unavailable. ( default not reachable with “super”. )
• In the event of conflicts between multiply “inherited”
default methods, all default methods become null and
void. In this case the class is required to provide the
method(s), just as if there had not been any defaults to
begin with.
• Java 8 also now allows static methods in Interfaces,
callable only through the Interface name, and not
inherited.
7
10/4/2018
Review
Which of the following is NOT a problem with multiple
inheritance?
A. How to store fields inherited from multiple parents,
even when the fields are different.
B. How to resolve conflicts between the same field
name inherited from different parents, especially
when the data types are different.
C. How to handle the same field inherited from a
common ancestor via multiple parents ( The diamond
problem. )
D. None of the above. That is to say that all of the
above are problems.
E. The sparkling ruby problem.