Re: Query regarding java Multiple Inheritance
In article
<b66dd337-12b5-40e5-be01-9a52d159ec78@g1g2000pra.googlegroups.com>,
Amit Jain <amitatgroups@gmail.com> wrote:
I am new to Java and need your views for my query.
What will be the solution for implementint two Interface in a single
class A if these interfaces I1 and I2 are written as mentioned below:
interface I1{
void method ();
long square(long i);
}
interface I2{
String method();
long squareRoot(long i);
}
class A implements I1, I2{
void method(){
System.out.println("--- Hi ----");
}
// implementation of square() and squareRoot() method
}
Above implementation of interface in class A will never complies
because of "void method();" declared in both I1 and I2. And we want
the implementation of I1 and I2 in class A only. How can we acheive
this in such situations.
If the two methods named "method" are actually the same, derive A from
an abstract class that implements I1 & I2. See "When an Abstract Class
Implements an Interface":
<http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html>
For example,
public class AbstractInterface {
public static void main(String[] args) {
A a = new A();
System.out.println(a.method());
System.out.println(a.square(2));
System.out.println(a.cube(2));
}
interface I1 {
long square(long i);
}
interface I2 {
long cube(long i);
}
private static abstract class X implements I1, I2 {
public String method() {
return "Hi!";
}
}
private static class A extends X {
@Override
public long square(long i) { return i * i; }
@Override
public long cube(long i) { return i * i * i; }
}
}
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>