views:

177

answers:

2

hi i am new to java....moved from objective c (iphone background)

all we know that here we cannot use multiple inheritance....

alternative way is interface......

my question is...... does inheritance through interfaces make any sense...because we have to define the code of function in our class.......only helping part here is variables... it is like a single .h class in objective c...

i am talking about only functions of that interface...why to declare there....just to save 2 or three lines...that's it..

please tell why there is no multiple inheritance(simple reason...).

i know this may be a bad question to ask but.........

i am in little bit of dark please help.....

+6  A: 

Refer this answer of Stephan Schmidt . This answer is taken from the site http://codemonkeyism.com/java-interview-questions-mutliple-inheritance/

Does Java support multiple inheritance?

Well, obviously Java does not have multiple inheritance in the classical sense of the word. So the right answer should be “no”, or “no, but” or “yes, but”. From there one can explore several ways. Mostly I start by asking if the Java language designers were too stupid to implement multiple inheritance? Why did the C++ guys implement it then? We mostly land at the Diamond Anti-Pattern then: In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?

The other way to explore is how Java “emulates” multiple inheritance? The answer, which might already have surfaced, ist Interfaces. We then usually discuss interfaces in Java, if, when and how the candidate has used them in the past. What are interfaces good for, does he like them? I can explore how good he is at modelling and sometimes make him draw a diagram with interfaces. We go on with problems of interfaces in Java, and what happens when two interfaces have the same static fields and a class implements both – some kind of “multiple inheritance” in Java:

public interface I1 {
   String NAME = "codemonkeyism";
}

public interface I2 {
   String NAME = "stephan";
}

public class C implements I1, I2 {
   public static void main(String[] args) {
      System.out.println(NAME);
   }
}

Staying true, the language designer decided that this does not work in Java:

C.java:3: reference to NAME is ambiguous, both variable NAME
              in I1 and variable NAME in I2 match
      System.out.println(NAME);
                         ^
1 error

There are many more ways to explore with the candidate, e.g. what are the modifiers of interface methods. Are mixins or traits a better solution to the diamand problem than interfaces? Or are they just as bad as multiple inheritance? As I’m no longer very fond of inheritance and think most uses are a code smell, one can also discuss the down side of inheritance – thight coupling for example – with the candidate. Why?

Why do I ask this question and what do I learn from it? Inheritance is a very basic concept in OO and should be understood by every Java developer. Reflection on his work and going beyond knowing the syntax is an essential trait also, therefor the multiple inheritance question. I prefer questions that spawn many opportunities to explore. The inheritance question is one of them: Mutliple inheritance, language design, code smells, solutions, interfaces, role based development.

If you interview with me, you should know the answers, or tell me you’ve read my blog.

Tilsan The Fighter
+2  A: 

I would like to add something to the previous answer.

As you understood from the deep explanation given by Tilsan The Fighter multiple inheritance is not supported by Java.

Alternatively you should use one of delegation design patterns. I know that sometimes the code looks more verbose than code done using multiple inheritance but this is the price we have to pay.

You asked a question why do you need to create class that implements more than one interface? I'd ask you a question why do you need interface at all? I think that the answer is that this allows you to separate interface (contract) from concrete implementation, make modules independent, simplify re-factoring and make programs more flexible: you can switch implementations only if the concrete implementation is hidden by interface.

If your class can be used in different contexts as A and as B (where A and B are interfaces) the class should implement 2 interfaces.

Example: Let's assume that you have a business interface Foo that declares only one method

int foo(String str);

Now you create a couple of classes A and B that implement Foo:

public class A implements Foo {
    public int foo(String s) {
        return s.length();
    }
}

public class B implements Foo {
    public int foo(String s) {
        return s.hashCode();
    }
}

Now you would like to create collection of Foo:

Collection<Foo> collection = new ArrayList<Foo>();
collection.add(new A());
collection.add(new B());

Code that uses this collection does not know anything about the concrete implementation but it does not bother it to invoke foo().

Now you wish to sort this collection. One of the ways is to implement yet another interface Comparable:

public class A implements Foo, Comparable<Foo> {
    public int foo(String s) {
        return s.length();
    }
    public int compareTo(Foo o) {
        return getClass().getName().compareTo(o.getClass().getName());
    }
}

Once you are done you can say:

Collections.sort(collection);

And the collection will be sorted according to the rule defined in compareTo().

Now you probably wish to serialize the instances of A and B. To do this you have to implement yet another interface Serializable. Fortunately Serializable is a special interface that does not declare any method. JVM knows to serialize and desirialize objects that are instances of Serializable.

public class A implements Foo, Comparable<Foo>, Serializable {
    public int foo(String s) {
        return s.length();
    }
    public int compareTo(Foo o) {
        return getClass().getName().compareTo(o.getClass().getName());
    }
}

Class B looks the same.

Now we can say:

DataOutputStream os = new DataOutputStream(new FileOutputStream("/tmp/foo.dat"));
os.writeObject(new A());
os.writeObject(new B());
os.flush();
os.close();

and store our objects in file /tmp/foo.dat. We can read the objects later.

I tried to show why do we need classes that implement several interfaces: each interface gives the class its behavior. Comparable allows sorting collection of such instances. Serializable allows serialization etc.

AlexR