This question already has answers here:
Multiple Inheritance in java
(8 answers)
Java inheritance
(6 answers)
Closed 8 years ago.
well,
I am currently developing an application which uses JFrame and Applet.
Why we can't extend both..
public class myClass extends Applet, javax.swing.JFrame {...}
//invalid...
The valid code is.
public class myClass extends Applet {
javax.swing.JFrame frame = new javax.swing.JFrame();
public void init(){
frame.setSize(300, 400);
frame.setVisible(true);
}
}
Why so?
Why we can't extends more than one class
The designers of Java learned from the mistakes made in other languages such as C++ where the diamond problem was an issue caused by multiple inheritance so decided to make Java a single inheritance language to simplify development.
This is how Java works. It just doesn't support multiply inheritance. There are many reasons why multiply inheritance may be dangerous. For example there can be a name conflict.
On the other hand you can implement as many interfaces as you want to.
When a class is abstract or extended, you are specifying the role that the class plays. This also helps to prevent looping inheritence.
An interface is merely a contract that you will have provided method functionality in your class. If you want to inherit from multiple sources that don't specify the role of the class, use an interface.
Just like what Jakub said, multiple inheritance is not supported in java so you will have to use interfaces instead. It's just the nature of the language. I do hope that's what you're asking.
Java does not allowed multiple inheritance nativelly but you can simulate the "multi-inheritance" like that see.
Or you can use Java8 which allow you to use specific interface in which you can implement ONE method.
Multiple inheritance is not supported in java, to get around this the concept of Interfaces is used. You can inherit from as many interfaces you like to achieve multiple inheritance.
Related
While i was working on my assignment on Java, I created 3 classes, lets say class 1, class2 and class3
Then I tried to define one of the classes in the following way:
public class class1 extends class2 extends class3{
To be honest, I was almost sure it would not work and in fact did not.
So I wanted to know If I could create a single subclass for two different super classes. Thank You!
What you are asking for is multiple inheritance. Java does not support this.
Multiple inheritance is hard for compiler writers to implement - mainly because of the diamond problem.
Except - in Java 8 there are interfaces with default methods. This allows sort of a light-weight version of multiple inheritance.
No you can't. In Java you can extend only one base class. You can however implement from multiple interfaces
For java,no.
in "Tink in java"1.8 Single Root extends structure has mentioned that.
but in C++,yes.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
WHY an Anonymous class in Java can't implement multiple interfaces directly? Simply because of syntax or there is another reason?
Hi all I was wondering why is it that Java anonymous classes couldn't implement more than one interface?
Like what problems will we have if the Java designers allowed anonymous classes to implement more than one interface?
As such:
IMammal, I4legged anonymous_creature = new IMammal, I4legged() {
{
//..
}
};
anonymous_creature.FourLeggedStuff();
anonymous_creature.MammalStuff();
Like what problems will we have if the Java designers allowed anonymous classes to implement more than one interface?
In Javas type-system there is precisely one static type for each expression. If you had to choose one static type for anonymous_creature you wouldn't be able to make much use of the variable, which is probably why you wrote
IMammal, I4legged anonymous_creature =
^^^^^^^^^^^^^^^^^
which actually changes Javas type-system fundamentally. (Possibly it has been excluded for the same reason as multiple inheritance, namely in order to keep the language simple.)
Besides, there is a trivial workaround, and that is to introduce a auxiliary interface extending both of them:
interface FourLeggedMammal extends IMammal, I4Legged {
}
and then do
... new FourLeggedMammal() { ... }
You can via an abstract class:
public abstract class AFourLeggedMammal implements IMammal, I4legged {
}
then in your code, you can do:
AFourLeggedMammal dog = new AFourLeggedMammal() {
}
dog.FourLeggedStuff();
dog.MammalStuff();
I can't see any technical problem. But anonymous inner classes should be small. Typically implementing a single method. If you want to implement more then a single interface you are probably better of with a top level class.
Of course if you absolutely have to you can create an interface that combines all the interfaces you want to implement and then create an anonymous class for that. Of course the new interface needs a name ...
http://books.google.it/books?id=G4ridwFSpIoC&pg=PA472&lpg=PA472#v=onepage&q&f=false
Paragraph that starts with "One more thing...".
An anonymous class can only implement one interface. Why? It's simply a language design choice. Nothing would make it technically impossible.
If you want to implement two or more interfaces you will have to make it a named class or use an intermediate interface or abstract class which extends (interface) or implements (abstract class) two or more other interfaces. Also, you can only refer to it only by one interface name, not two, just like all other objects in Java.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Interface vs Abstract Class (general OO)
I am little bit familiar with the terms Abstract class and the interface.
But i want to know in which situation i have to use the interface and in which condition the abstract class.
Thanks
Interface vs Abstract Class should be a useful read.
In short, Abstract Classes are meant to be extended, as in you're giving someone a base to work off of. Interfaces ensure that things have a common way of interacting with one another without having to worry about the inside details.
Simple answer: you can implements many interfaces, but can only inherit from one class, so if you want to inherit some logic, you should use Abstract Class, otherwise Interface is more extensible.
This question already has answers here:
Abstract class vs Interface in Java
(15 answers)
Closed 5 years ago.
In interview I have been asked following question. I tried to answer the question but I want exact answer of the question.
If I can simulate Abstract class as Interface, why java provided Interface?
This mean if in Abstract class I can mark all methods as abstract and then abstract class will work as interface, so why I need interface.
Can anyone explain me in brief.
That's a very standard interview question. The answer is: because you can implement multiple interfaces, but can't extend multiple abstract classes.
Example from the JRE: LinkedList is both a List and a Deque. These interfaces define the behaviour of the class. They do not provide any implementation details. While abstract classes could provide some.
Related questions: this and this. The latter is not directly related, but it shows why are interfaces needed, even in cases when an abstract class would suffice.
Interfaces define contracts & can define constants, but provide no implementation at all of the contracted methods.
Abstract classes can provide implementations of methods as well as member variables - if you want you can create an abstract class that defines everything except the fine-tuning you want in your concrete subclasses. You can't do this with interfaces, but you can implement multiple interfaces & extend only one parent class.
Both interfaces & abstract classes can be used to make use of concrete classes polymorphically.
Abstract classes do well to set default methods and set up the hierarchy. The issue is subclasses may only extend a superclass one-time. Interfaces on the other hand can extend each other multiple times and subclasses can implement any number of interfaces. This provides a lot of flexibility and affords the potential for change. Ideally, the can be combined i.e. abstract class implements interface1…interface2, best of both worlds.
The reason why interviewers ask this question is because your answer reflects your deep understanding of what a programming language (and a compiler) is. In particular, Java defines the concept of interface on top of (pure) abstract classes in order to (partially) support multiple inheritance (between interfaces). If this mechanism had not been introduced, we would have either no way of achieving some sort of multiple inheritance, or the big mess created by fully-fledged multiple inheritance in C++.
Answer
1) MULTIPLE INHERITANCE in java is achieved through interfaces.
2) If there is a situation where some explanation of a method is required, but not a full fledged one, the best way is to use abstract class.
3)Interfaces merely provide an AGREEMENT for the return type and the argument types.
This question already has answers here:
Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
(21 answers)
Closed 9 years ago.
I can make abstract class equivalent to interface by putting all abstract methods within abstract class. Why did Java's designers choose to provide support for interfaces and single inheritance instead of multiple inheritance and abstract classes. What is the advantage?
At least for one reason (besides the conceptual differences between the two): you can implement multiple interfaces but you can only inherit from a single abstract class at most.
Class-based multiple inheritance is not supported by Java. You can only inherit from a single class, but you can implement multiple interfaces.
This becomes handy when you need to treat multiple classes polymorphicly when they have different inheritance trees.
An interface is a contract between a class and its behavior. If a class implements an interface, it MUST provide implementation for the methods specified in the interface. There's a number of things that differentiate the two, however.
Check out the Oracle website for more on the differences between interfaces and abstract classes:
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html
If you have multiple classes and you want to force that there would be some methods that should be common among them, irrespective of their behavior(implementation), Interface does that for you
Interface, is like a contract, which every other class which implements it has to follow