Hi friends
I want to know what is difference between abstract class and interface in java. I also want to know where we use abstract class and where we use interface in project.
Thank you.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SenthilkumarPosted Apr 17, 2012, 4:52 AM
Difference Between Interface and Abstract Class
1. Abstract class has the constructor, but interface doesn't.
2. Abstract classes can have implementations for some of its members (Methods), but the interface can't have implementation for any of its members.
3. Abstract classes should have subclasses else that will be useless..
4. Interfaces must have implementations by other classes else that will be useless
5. Only an interface can extend another interface, but any class can extend an abstract class..
6. All variable in interfaces are final by default
7. Interfaces provide a form of multiple inheritance. A class can extend only one other class.
8. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
9. A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
10. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.
11. Accessibility modifier(Public/Private/internal) is allowed for abstract class. Interface doesn't allow accessibility modifier
12. An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
13. An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
14. Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
15. Abstract scope is upto derived class.
16. Interface scope is upto any level of its inheritance chain.
For more info:
http://malliktalksjava.in/2010/06/01/difference-between-abstract-class-and-interface/
http://www.java-tips.org/java-se-tips/java.lang/difference-between-abstract-classes-and-inter.html
http://pythonconquerstheuniverse.wordpress.com/2011/05/24/java-abc-vs-interface/
Satyapriya NayakPosted Apr 16, 2012, 10:42 PM
Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods. A Java Interface can contain only method declarations and public static final constants and doesn't contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present
yes it is possible if all methods contain full implementation inside abstract class even though that class can be declare has a abstract class no need to keep at least one unimplemented method in side this class.
eg:
abstract class Allmethodsareimplemented
{
void test()
{
s.o.p("test from abstrct");
}
void test1()
{
s.o.p("test-1 from abstract");
}
void test2()
{
s.o.p("test-2 from abstract");
}
}
public class Manager
{
public static void main(String args[])
{
S.o.p("yes it is possible to keep even if all methods are contained implementation inside abstract then that class called as a abstract one ");
}
}
Note :- Abstract class contains both Abstract and Concrete methods.
http://interview-questions-java.com/abstract-class-interface.htm
http://www.codestyle.org/java/faq-Abstract.shtml#empty-abstract
http://www.javacoffeebreak.com/faq/faq0084.html
Thanks