I have the following situation:
I need my class let's say A to extend JComponent and a different class let's say B.
Now, I know that multiple inheritance is not possible in java, and that a proper way to do this would be to implement BInterface which will give me access to all methods from B.
The issue is that class B contains ~15 methods, and inside A I only need to override 2 of them, and all the other ones to use the 'super' implementation.
Question is: is there a possible way to do this without having to implement inside A all the methods from B?
Note: I'm not trying to add too much complexity to my code, if the 'workaround' for this problem breaks oop programming principles then I should better redesign my code.
As you said multiple inheritance is not supported in java. But in general its a good practice to use composition instead of inheritance [Effective Java - item 18]. Read about it here
You can use default implementation of java 8 feature.
Create an interface named as ABinterface. provide all 13 methods as default means
having definition and declare 2 methods (like abstract methods).
Class A and B will implement ABinterface and override those 2 methods according to
their need.
Class A which will extend jsComponent and implement ABInterface can access all
other existing 13 methods also.
You haven't provided the enough information in this query. So you need to think according
to real word scenarios with oops concepts before implementing this.
You can also use composition instead of inheritance.
I'm making a game in Java SE 8. I have to add a class in extends but there is already one added, and I cannot remove it. I would just like to know how to put 2 classes in extends.
you can not extends 2 classes in java. java doesn't support multiple inheritance. you can make it like if you have 3 classes A, B, C than A extends B and B extends C.
Extends models inheritance. In Java there is only "single inheritance", in other words: you can only have a single super class to extend from (which itself can only extend from one super class, and so on). You can have multiple interfaces, and since Java 8 interfaces can also have some implementation, but it's a controversial subject, maybe just a loophole. Most of the time, your desire for multiple inheritance can be solved by a better design of your class. Maybe your class is trying to do too many things (too many responsibilities), and you can split it up. Maybe you can use composition (using delegate classes, either via fields on your class, or by calling into static methods on utility classes), instead of inheritance.
I just had an interview, and I was asked a question.
Interviewer - Does Java support multiple inheritance?
Me - No
Interviewer - Each class in Java extends class Object (except class Object) and if we externally extend one class like
Class A extends B{
// some code here
}
then you can say that class A extend class B and class Object, which means it is multiple inheritance. So how can you say Java does not support multiple inheritance?
Me - Actually class B extends class Object, so when you extend class B in class A then class A extends class Object indirectly. This is multi-level inheritance, not multiple inheritance.
But my answer did not satisfy him.
Is my answer correct? Or where am I wrong?
What actually happens internally?
My answer is correct?
Yes, mostly, and certainly in the context you describe. This is not multiple inheritance:
It's what you said it is, single inheritance with multiple levels.
This is multiple inheritance: Inheriting from two or more bases that don't have any "is a" relationship with each other; that would be inheriting from unrelated lines, or from lines that had previously diverged (in Java, since Object is always a base, it would be the latter):
(Image credits: http://yuml.me in "scruffy" mode)
Internally What happens actually?
Just what you said: There are multiple levels. When the compiler is resolving a member on an instance:
obj.member
...it looks to see if the type of obj (which in this case is a class, say ClassB) has member, either because it provides it directly or it has it through inheritance. At runtime, the JVM uses the member the object actually has.
The reason I said "mostly" above is that Java has interfaces, and as of Java 8 it has "default methods" on interfaces. This complicates things a bit, but your answer about levels is correct in the context of what you described the interviewer saying about Object, ClassA, and ClassB.
Interfaces have always made it possible, in Java, for something to have an "is a" relationship with two different types: A class type it inherits from, and any of several interface types it implements. Interfaces without default methods aren't multiple inheritance in a practical way (the class has to provide the implementation), but they did make it possible for a class to have multiple "is a" relationships from unrelated type trees. (I'm not an academic, it's possible an academic would argue that they provide multiple inheritance in an academic way.)
With Java 8, interfaces can provide default implementations of the methods they define, which really blurs the lines even at the practical level. Let's look at that a bit more deeply:
Say we have ClassA:
class ClassA {
void doSomething() {
// Code here
}
}
and Interface1:
interface Interface1 {
default void doSomethingElse() { // Requires Java 8
// Code here
}
}
and finally ClassB:
class ClassB extends ClassA implements Interface1 {
}
ClassB inherits the implementation of doSomething from ClassA. But it also gets the "default" version of doSomethingElse from Interface1. We didn't implement it in ClassB, but ClassB isn't abstract: It really has doSomethingElse. It gets it from the interface. I used the word "gets" rather than "inherits" there, but this looks a lot like inheriting the default method.
This is basically multiple-inheritance "light" (as in "light beer"). It does an end-run around the thornier problems with true multiple inheritance, like:
What should the type of super be? (Java 8's answer: ClassA)
What order do you run constructors in? (Java 8's answer: Single-lineage constructor chaining, interfaces don't have constructors.)
Do you run constructors that you inherit more than once, more than once? (Java 8's answer: You can't inherit constructors more than once, interfaces don't have them.)
What happens if you inherit multiple methods with the same signature? (Java 8's answer: If one of them is from the base class, that's the one that's used; a base class's implementation can override the default method of multiple interfaces. If you have multiple default methods with the same signature from different interfaces at compile-time, it's a compile-time error. If an interface has been changed without the class being recompiled and the situation arises at runtime, it's a runtime IncompatibleClassChangeError exception listing the conflicting default methods.)
you are correct
First of all, Object class is the super/base/parent class of every class including user-defined classes.
So even if we don't mention it explicitly, the user-defined classes extends Object class by default.
its like
class A
class B extends A
but compiler read it as
class A extends Object
class B extends A
proved
for more detail check this java documentation for inheritance
My answer is correct?
You are absolutely correct in saying that it is multi-level inheritance and not multiple inheritance.
Only the root of the hierarchy is Object, all classes don't individually extend Object.
A counter to the interviewer:
If all classes extend Object, then how many times constructor of Object will be called on A a = new A();
The answer is only once, and that will be for the root of the hierarchy.
Multiple inheritance and class Object
Yes, you are correct... as many others have pointed out. I just wanted to say that interviews are not only about technical knowledge, it is also about sticking to your guns. Some interviewers will question your answer, not because they want to know if you are sure of your convictions but also to test how well you can teach others and how well you handle an authoritative figure.
For the first point, if you can't teach others then you can't be a mentor. Nowadays it is crucial to hire someone who can coach junior developers.... because it makes sense economically.
For the second point, because they don't want you changing technical aspects just because your boss asked you to. If your boss asks you to remove all indexes from the database because they take up too much space, would you do it? Would you try to convince your boss otherwise? How?
Does java support multiple inheritance?
Yes for interfaces but not for classes.
The class and interface can implements many interfaces but extends only one class
Your answer is correct !
class Object //for illustration purpose
{
}
class B
{
}
class A extends B
{
}
When you create an object of class A, constructor chaining happens.
i.e. the constructor of class A calls super() implicitly and hence the constructor of class B is invoked, which then calls its super class implicitly which is the Object class.
In java, a class extends only a single class because the constructor of that class only call one super class constructor. This is not true in case of Interfaces since they do not have constructors.
Also when an object of class A is created, and assume that you have defined the constructors of both classes A and B, then constructor of class B is executed first and then the constructor of class A.
Your answer is perfectly alright. You can explain interms of multilevel inheritance support from Object class in java
Your answer is right, because java doesn't support multiple inheritance from classes. Java supports multiple inheritance from interfaces, and there is no any other inheritance. But you can use composition of classes, but that's another story.
What a dumb question.
Of course Java doesn't support multiple inheritance, and interfaces are not inherited.
Inheritance only happens via "extends", not via "implements". When you define a class implements several interfaces you are not saying it will be an extension of those interfaces, but it will have the same behavior, and behavior (at least in Java), doesn't define inheritance.
For Java to support multiple inheritance, it would need to support something like
public class MI extends Object, MyOtherClass
Which Java can't.
Well, maybe I wouldn't get the job for calling the interviewer's question dumb :)
Your answer is absolutely correct.
These types of questions asked just to check whether a candidate is conceptually strong or not.
Well the simplest and precise answer to this question is here:
"Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object."
Please refer this link
https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
The answer you gave is correct. The interviewer was wrong:
Internal process
if suppose Class A Doesn't extends any other class
then ---> Class B extends java.lang.Object
then ---> Class A extends B
then class A also inherited the property of java 'Object' class...
so,Java doesn't support multiple inheritance.
If you want to verify this process just generate 'javadoc' for your class A and verify the results.
Can we say that multiple inheritance in Java is made possible by implementing interfaces?
With default methods in interfaces in java8 we will have multiple inheritance :)
No. An interface defines how an implementation should communicate with the outside world you do not define any behavior. You can of course implement multiple interfaces but that doesn't mean that you have multiple inheritance, just that the class implementing the interfaces can appear as different things.
No. You aren't really inheriting anything. You are specifying behavior.
It's not really "multiple-inheritance", you are simply outlining what the class should be able to do.
About as close a "multiple-inheritance" goes I suppose would be interfaces extending interfaces, really.
Nope. When you implement an interface, you are simply making a "promise" to implement certain methods in said class. When you extend another class, you inherit its methods and instance variables. Two completely separate things.
I'm using an interface in java, that communicates with PureData. In order to do so, my classes have to extend a given class MaxObject. While designing my class, which is a cirular buffer, I discovered that I need to extend java's Iterator class. So I have to extend two classes at the same time.
My guess is that the only solution is to create two different classes and let one of them be a component of the other one. But, is it the only solution? Is it the best one?
Further, whenever I find myself needing inherit from two classes, is it a because of a bad design? Is there a design pattern to solve this class?
Thank you
Iterator is not a class, it's an interface. As such, you don't extend it, you implement it. You can implement any number of interfaces - the only limitation is that you can only extend one class.
In your case:
class MyClass extends MaxObject implements Iterator<Type>
edit: I should have read closer what's being extended. EboMike is right, you don't need to extend the Iterator class.
Sounds like the DDofD: http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html
Iterator is an interface. From a theoretical point of view there's nothing against extending MaxObject and implementing Iterator.
Due to a lack of information I cannot say if it's a good idea to do this, but I have a bad feeling.