This question already has answers here:
Can an interface extend multiple interfaces in Java?
(7 answers)
Closed 5 years ago.
i work in GWT. The Devmode class in gwt has an interface in it called 'HostedModeOptions'. But HostedModeOptions extends more than 1 class like :
protected interface HostedModeOptions extends HostedModeBaseOptions, CompilerOptions {
ServletContainerLauncher getServletContainerLauncher();
String getServletContainerLauncherArgs();
void setServletContainerLauncher(ServletContainerLauncher scl);
void setServletContainerLauncherArgs(String args);
}
How is this possible in java 7 ? As far as i know we cannot extend more than 1 class at a time in java 7.
Can anyone explain please .
You cannot extend other more than one class however you can implement many interfaces. If you are developing an interfacing you can make use of other interface and can extend many other interfaces.
They are all interfaces and not classes.. An interface can extends many interfaces. This facility is there from very first version of Java.
If you have any doubts regarding ambiguity, below link might help a bit
Can an interface extend multiple interfaces in Java?
Related
This question already has answers here:
Java Multiple Inheritance
(17 answers)
Closed 2 years ago.
Please explain how to extend two classes from different packages.
public class Animal extends Herbivores extends Omnivores {
}
Multiple Inheritance or Diamond Inheritance is a feature of object oriented concept, where a class can inherit properties of more than one parent class.
Java does not support that.
There are multiple resources over the net which tells you the problem that arises if you are allowed to do that.
Coming to your problem, it seems to me like a fault in Design.
Animal should be a parent type rather than a child type. Both Herbivores and Omnivores are animals. Whereas an animal doen't necessary have to be either. It can be a carnivore also.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When shall we go for interface or abstract class in Java?
I have a doubt in java.I know about the interfaces and abstract classes.But I want to know specifically when to use interface and when to use abstract classes in java and android.I want a practical explanation with real world example not a theoretical or documented one.
Thanks.
The key difference is that you can implement multiple interfaces in a
class, but only extend a single abstract class.
Interface is used when you only want to declare which methods and members a class MUST have. Anyone implementing the interface will have to declare and implement the methods listed by the interface.
If you also want to have a default implementation, use abstract class. Any class extending the abstract class will have to implement only its abstract methods and members, and will have some default implementation of the other methods of the abstract class, which you may override or not.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Interface vs Abstract Class (general OO)
When to use abstract class or interface?
Can you provided implementations on a abstract class?
what's the difference between these two?
and when will I know when will I know to use them?
This pages gives a good comparison: http://download.oracle.com/javase/tutorial/java/IandI/abstract.html . You could have found it with a very quick google search.
Interface is used for defining a contract. Abstract classes are used for defining some methods which are common to all descendants and possibly some methods which will differ(they will be abstract). With interfaces or abstract classes polymorphism is reached.
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.