July 7, 2008

Chapter: Interfaces

Create three interfaces, each with two methods. Inherit a new interface that combines the three, adding a new method. Create a class by implementing the new interface and also inheriting from a concrete class. Now write four methods, each of which takes one of the four interfaces as an argument. In main(), create an object of your class and pass it to each of the methods.


In Java:
package chapter.interfaces;

interface A {
void a1();
void a2();
}

interface B {
void b1();
void b2();
}

interface C {
void c1();
void c2();
}

interface D extends A, B, C {
void d1();
}

class E {
public void e1() {System.out.println("e1");}
}

class F extends E implements A, B, C, D {
public void a1() {System.out.println("a1");}
public void a2() {System.out.println("a2");}
public void b1() {System.out.println("b1");}
public void b2() {System.out.println("b2");}
public void c1() {System.out.println("c1");}
public void c2() {System.out.println("c2");}
public void d1() {System.out.println("d1");}
}



public class Exercise14 {
public static void foo(A a) {
a.a1();
a.a2();
}
public static void bar(B b) {
b.b1();
b.b2();
}
public static void yen(C c) {
c.c1();
c.c2();
}
public static void zen(D d) {
d.a1();
d.a2();
d.b1();
d.b2();
d.c1();
d.c2();
d.d1();
}
public static void main(String[] args) {
F f = new F();
foo(f);
bar(f);
yen(f);
zen(f);
f.e1();


}

}

0 comments: