Question:
In java What is the difference between an interface and an abstract class??
2008-03-05 16:32:48 UTC
I have a programing assigment and it says that i need to create a Interface class with some abstract methods and also an abstract class??? could not I do the same just with an abstract class???? This is a homework about ordering ArrayLists of generic type....

Thank you
Four answers:
Culled
2008-03-05 16:59:11 UTC
The main difference between an interface and an abstract class is what they allow you to do. An abstract class is essentially a class that you can't initialize into an object but you could still write a method and then when you inherit from that class the child class will be able to use the code from your abstract class. Or you could just write a method header and the child class would have to come up with its own implementation of the method. Since Java will only allow a class to be the child of one parent class you cannot write multiple abstract classes and then write one class that is a child of all of them. An interface only allows you to write method headers, you can't write actual code that other classes can use. However you can have any class implement multiple interfaces.
2016-05-26 05:09:43 UTC
An abstract class is a class the has unimplemented methods that must be implemented in a derived class. Abstract classes cannot be instantiated. An interface is not a class: "interface class" doesn't mean anything. An interface specifies a set of methods a class using it -- called implementing in Java -- will define. An interface is a contract between the class and the client specifying a minimum set of methods it implements. For more information, Google is your friend. HTH
question asker
2008-03-05 17:40:35 UTC
Hi I can't beat Culled's answer but I just wanted to add that all methods in an Interface are 'implicitely' abstract. So the use of the abstract modifier in an interface is optional.
2008-03-05 16:47:55 UTC
Yeah you probably could, but I think the purpose of the assignment is to have you create both and have them work together.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...