Reputation: 305
In an interview it was asked to me to justify when to choose interface and when to choose abstract classes and in which conditions you will choose out of the two only one.. I have come up with my analysis for interface and that is...
Interface is best choice for Type declaration or defining contract between multiple parties. If multiple programmers are working in different modules of a project they still use each others API by defining interface and not waiting for actual implementation to be ready.
This brings us a lot of flexibility and speed in terms of coding and development. Use of Interface also ensures best practices like "programming for interfaces than implementation" and results in more flexible and maintainable code.
But I don't have strong reasons to justify the abstract classes, Please advise..!
Upvotes: 4
Views: 5637
Reputation: 107
you'd want to use an abstract class when you want your classes to only implement the methods if the situation would call for them.
an example would be if you had a super class vehicle and you wanted your sub-classes to give you number of wheels if they have them. You wouldn't need them if they were boats so the contract would force you to implement still while you could just out right ignore it for your abstract class.
Upvotes: 0
Reputation: 159754
Abstract classes can have default behaviour(implementation) if is required, interfaces cannot.
An abstract class can provide default behaviour for ALL methods or no methods whereas interfaces cannot.
Abstract classes can have state shared with all subclasses, interfaces don't specify state.
You can implement multiple interfaces, you can only extend one (abstract) class.
Upvotes: 2
Reputation: 33534
1. If speaking generally, An Abstract class
will be needed when we need to force down some features on the Sub-Class
from Super-Class
, letting the Sub-Class to have the flexibility to add other functinality.
Eg:
Let Car be the Abstract Super-Class, which has 4 Tyres, 1 Steering ,etc...
Now the Sub classes like Santro i10, Maruti-800, Mahindr Bolero etc are Sub classes,
but they need to have 4 Tyres, 1 Steering to be called a car, not they can have a radio
or not as an additional feature.
2. Interface
was introduced in java, because there is no Multiple Inheritance.
3. Interface
is more about providing a role
.
Eg:
Let Dog be the Super-Class
.
Wild Dogs and Pet Dogs are Sub-Classes.
Wild Behavior and Pet Behavior are Interfaces
Now as both Wild Dog and Pet Dog are dogs, but with different behavious. Then they must
implement the Wild Behavior or Pet Behavior respectively
Upvotes: 1
Reputation: 8708
Abstract classes are used to group a number of concrete classes under one entity.
For example, take the abstract class Animal
.
Animal is not something concrete. it's a family of, well, animals. but they all share certain aspectes, for example, each has a speak()
option (well, except fish and sort). but each one implements it differently. this way you can override just the methods which are not the same, for example sleep() or breath()
are common (again, fish are differnet :) ).
Interfaces on the other hand are more direct definition of an 'action'. That's why most (if not all) the interfaces in Java ends with 'able' (Comprable, Serializable...) By implementing the interface, you're telling other programmers or who ever uses your code that this class can do this and this. A dog, for example, is not, Animable.
Basically, to sum it up, I think that the best definition is this.
Use abstract classes when you have a class that A is kind of B
and interface when A can do B
.
Hope that's help.
Upvotes: 29
Reputation: 5679
You are correct on your definition of interfaces.
Abstract classes should serve a completely different purpose. As an example, let's say you are implementing several classes for an animal simulator. 'Animal' defines several behaviours that could already have a basic implementation, but is not itself something that would make sense to have instantiated. Likewise for 'Mamal', a bubclass of 'Animal'. Only a subclass 'Tiger' would not be abstract, but most of what a tiger does that's not tiger specific would be implemented in it's abstract superclasses.
Upvotes: 1
Reputation: 10540
An interface declares a contract with whatever implements it. It's a guarantee that the class will contain the methods in the interface.
An abstract class is similar in that anything that subclasses it will also have to implement the abstract methods, but you can also have working methods with code in them.
I use interfaces a lot for callbacks with Android programming. Abstract classes I use a lot if I have a lot of similar data to display and just want small changes to implementation. Use an abstract class to cut down on repeated code while still having different implementation.
Upvotes: 1
Reputation: 9701
Abstract classes are used when you want to provide partial implementation.
Upvotes: 5