Advanced Programming
SUT • Spring 2019
interface
Multiple Inheritance
interface Shape {
int getColor();
double getArea();
double getPrimeter();
}
All the methods are implicitly abstract
No need to abstract specifier
All the methods are implicitly public
No need to public specifier
interface Shape {
int getColor();
double getArea();
double getPrimeter();
}
Some classes may inherit abstract classes
Some classes may implement interfaces
Is-a relation exists
If a class implements an interface
But does not implement all the methods
What will happen?
The class becomes an abstract class
class Rectangle implements Shape {...}
class Rectangle implements Shape {
private double width, length;
private int color;
public Rectangle(double width, double length, int color) {
this.width = width;
this.length = length;
this.color = color;
}
public int getColor() {
return color;
}
public double getArea() {
return width * length;
}
public double getPrimeter() {
return 2 * (width + length);
}
}
A class can inherit one and only one class
A class may implement zero or more interfaces
interface CanFight {
void fight();
}
interface CanSwim {
void swim();
}
interface CanFly {
void fly();
}
class ActionCharacter {
private String name;
public String getName() {
return name;
}
}
class Hero extends ActionCharacter implemenets CanFight, CanSwim,
CanFly {
public void fly() {}
public void swim() {}
public void fight() {}
}
Why multiple inheritance is not supported for classes?
Why multiple inheritance is supported for interfaces?
The return types are incompatible for the inherited methods B.f(), A.f()
interface A {
int f();
}
interface B {
void f();
}
public abstract class TestCase implements A, B {};
interface CanFight {
void fight();
void move();
}
interface CanSwim {
void swim();
}
interface CanFly {
void fly();
void move();
}
class ActionCharacter {
public void fight() {}
}
class Hero extends ActionCharacter implemenets CanFight, CanSwim,
CanFly {}
Interfaces may inherit other interfaces
Code reuse
Is-a relation
interface CanSee{
void move();
void see();
}
interface CanMove{
void move();
}
interface CanFight{
void fight();
}
No member variable
Variables: implicitly final and static
Usually interfaces declare no variable
Only operations are declared
No constructor
Why?
interface A {
int a = 5;
int f();
}
public class TestClass implements A {
public static void main(String[] args) {
System.out.println(a);
}
}
Pure Abstract classes
Describe the design
The architect designs interfaces, the programmer implements them
Only interfaces are delivered to code consumers
More powerful inheritance and polymorphism
public interface Interface {
void method(String str);
default void log(String str) {
System.out.println("logging::" + str);
Interface.print("abc");
}
}
public interface OtherInterface {
void otherMethod();
default void log(String str) {
System.out.println("Other logging::" + str);
}
}
public class Concrete implements Interface, OtherInterface {
@Override
public void method(String str) {
}
@Override
public void otherMethod() {
}
@Override
public void log(String str) {
System.out.println("MyClass logging::" + str);
}
}
public interface Interface {
void method(String str);
default void log(String str) {
System.out.println("logging::" + str);
Interface.print("abc");
}
static void print(String str) {
System.out.println("Interface Print " + str + ".");
}
}
public interface Interface {
void method(String str);
default void log(String str) {
System.out.println("logging::" + str);
Interface.print("abc");
}
private void print(String str) {
System.out.println("Interface Print " + str + ".");
}
}