What should I use Interface or abstract class? [duplicate] - java

This question already has answers here:
Interface vs Abstract Class (general OO)
(36 answers)
Closed 9 years ago.
After searching through google and reading a lot of stuff , I conclude that I may able to decide when to use Interface and when to use abstract class except
If all the methods are abstract and public and in future no need to add any method.
So I want to know what option (Interface or Abstract Class) I use if the above condition arises.

Java doesn't support multiple inheritance so we can only extends one class, there for it is better to use interfaces. But depending on a situation this can be differ. My opinion as a best practice, interface better than abstract class most of the time.

You want to use an interface if you have to define a set of common behaviour on different entities.
You use an abstract class if you want to differ between related entities that share common functionality. The abstract class can then hold the common functionality and define the abstract methods that should be used by subclasses to define their specific behaviour.
Keep in mind though that a class can only extend one other class, but it can inherit from multiple interfaces.

If all the methods are abstract and public and in future no need to add any
method
In Interfaces all functions are by default public and abstract whereas an abstract class can have non abstract methods. So you have your answer!

Abstract classes are best used when they carry behaviour with them in form of implemented methods which´s functionality is usefull for child classes.
In your case, you should go for an interface.

Related

When to use Interface and when to use Abstract class [duplicate]

This question already has answers here:
When to use an interface instead of an abstract class and vice versa?
(26 answers)
Closed 8 years ago.
Folks can you explain me in which condition I should go with Interface and at which condition should I prefer Abstract class ... ? keep in mind I am not asking difference between Interface and Abstract class.
If you found any Is a relationships between objects you can use abstract
for example: Bird, Aeroplane, Paper Rocket these all are flyable but there is no any Is a relationship between these objects, so you can use here interface instead of abstract,
Because Bird,Aeroplane and Paper Rocket all are flyable but the way of flying is different.
And off course lots of differences are on Google.
Use Abstract class when : You have some common functionality (method) which must be implemented at only one place and other concrete classes can just use it.
Use Interface when : There is no common functionality. Every concrete class has its own implementation of functionality.
I would suggest to stick to the following rules:
use an abstract class only, if you need code-reuse
restrict the visibility to the package the abstract class is defined
Otherwise use an interface and delegation. But as it is with software design you have to make the proper decision for each concrete situation. If you have restriction / rules which must be applied but need some flexibility, think about using the strategy-pattern in preference to inheritance.
The problem with abstract classes is that you can't inherit from more then one. Hence, if you need a type which is of type A and B and both are abstract classes, how do you achieve this? Interfaces are open to be implemented by any other type without restrictions.
We must preferred first to inferface. If we have to write some common functionality in that class then and only then you can go for abstract class. Otherwise use interface. Because We can implement number of interface but we can extend only one class so for future you must go for interface rather than abstract class.

When to use interface and abstact classes in java and android? [duplicate]

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.

Abstract class or interface Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Interface or abstract class?
Abstract and Interface in java
I am still a student in Java and i came across abstract class and interface.
now when you create an abstract class you write methods like this:
public abstract void something();
but when you create a method in your interface it looks like this:
public void something();
Now im not blind i can see that there is some difference also i do know that you extend an abstract class and you implement an interface.
But can someone tell me what the difference is maybe an example of where you would use one over the other?
There's no difference between those two routines. The difference lies in that abstract classes can contain common logic used for all implementations while interfaces cannot.
"Abstract" basically means that the method has no implementation. The implementation has to be provided by subclasses. As a consequence, one cannot create instances of classes having abstract methods.
Now interfaces in Java are just a collection of method signatures. They cannot contain implementations by design. Thus, the abstract keyword would be redundant. One cannot create instances of interfaces, only of concrete classes that implement the interface.

When shall we go for interface or abstract class in Java? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When to use an interface instead of an abstract class and vice versa?
Can any one tell me under which circumstances we should go for interface and abstract class.
Java specific aspects are welcome.
Always use an interface unless you need to ...
... provide subclasses with some state
... provide subclasses with default implementations of some methods
... want to defer implementation of some of the abstract methods
Note that you can only extend one class, while you can implement multiple interfaces. So if there's any chance that a subclass will need to extend some other class, strive for using an interface.
Here are some good links that discusses this topic:
Java World: Abstract classes vs. interfaces
Interface vs Abstract Class (general OO)
Abstract Class versus Interface
Mindprod: Interfaces vs Abstract classes
In simple Language :
Use interface if you want your objects be accessed by common way.
Use abstract class if you want to define some functionality in super class and to define prototype of some methods that must be override in child classes i.e., extending the functionality of a class.
Here is a funny example that might help you to clear the fundamentals.
http://ganeshtiwaridotcomdotnp.blogspot.com/2011/05/understanding-importance-of-interface.html
If you don't want to implement any method and you just want to define your contract, then you use an interface.
However, if you do want to have some implementation already, you should use an abstract class.
You will use an abstract class if you want to provide a partial implementation for the subclasses to extend, and an interface if you only want to provide signatures of methods that must be implemented.
It is perfectly normal to provide both and interface and an abstract class that implements parts of it.
There is however one limitation of abstract classes: In a subclass you can only extend one (abstract) class, but you may implement as many interfaces as you like in a single class.
Interfaces are simply a collection of public method signatures and public static final fields. No constructors, no protected/internal methods, no other type of fields.
On the other hand, any class can be abstract simply by putting abstract in front of its declaration. They can declare abstract methods and implement interfaces and other abstract classes without defining the method implementation.
An abstract class is more restrictive when it comes to inheritance (only one can father a subclass), but you can implement methods and constructors in it.
Any number of interfaces can be implemented by a class, but there is no default method & constructor implementation.
That is why it is always a good idea to provide an abstract class next to an interface as a default implementation option.
Check these
http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html
Abstract class and interface

When to use an abstract class? An interface? [duplicate]

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.

Categories