This question already has answers here:
Do/can abstract classes replace interfaces? [duplicate]
(11 answers)
what is the actual use of interface in java? [duplicate]
(6 answers)
Closed 7 years ago.
In java there is interface which having only abstract method means method declration only and those methods are implement in class that implement interface so why to use interface which has only declaration .This question is asked in interview.
Interfaces are used for only declaration of methods because it can achieve multiple behavior. It is the class's responsibility to implement the interfaces and define their methods as they desire.
Interfaces allow same methods to be implemented in different manner by implementing classes, which allows you to achieve behavioral facility.
For understanding sake, you can consider interfaces exist so you can achieve behavioral approach for your application, and class exist to achieve characteristic approach.
Related
This question already has answers here:
Is there more to an interface than having the correct methods
(17 answers)
Closed 1 year ago.
So, you create an interface and define some methods. Then if you implement the interface to a class you must override all of the methods of the interface. Then what is the purpose of an interface in this case, since you are just repeating yourself, rewriting methods?
Java interfaces help you define rules.
For example, if you had a class for animal, you would want every class implementing animal have a method for movement, and number of limbs, the way they emit sounds etc.
You needn't know beforehand what those methods would be, but you do want to establish those set of rules that make an animal, an animal.
To implement Polymorphism concept.
This question already has answers here:
Protected in Interfaces
(15 answers)
Closed 6 years ago.
Why does java force interface methods to be public and how does abstract class differs from an interface?
A quick response is:
All the interface methods must be overriden, for that they must be public.
Abstract classes can be extended by other classes, the interfaces only can be implemented by classes and inherited by other interfaces.
You can get a lot more information here: http://www.javaworld.com/article/2077421/learn-java/abstract-classes-vs-interfaces.html
This question already has answers here:
What is the use of marker interfaces in Java?
(10 answers)
What is the purpose of marker interface? [duplicate]
(1 answer)
Closed 8 years ago.
Can any one tell the In which situation Marker Interface will usefull?
1.Marker Interface does not have any methods inside interface.If our class implements any marker interface internally jvm will add some capabilites to our class.
2.Some of the marker interfaces are
Serializable,Cloneable,RandomAccess etc
3.Marker interfaces are introduced to reduce the burden on the developers.Internally jvm takes care of logic when we implement markerinterface to add the capability or functionalty to our class
4.We can write our own marker interface only possible with customization of jvm
Uses
1.Marker interfaces are useful to declare the metadata about the class
2.Marker interfaces are useful in the case other code takes decisions depending on whether an object implements some marker interface.
This question already has answers here:
Defining an abstract class without any abstract methods
(12 answers)
Use of an abstract class without any abstract methods
(7 answers)
Closed 9 years ago.
can any one please explain the scenario when A class may be declared abstract even if it has no abstract methods. i have tried in many websites but i did not found it.Thanks a lot.
Broadly, this would be because the class provides concrete implementations of functionality but should not itself be instantiated. For example, an AbstractWidget in a price calculator may not be suitable to directly instantiate, but it has concrete implementations of certain widget functionality like being rearranged. The expectation is that the subclass adds new methods altogether, or overrides the ones already declared.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Interface vs Abstract Class (general OO)
When to use abstract class or interface?
Can you provided implementations on a abstract class?
what's the difference between these two?
and when will I know when will I know to use them?
This pages gives a good comparison: http://download.oracle.com/javase/tutorial/java/IandI/abstract.html . You could have found it with a very quick google search.
Interface is used for defining a contract. Abstract classes are used for defining some methods which are common to all descendants and possibly some methods which will differ(they will be abstract). With interfaces or abstract classes polymorphism is reached.