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

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.

Related

when abstract class doesn't contain any abstract method [duplicate]

This question already has answers here:
Why use an abstract class without abstract methods?
(11 answers)
Closed 7 years ago.
I am still confused when abstract class doesn't contain any abstract method, what a purpose of it? why don't use regular class rather than abstract class if it doesn't contain any abstract method ? In fact, I was saw this situation is applied on java and libgdx library or perhaps for every library.
So, because this situation, I was thinking is it very important to know why use abstract class without abstract method rather than regular class.
When you make a class abstract (either with or without abstract methods), you are forcing the users of this class to create concrete sub-classes of it, since they can't instantiate it.
A user of an abstract class must create a concrete derived class.
This can be useful since it allows the author of an abstract class to introduce abstract functions at a later date. The amount of refactoring necessary at that time is then significantly reduced.

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

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.

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