0% found this document useful (0 votes)
27 views19 pages

Java Interfaces: at Most One Superinterface

Java interfaces declare methods but provide no implementation. Classes that implement interfaces must provide an implementation for each declared method. Interfaces can inherit from other interfaces and a class can implement multiple interfaces. The document then provides examples of interfaces Printable and Displayable and a Circle class that implements the Displayable interface, requiring it to implement all methods from Printable and Displayable.

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views19 pages

Java Interfaces: at Most One Superinterface

Java interfaces declare methods but provide no implementation. Classes that implement interfaces must provide an implementation for each declared method. Interfaces can inherit from other interfaces and a class can implement multiple interfaces. The document then provides examples of interfaces Printable and Displayable and a Circle class that implements the Displayable interface, requiring it to implement all methods from Printable and Displayable.

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Java Interfaces

● Interfaces declare features(i.e. methods) but provide no


implementation
● classes that implement an interface must provide an
implementation for each feature
● Interfaces can inherit from another interface
– at most one superinterface
● A Java class can implement more than one interface
– Java inherits(extends) from at most one type, but can
implement more than one interface.

1
Java Interfaces (cont)
● Printable defines 3 interface Printable {
methods public String printInstVar();

public String showInfo();


● Displayable inherits from
Printable and adds some public String printWithSpaces();
}
more method signatures
● A class that implements
interface Displayable extends
Displayable will have to Printable {
provide an public String display();

implementation for all 6 public String refresh();


methods.
public void displayColor(Color c);
}

2
Java Interfaces (cont)
public class Circle implements
● keyword implements Displayable {
private int radius;
followed by a list of one public String printInstVar(){
System.out.println(“Radius “
or more comma separated +radius);
interface names }
public String showInfo(){
● Method signatures must System.out.println(“ I am a
Triangle with radius “
match + radius);
}
– same modifiers public String printWithSpaces(){
...
– same method names }
public String display(){
– same number of arguments ...
}
– corresponding argument public String refresh(){
types. ...
}
public void displayColor(Color c){
...
}
} 3
Types Revisited
● In Java each interface defines a type. Interface extension
and implementation as subtype relationships
● A subtype relation in Java is:
– if class C1 extends class C2 then C1 is a subtype of C2
– if interface I1 extends I then I is a subtype of I
– if class C implements interface I then C is a subtype of I
– for every interface I, I is a subtype of Object
– for every type T , T[ ] is a subtype of Object
– if T1 is a subtype of T2 then T1[ ] is a subtype of T2[ ]

4
Types of Circle
public class Circle implements
● Circle is a subtype of Displayable {
private int radius;
public String printInstVar(){
– Object System.out.println(“Radius “
+radius);
– Displayable }
– Printable public String showInfo(){
System.out.println(“ I am a
Triangle with radius “+ radius);
}
public String printWithSpaces(){
...
}
public String display(){
...
}
public String refresh(){
...
}
public void displayColor(Color c){
...
}
}
5
Inheritance and its forms
● Combination
– child class inherits features from more than one parent
● Java does not directly support this last form, although we
can simulate it (more on this next time)

● Using interfaces a class can inherit features from more


that one parent.
– parents do not have to be in an direct inheritance relationship

6
Students, TAs and Professors
● Modeling a department with
– students
– TA
– professors
● Students
– gpa, full-time or part-time, courses
● TA
– is a student, office hours, course TAing for and professor
● Professors
– office hours, teaching courses, TA for each one

7
Students, TAs and Professors (cont)
● The goal is not only to correctly implement but to also
capture each concept separately.
– design is equally important

Student TA Prof

teaching course
full-time, part-time
assigned TA/Prof
GPA
office hours
8
Students, TAs and Professors (cont)
● Doing this only with Classes and inheritance
– TA to inherit from Student and Prof,
● impossible in Java
– Can make Prof and Student inherit from TA
● exposes unused methods inside Prof and TA
– Use inheritance for construction
● keep inside TA an instance of Student and Prof
– lose substitutability (less flexible design)
● Let's try interfaces
– define a interface for each role( TA, Prof, Student) that
enforces each role's features

9
Students, TAs and Professors (cont)

Person <<Personify>>

StudentImpl ProfessorImpl
<<Teaching>> <<Student>>

TAImpl
<<TA>> <<Professor>>

10
Students, TAs and Professors (cont)
● Declare a type Personify as a Java interface type holding
features shared by all
● Declare an abstract class Person that implements these
common features
● For each role, define a corresponding interface
– abstract away common behavior (i.e. Teaching)
● Create implementation classes for each of the roles
– a student implements the Student Interface
– a TA implements the TA Interface
– a Professor implements the Professor Interface
● Check the source code on the class web page.

11
Dynamic Data Structures
● Data Structures that have the ability to dynamically alter
some of their properties like
– e.g. size
● Some examples
– LinkedList, Queues, Trees, HashTables
– standard implementations are available in the standard Java
library classes. Most of them under java.util
● We will examine some of these

12
LinkedList
● a collection of locations with references from one cell to
the next
● SingleLinkedList

DoublyLinkedList

13
LinkedList (Java)
● Rich set of operations
– add
● at a specific index, begging, end
– size
– remove
● an Object, first, last, at a specific index
● Return methods give you back an instance of type Object

14
LinkedList (Java) and Iterators
● Java provides a convenient way to go through an list, an
iterator
● iterator() - returns an instance of an iterator initialized to
point to the first element of the list.
● iterators can alter the underlying list elements !
LinkedList myList = new LinkedList();
myList.add("The");
myList.add("quick");
myList.add("brown");
myList.add("fox");
Iterator it = myList.iterator();
while(it.hasNext()){
System.out.print((String)it.next());
System.out.print(" ");
}
System.out.println("!");
15
Stack
● LIFO stack of objects
● Operations
– push(Object) – place
something on the top of the d
stack
c
push(a); b
push(b);
push(c); a
push(d);

16
Stack (cont)
● LIFO stack of objects d
● Operations
– push(Object) – place
something on the top of the
stack
– pop():Object – remove the c
first element of the stack
b
a
pop();

17
Stack (cont)
● LIFO stack of objects
● Operations
– push(Object) – place c
something on the top of the
stack
– pop():Object – remove the c
first element of the stack
b
– peek():Object – look at the
first element without a
removing it

peek();

18
Stack (cont)
● LIFO stack of objects
● Operations
– push(Object) – place
something on the top of the
stack
– pop():Object – remove the c
first element of the stack
b
– peek():Object – look at the
first element without a
removing it
– empty():boolean – check to
see if the stack is empty

19

You might also like