-4

I am learning java, and came to know that Java doesn't support multiple inheritance. So, java introduced interfaces for that. How does the problem of multiple inheritance is solved by interface?

What I read online : "Inheritance in java means code reuse".

If i implement an interface in class say A, I will have to give my own implementation for it in A, and same in B, which means methods in interface (say I) will have different implementation in A and B. How does it use the code re-usability feature?

Aman Grover
  • 45
  • 1
  • 10
  • You probably the see the https://stackoverflow.com/questions/21824402/java-multiple-inheritance – dabaicai Sep 17 '17 at 07:48
  • I know how interfaces work, but don't see how it solves multiple inheritance problem – Aman Grover Sep 17 '17 at 07:53
  • 2
    What makes you think that is *does* solve these problems? Where did you get the idea that it does from? Hint: if you can give us some citations, we can (maybe) figure out what it (the text you are reading) is actually saying, and explain it to you. – Stephen C Sep 17 '17 at 08:03
  • every website that I have referred to says that in java, multiple inheritance can be achieved through interfaces, so, doesn't inheritance mean code re-usability? – Aman Grover Sep 17 '17 at 08:12
  • Interfaces is not about code reuse, but rather about abstraction. One does not *inherit* an interface, but rather one *implements* an interface. – Joe C Sep 17 '17 at 08:17
  • 1
    *"Every website that I have referred to says that in java, multiple inheritance can be achieved through interfaces"* That is not a valid citation! *"so doesn't inheritance mean code re-usability"* Nope! Where did you read that? Citation please! – Stephen C Sep 17 '17 at 09:22

4 Answers4

3

You haven't given proper citations for any of the assertions that you make in your Question, so it is impossible to know what they are actually saying ... or why you think that they are say these incorrect or misleading things.


I am learning java, and came to know that Java doesn't support multiple inheritance.

That is correct.

So, java introduced interfaces for that.

That is INCORRECT. Java introduced interfaces to support polymorphism. Specifically polymorphism that does not depend on class inheritance.

How does the problem of multiple inheritance is solved by interface?

It isn't.

Multiple inheritances are about inheritance of method and field declarations from multiple parent classes. Interfaces does not provide that, and it does not solve the problem.

Interfaces do allow you to declare a common API (or APIS) and implement that API without necessarily sharing code. But with careful design you can do code-sharing under the hood any; e.g. by using the Delegation design pattern.

What I read online : "Inheritance in java means code reuse".

I doubt that what it actually said. Anyway, it is INCORRECT.

Inheritance (of classes) is one way to achieve code reuse, but it is only one of many ways to achieve reuse. There are many others.

If i implement an interface in class say A, I will have to give my own implementation for it in A, and same in B, which means methods in interface (say I) will have different implementation in A and B.

I presume that you mean something like this:

public interface I {
    void someMethod();
}

public class A implements I {
    void someMethod() {
         // some code
    }
}

public class B implements I {
    void someMethod() {
         // some code
    }
}

How does it use the code re-usability feature?

There is no code reuse in that example. But this is not inheritance of classes (which I described as one way to achieve code reuse). It is a class implementing an interface.

(Now in Java 8, you can put code into interfaces in certain circumstances; read up on the default methods feature. And that does allow direct code reuse via an interface. But I doubt that is what the sources you have been looking at are talking about.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Java solved the problem of multiple inheritance (or rather the problems that come with this feature) by allowing single inheritance, that is allowing only one super class. This design created a new problem of a class that needs to implement multiple contracts. a contract, like is explained in @Kermi's answer, allows other objects to refer to the same Object as different types, for various purposes, the most common one is for storing in Collections. an interface can be regarded as a super class that has no implementation (all the methods are pure virtual if you like)

So, Java removed the problems that come with multiple inheritance (the famous diamond problem) but also the advantages that come with it such as code reusability. This design follows the principal of making Java simple and easy and predictable by removing "advanced" (some say confusing) C++ features like multiple inheritance, operator overloading, pointer manipulation (among others).

There are several technics that allow Java to restore most of the code reusability of multiple inheritance. One such technic is composition: so if we take @Kermi's example, you can have a GeneralAnimal class that implements the most common behavior of an Animal. every Dog instance holds a reference to a GeneralAnimal instance (obtained through ctor or factory or dependency injection or ...) and can delegate some messages (=method calls) to that instance. The same is done in Cat instances and so on.

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
0

Interface doesn't resolve multiple inheritance problem or rather it doesn't create a multiple inheritance problem. It gives you a possibility to reuse existing implementations. For example:

class Dog implements Comparable<Dog>, Animal

As your class implements 2 interfaces you can use them in difeerent ways. To use TreeSet object needs to implement Comparable methods (it is not the only possibility). When Dog is passed to TreeSet implementation of that structure is then sure that object has compareTo(Dog dog) method and can use it. But than you want to store a List of Animals, and than iterate through that list with execution method declared for Animal, than you would not use Comparable interface, but Animal.

List<Animals> animals = new ArrayList<>();
animals.add(dog);
animals.add(cat);
for (Animal animal : animals) {
    animal.walk();
}
Kermi
  • 234
  • 1
  • 7
-2

Interface is a criterion, I think. A and B should conform to it. In addition, A and B do different things such as ArrayList and LinkedList. "Inheritance in java means code reuse" is right, but Interface is not. It embodies a norm. When you learn Collections, you will understand it clearly.

T Nian
  • 1