This question already has answers here:
When to use an interface instead of an abstract class and vice versa?
(26 answers)
Abstract class vs Interface in Java
(15 answers)
Closed 8 years ago.
I understand the technical difference between the two, but I don't understand why one is better to use than the others? Can anyone give me an example that would help me distinguish an advantage one has over the other?
For example: If I was making an rpg game, and I was working on some healing items. What would the uml diagram look like. Would I be using an interface for items, and then healing items or abstract classes.
When to use interfaces
Interfaces are basically used to inherit the behaviour, as we know interfaces can only have abstract methods. When a class implements that interfaces it has to implement all its abstract methods (unless the class itself is an Abstract class), thus it behaves according to the way interfaces defines.
For example, if you want your class to behave as a Thread. You need to implement Runnable interface and override its run() method.
We need to inherit behaviour of two or more forms.
Interfaces are basically created when we know what to do, but we don't know how to do it. To explain in simpler terms I would say, suppose there are 5 tasks which you know must be done. But there can be different ways to do them. Any class implementing them can do them in any way they wish to. So create an interface and put your methods in them.
When to use Abstract class
We know abstract classes can have both abstract as well as concrete methods.
So same example, if you have 5 tasks to be done but for one task there is one default way to do, so you will wish to define its body and you won't be able to do so in interfaces.
In this case we'll create an abstract class. Put 4 abstract methods and one concrete method, which will be inherited by its child classes directly and they won't need to define it on their own. However if they wish they can override its functionality.
Also keep in mind, a class can extend only one abstract class at a time.
Related
This question already has answers here:
Java Multiple Inheritance
(17 answers)
Closed 2 years ago.
Java does not allow multiple superclasses; and interfaces cannot have method bodies as I know. So how to handle the case in which I want to inherit two implemented methods of different superclasses.
Suppose that I create classes for objects which can do some specific tasks, let's say objects can play and sing. Some objects only play and their subclasses as well, so there is no need to define the exact same play() method for each subclass, I define it in superclass Player. Some objects only sing, so, I define the generic sing() method in superclass Singer, not in each subclass. What to do, if a subclass can sing and play, so need to extend Player and Singer superclasses to be able to apply implemented play() and sing() methods of superclasses. How to handle that kind of situation, in the best efficient way?
Starting with Java 8 interfaces can have default methods. These methods are marked with the default keyword and can have an implementation in the interface, that can then be used in (or reimplemented by) classes implementing the interface. Classes can also implement multiple interfaces, so you could have a class implement both Player and Singer and provide default methods play() and sing() in their respective interfaces. This is also useful if you want to modify an interface without having to also modify the classes that already implement it.
This question already has answers here:
Abstract class vs Interface in Java
(15 answers)
Closed 9 years ago.
In abstract class we can make all methods abstract so that it can work like an interface, so why to use interface at all?? One of the reason I could come up was that we can implement multiple interface not extend multiple class.. Is there any design or performance related thing involved??
You already got the answer. Using Interfaces we can enforce multiple types of behaviours where as using classes will not work for you. For example, you can enforce a class to be IComparable as well as INumerable however it is not possible if you want to do it with classes.
You've already identified the one thing that interfaces allow that abstract classes don't allow. One class can't extend multiple abstract classes.
Is there any design or performance related thing involved??
There is no performance difference.
You could argue that the single inheritance restriction of abstract classes (in fact, all classes) makes this "a design thing" though. Certainly it would seriously restrict your use of polymorphism in an OO design.
(You could also argue that you can't follow the maxim of "programming to the interface" when you don't have interfaces. However, that is a weak argument ... a terminological quibble.)
Design wise it is preferred guidelines to use Interface for you code behavior/contract/functionality definition (see List interface) and use Abstract class where you have atlease some reusable (via inheritance) method implementation .
Though having all abstract method is possible, but in such cases an Interface is preferred.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Alternative of Multiple inheritance in Java
Multiple inheritance in Java?
i have a code like so:
public class myApplet extends JApplet
im interested in adding another extend like so:
public class myApplet extends JApplet, extends Object implements Serializable
can this be done some way and how?
Java doesn't have multiple inheritance, and any class extends Object anyway, so it's useless in that case.
you can not extend more than one class in Java. But you may implement more than one interfaces.
However, if your parent is a child from another parent you are of course also extending all classes in the hierarchy.
If you do not add a extend-statement, Object is automatically choosen as parent. Object is the super-parent, and in every hierarchy you will find it. Therefore in your case you can skip the "extends Object" as the hirachy is the following:
java.lang.Object
extended by java.awt.Component
extended by java.awt.Container
extended by java.awt.Panel
extended by java.applet.Applet
extended by javax.swing.JApplet
extended by your.package.myApplet
(see http://docs.oracle.com/javase/6/docs/api/javax/swing/JApplet.html)
Java doesn't have multiple inheritance, but you can implement multiple interfaces.
public class myApplet extends JApplet implements Serializable, SomeThingElse
You don't need to extend Object since all classes will extend it.
If you're in the situation where you have methods in two classes you'd like to share in a single class, consider defining a third class which uses these two classes if the functions sets are cohesive and perform different operations. Don't try to combine classes to gain their powers. Create separate classes which tackle different problems and store instances of these in higher order classes.
If you do have two classes which perform similar functionality, it may be possible to abstract their common code into a superclass. So Class A would extend Class B, and you would in turn extend class C and add more specifics there.
If you have two classes which are very similar in structure but operate on different data structures or algorithms, then you might consider creating an interface which they both implement. A good example of this is a Vehicle interface which has a drive() method. A Car will have a different implementation to drive() than a Motorbike.
To extend this last idea, if these classes will share common code and it's possible to say "A Car is a Vehicle" and "A Motorbike is a Vehicle", then it's likely that using inheritance is preferred to an interface, with Car and Motorbike both extending Vehicle and it's abstracted functions.
You can't extend class twice in java... Nevertheless every class in java inherits from class Object instantly, so you don't have to extend your class with Object class ;)
No, Java doesn't support multiple inheritance. Every class inherits from Object automatically though.
Java doesn't support multiple inheritance,you can use multiple interface
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When to use abstract class or interface?
I am a newbie in Java , can anyone please explain a scenario where abstract class will be
useful and interface will not be and vice versa.
I believe in not so complex problems both can solve the issue with equal ease.
Please explain in layman's term and pardon my ignorance!
When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.
When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Abstract class and Interface class?
When i need to use abstract class and interface in Java?
My doubt is
Which situation i need to use abstract class and which situation i need to use interface.
interface satisfy the abstract class properties. so then why we need especially abstract class?
i know, that abstract class contains abstract methods and non abstract methods, but we can use abstract class as a ordinary class, then the result will be same in the both classes. the ordinary class also inherited same as abstract class. So why we need abstract class.
If anybody know the good example please reply me.
Thanks.
Another important aspect of abstract class is that unlike interface, adding new methods to it won't break binary compatibility. So, from the API evolution point of view, especially when you can expect additions to the public API, abstract classes are more preferable.
I would say that there are two types of inheritance. One I would say as Implementation Inheritance and other as Contract Inheritance.
Abstract classes are used for having Implementation Inheritance. You can extend/change the behavior of your super/parent class.
Interfaces would go for Contract Inheritance. Where you are more interested in having the class implement some kind of a contract (service methods with arguments - more of a contract) and the behavior is different for different implementation, nothing generic that you can bundle up in an abstract class and extend the behavior.
You need to use abstract classes if you want to apply the template pattern http://en.wikipedia.org/wiki/Template_method_pattern, usually in framework code. As a rule of thumb, if you're not intending to implement the template pattern, you're better off with interfaces, which permit loose coupling, the way spring framework does. Loose coupling leads to a design open to evolutions and a better testability, with techniques like mock objects (http://easymock.org)