Why to use interface while we can make all methods abstract? [duplicate] - java

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.

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.

When to use interfaces compared to Abstract classes? [duplicate]

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.

Abstract Class V/s Interface in Design Java [duplicate]

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.

When i need to use abstract class and interface in Java? [duplicate]

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)

When to use abstract class or interface? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Why are abstract or interface classes created, or when should we use abstract or interface classes?
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.
--EDIT - forgot to mention, Earwicker reminded me
Finally, you can implement as many interfaces as you want, but only extend one class (being it abstract or not). Keep that in mind before choosing.
The key difference is that you can implement multiple interfaces in a class, but only extend a single abstract class. This is because an abstract class can also define fields that store data, whereas an interface cannot.
An abstract class is a class, that has atleast one abstract method or you can also make all your methods as abstract. Obviously it cannot be instantiated. You have to inherit from an abstract class and implement the abstract methods in the inheriting class (i.e, the class extending the abstract class).
Interfaces are not classes at all (so don't call them interface class). Interfaces define the signature of methods without any implementation. Also interfaces have no member-fields. If you implement an interface in a class, you have to provide implementations for all the methods provided by the interface.
It makes sense to define a generalized API for some stuff, that can have completely different implementations. Abstract classes are more useful for classes that do mainly the same, but have some subtle differences. You can combine both approaches.
A good example is the collections framework of the Java class-library. You have the interface List, that defines how Lists have to behave. Some implementations are for instance ArrayList and LinkedList. As they behave similar, the stuff that works the same for both is implemented in the abstract class AbstactList, both inherit this.
See Interface is basically a "Contract". When you are defining an interface you are defining a Contract. Where abstract classes are extended, interfaces are Implemented.
Let's consider an example.
public interface Friend {
void hello();
}
Now you have defined a contract which says that any class which wants to implement Friend needs to provide a definition for method hello().
Here is an implementation:
public class myFriend implements Friend {
public void hello()
println("Done");
}
Now myFriend has fulfilled the contract. Now the question is: Where should interfaces be used?
Interfaces help you define a behavior which must be implemented. Say you have a class A which defines some functionality. You want the other classes to use this class functionality only if they define particular behavior (methods). You enforce this restriction in term of interface.
SamuelCarrijo seems to have answered this question well.
In addition for Java, some frameworks require an interface to work with. I'm thinking of (say) dynamic proxies, or some client/server proxying frameworks. This is because they use introspection on the object to determine methods implemented by the interfaces implemented by the object. So occasionally you have to implement an interface for an object where, perhaps, you wouldn't normally bother.
Note this reason for interfaces is specific to Java.
Abstract classes are used when you are building an inheritance hierarchy. However, most inheritance hierarchies should not be too "deep" (i.e. too many levels of inheritance). Many object oriented design books will favor interfaces over inheritance (one book I read once quoted a developer as saying that "inheritance is the single coolest [object oriented] feature you won't implement"), as this allows classes to be assigned behaviors "by contract", where the contract is the interface.
It is worth noting samuelcarrijo's answer - if you want to have a default implementation of a method, you would have to use an abstract class that has a concrete implementation of the method to give it a default implementation. This default implementation can be overridden in child classes.
Hope this helps!

Categories

Resources