why multiple inheritances are not possible in java [duplicate] - java

This question already has answers here:
Why is Multiple Inheritance not allowed in Java or C#?
(17 answers)
Java Multiple Inheritance
(17 answers)
Closed 8 years ago.
"Why multiple inheritance is not possible in java ?" is any different to the question
"Why multiple inheritance is not supported in java ?" or both are inter related .
I know this has already been answered , just looking for a difference if any not the answer to the why.

Both are same question.
If it is possible, we may end up with famous Diamond death problem.
The reason that Java's creators chose not to allow multiple inheritance is that it can become quite messy.
In a nutshell, the problem is that if a class extended two other classes, and both superclasses had, say, a doStuff() method, which version of doStuff() would the subclass inherit?This issue can lead to a scenario known as the "Deadly Diamond of Death,"
Because of the shape of the class diagram that can be created in a multiple inheritance design. The diamond is formed when classes B and C both extend A, and both B and C inherit a method from A. If class D extends both B and C, and both B and C have overridden the method in A, class D has, in theory, inherited two different implementations of the same method. Drawn as a class diagram, the shape of the four classes looks like a diamond."
So in Java for simplicity, you have one and only one base class. Searching for the base class is a linear search from top to bottom, simple, fast and efficient.
But multiple inheritance is useful, it's conceivable that the same class may want to represent itself differently to different clients. That is done by using interfaces. An interface is just like a base class, but carries no data and no methods.
When you implement an interface you have to supply all the members it needs, which is simple to understand. When the computer casts to the interface all it needs to do is start from the type object and search upwards through the base classes looking for one that implements the interface.
Interfaces do 95% of the work multiple inheritance for 10% of the brain space and 15% of the CPU space. The 5% that interfaces can't do that multiple inheritance can may be simply implemented by composition.
That is why its not supported.

Related

how is abstract class different from an interface in terms of abstraction concept [duplicate]

This question already has answers here:
Interface vs Abstract Class (general OO)
(36 answers)
Closed 4 years ago.
I have read number of articles about differences between abstract classes and interface. can someone please specify the conceptual difference between the two, i am summarizing my understanding so far as:
When we talk about abstract classes we are defining characteristics of an object type; specifying what an object is.
When we talk about an interface and define capabilities that we promise to provide, we are talking about establishing a contract about what the object can do
Please refrain from using Car, Animal examples for differentiating between the two terms.
I am more interested to understand conceptually how abstract class and interface fits into the concept of abstraction. If someone could additionally talk about abstraction concept in relation to abstract class or interface
As far as "abstraction" is concerned, interfaces and abstract classes are very similar.
In Java, "abstraction" is made concrete (couldn't help using this oxymoron) by specified, but unimplemented behavior. In this regard, interfaces and abstract classes are the same. And the biggest sign is that no objects can be made directly from either.
What makes interfaces different from abstract classes has little to do with the concept of abstraction. For example, the fact that abstract classes can have state and constructors gives them some "concrete" nature, although this doesn't change much of their difference from interfaces, as far as "specified/unimplemented" behavior is concerned.

How interfaces in java used for code reuse? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
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?
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.)
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.
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();
}
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.

Implement multiple classification in Java [duplicate]

This question already has answers here:
Java Multiple Inheritance
(17 answers)
Closed 8 years ago.
Is there a way to implement multiple classification in Java? For instance, take this UML diagram -
How should the GrainCrop class be implemented?
Interfaces. One or both of Crop and FoodItem would be an interface to be implemented by GrainCrop. So either:
GrainCrop is a Crop (and inherits its behaviors) and exhibits the behavior of FoodItem. In this case Crop is a base class (potentially abstract) and FoodItem is an interface.
GrainCrop is a FoodItem (and inherits its behaviors) and exhibits the behavior of Crop. In this case FoodItem is a base class (potentially abstract) and Crop is an interface.
GrainCrop exhibits the behaviors of both FoodItem and Crop. In this case both FoodItem and Crop are interfaces.
In a single-inheritance environment you take specific care to identify what something "is" vs. what behaviors it exhibits. (This is one of the reasons why interfaces are often described by their behaviors, such as Eatable, rather than their structure, such as FoodItem. It makes the modeling more clear.)
As an often handy over-simplification, think of a base class as what something "is" and an interface as what something "does." An object can only be one thing, but it can do many things.
Java does not support multiple inheritance on classes, only interfaces can inherit from multiple interfaces. So what I would do is that the Class GrainCrop in question implements the interface from the two other classes. Then GrainCrop aggregates the two other classes and delegates to the "inner" objects. The calls in GrainCrop can then also be decorated if you need to do additional stuff. E.g.
public void methodFromClassCrop() {
System.out.println("Stuff before call");
oneOfTheObjects.methodFromClassCrop();
System.out.println("Stuff after call");
}
Of course you can only introduce an interface for one of the classes and inherit from the other. It depends on the question if your new class extends one of the other classes in a sense of: introduce new behavior, algorithm, data which uses the other data, or if you just "act" (aggregation in this case) on them.
Well, given the fact that FeedItem doesn't appear to have any implementation, I would make it an interface, make Crop a class (possibly abstract), and have GrainCrop extend both of them. You can only extend one class, but you can extend as many interfaces as you want.

Is possible to support multiple inheritance through virtual?

I know that Java does not have any ability to support Multiple Inheritance. For an example, class C inherits all the properties from class A and class B then the compiler gets confused which method should be called which is defined in A and B. So, C++ supports multiple Inheritance and Java does not support.
I know that by using Interface can achieve this Multiple Inheritance in Java.
My doubt is, whats that Diamond problem and how could be solved in Java?
For an example, consider multiple classes such as A, B, C, D. Class B and Class C inherits the properties from Class A and class D inherits the properties from both class B and Class C. This is called as "Diamond problem".
How can we solve this Diamond problem in Java and when does this Diamond problem may occur exactly in coding?
I also referred that in C++ by using Virtual can achieve this Multiple Inheritance concept. And, Java is built and designed for simplicity !
What is that actual meaning of Virtual in C++ and how could be used in Java? Is it possible to achieve multiple Inheritance through Virtual?
I am Java Beginner and very sorry for this kinda question. But, I believe that can learn from my mistakes!
There is no concept of multiple inheritance in Java. Implementing multiple interfaces is possible, but this is not multiple inheritance.
Because there's no multiple inheritance, the "diamond problem" that arises in languages that do have multiple inheritance does not arise in Java. So there is no virtual keyword in Java - there's no need for it.
The designers of Java decided that the number of cases in which multiple inheritance is actually useful is so small that it wasn't worth including in the language. In approximately 15 years of programming Java, I have only once encountered a business problem in which C++-style multiple inheritance would have been useful. So I'm happy to say they were right.
If you want multiple 'inheritance', you need to implement multiple interfaces. Each interface declares abstract methods that the implementing class needs to provide definition for.
public class MyClass implements interface1, interface2, interface3{
// provide a definition to the abstract methods of interface
}
The 'diamond' problem in C++ is when a class inherits from two classes , both of whom have methods from a common grand parent. In Java, there is no diamond problem because you can never extend two classes; just one class and many interfaces.
You are combining far too many questions. I recommend that you split your post into separate questions.

Need more logical answer to "Why Java has no multiple inheritance" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why there is no multiple inheritance in Java, but implementing multiple interfaces is allowed
Every answer I have seen so far regarding "Why Java has no multiple inheritance" has only one answer in more specific or detailed way that is "To reduce complexity" but no one defined how it reduces complexity, If we use interface instead of class what difference it makes.Isn't it one and a same thing? what is the difference if we implement an interface and not extend our class?Some one answered with Diamond problem but interfaces can also produce diamond problems.
The difference between multiple inheritance among interfaces vs. classes is when you must inherit implementation. When you inherit method interface through multiple paths, you can say that the implementing class must implement the inherited method. When you inherit from multiple classes, you must decide which of the several implementations to choose. This increases the complexity of the language very significantly, as you can see by examining the way multiple inheritance is implemented in C++.
Here is an illustration:
public class Base {
public void foo() {System.out.println("base");}
}
public class A extends Base {
public void foo() {System.out.println("a");}
}
public class B extends Base {
public void foo() {System.out.println("b");}
}
public class AB extends A, B /* imagine that it's a possibility */{
}
What is going to happen when you do this?
AB ab = new AB();
ab.foo();
With inheritance of interfaces, AB would have to implement foo; with inheritance of implementation, the language would need to decide, or provide you a way to specify it yourself. One way or the other, the complexity is going to grow.
Interfaces don't come with code: only a set of methods that must be implemented.
Classes come with code. When you have a diamond problem with classes, you end up with two implementations for the same function, that may reference the same variables.
With interfaces, there's only ever one implementation, although the interface can come from many places.

Categories

Resources