XML security work
Download
Report
Transcript XML security work
Designing with Java
interfaces
Gwan-Hwan Hwang (黃冠寰)
Internet Computing Lab.
Dept. Computer Science & Information Engineering
National Taiwan Normal University
台灣師範大學資訊工程學系
http://iclab.csie.ntnu.edu.tw/
Source of this presenation
Designing with interfaces
One programmer's struggle to understand the
interface
By Bill Venners, JavaWorld.com, 12/01/98
http://www.javaworld.com/javaworld/jw-121998/jw-12-techniques.html
2
Deciphering the interface
Java interfaces primarily as a special kind of
multiple inheritance: multiple inheritance of
interface (the object-oriented concept) without
multiple inheritance of implementation.
Prior to the advent of Java, I spent five years
programming in C++, and in all that time I had
never once used multiple inheritance.
In contrast to multiple inheritance in C++,
which in five years I never used, I was using
Java's interfaces all the time.
3
Interfaces and the 'diamond
problem'
The diamond problem is an ambiguity that can
occur when a class multiply inherits from two
classes that both descend from a common
superclass.
4
Interfaces and the 'diamond
problem‘ (Cont’d)
Multiple inheritance in Jurassic Park
5
Interfaces and the 'diamond
problem‘ (Cont’d)
Here's what the code might look like if Java supported
traditional multiple inheritance:
6
abstract class Animal {
abstract void talk();
}
class Frog extends Animal {
void talk() {
System.out.println("Ribit, ribit.");
}
class Dinosaur extends Animal {
void talk() {
System.out.println("Oh I'm a dinosaur and I'm OK...");
}
}
// (This won't compile, of course, because Java
// only supports single inheritance.)
class Frogosaur extends Frog, Dinosaur {
}
Syntax error
7
Interfaces and the 'diamond
problem‘ (Cont’d)
Because of the ambiguity caused by the
diamond problem, it isn't clear whether the
runtime system should invoke Frog's or
Dinosaur's implementation of talk(). Will a
Frogosaur croak "Ribbit, Ribbit." or sing "Oh, I'm
a dinosaur and I'm okay..."?
Animal animal = new Frogosaur();
animal.talk();
8
Interfaces and the 'diamond
problem‘ (Cont’d)
In Java, interfaces solve all these ambiguities caused
by the diamond problem.
Through interfaces, Java allows multiple inheritance of
interface but not of implementation.
Implementation, which includes instance variables and
method implementations, is always singly inherited.
As a result, confusion will never arise in Java over
which inherited instance variable or method
implementation to use.
9
Interfaces and polymorphism
Sure, the interface represented Java's way of
dealing with the diamond problem, but was
that the key insight into the interface?
As time went by I began to believe that the key
insight into the interface was not so much
about multiple inheritance as it was about
polymorphism.
The interface lets you take greater advantage
of polymorphism in your designs, which in turn
helps you make your software more flexible.
10
Interfaces and polymorphism
(Cont’d)
Ultimately, I decided that the "point" of the
interface was:
Java's interface gives you more polymorphism than
you can get with singly inherited families of classes,
without the "burden" of multiple inheritance of
implementation.
11
A refresher on polymorphism
This section will present a quick refresher on
the meaning of polymorphism.
12
A refresher on polymorphism
(Cont’d)
abstract class Animal {
abstract void talk();
}
class Dog extends Animal {
void talk() {
System.out.println("Woof!");
}
}
class Cat extends Animal {
void talk() {
System.out.println("Meow.");
}
}
13
A refresher on polymorphism
(Cont’d)
Given this inheritance hierarchy, polymorphism
allows you to hold a reference to a Dog object
in a variable of type Animal, as in:
Animal animal = new Dog();
14
A refresher on polymorphism
(Cont’d)
Polymorphism in Java is made possible by dynamic
binding, the mechanism by which the Java virtual
machine (JVM) selects a method implementation to
invoke based on the method descriptor
For example, the makeItTalk() method shown below
accepts an Animal reference as a parameter and
invokes talk() on that reference:
class Interrogator {
static void makeItTalk(Animal subject) {
subject.talk();
}
}
15
A refresher on polymorphism
(Cont’d)
At compile time, the compiler doesn't know exactly
which class of object will be passed to makeItTalk() at
runtime.
It only knows that the object will be some subclass of
Animal.
Furthermore, the compiler doesn't know exactly which
implementation of talk() should be invoked at runtime.
class Interrogator {
static void makeItTalk(Animal subject) {
subject.talk();
}
}
16
A refresher on polymorphism
(Cont’d)
Polymorphism helps make programs more
flexible, because at some future time, you can
add another subclass to the Animal family, and
the makeItTalk() method will still work. If, for
example, you later add a Bird class:
class Bird extends Animal {
void talk() {
System.out.println("Tweet, tweet!");
}
}
17
Getting more polymorphism
Interfaces give you more polymorphism than
singly inherited families of classes, because
with interfaces you don't have to make
everything fit into one family of classes. For
example:
18
Getting more polymorphism
(Cont’d)
interface Talkative {
void talk();
}
abstract class Animal implements Talkative {
abstract public void talk();
}
class Dog extends Animal {
public void talk() {
System.out.println("Woof!");
}
}
class Cat extends Animal {
public void talk() {
System.out.println("Meow.");
}
}
class Interrogator {
static void makeItTalk(Talkative subject) {
subject.talk();
}
}
19
Getting more polymorphism
(Cont’d)
Given this set of classes and interfaces, later
you can add a new class to a completely
different family of classes and still pass
instances of the new class to makeItTalk(). For
example, imagine you add a new CuckooClock
class to an already existing Clock family:
class Clock {
}
class CuckooClock implements Talkative {
public void talk() {
System.out.println("Cuckoo, cuckoo!");
}
}
20
Getting more polymorphism
(Cont’d)
Because CuckooClock implements the Talkative
interface, you can pass a CuckooClock object to
the makeItTalk() method:
class Example4 {
public static void main(String[] args) {
CuckooClock cc = new CuckooClock();
Interrogator.makeItTalk(cc);
}
}
21
Getting more polymorphism
(Cont’d)
With single inheritance only, you'd either have
to somehow fit CuckooClock into the Animal
family, or not use polymorphism.
With interfaces, any class in any family can
implement Talkative and be passed to
makeItTalk().
This is why I say interfaces give you more
polymorphism than you can get with singly
inherited families of classes.
22
The 'burden' of implementation
inheritance
Read it yourself.
23
Multiple Inheritance of interfaces
in Java
類別不允許多重繼承,是因為物件會因繼承順序
的不同而出現問題;介面中則允許多重繼承,因
為所有介面都只有定義常數與抽象方法,與繼承
順序無關。
See the code example.
24