An abstract class is the same thing as an interface except it is an actual class, not just a contract.
Why interface is refered to as a contract ?
An abstract class is the same thing as an interface except it is an
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Both interface and an abstract class are contracts, since they bind you.
But there other differences between an interface and an abstract class.
Like you said, if you need an actual class, with content to the functions or actual data members with default values, or a ctor, then obviously you will need an abstract class.
But many times, the choose between an interface or an abstract class will be technically the same. And sometimes even in the long term, it will be the same.
Your decision then should be based on the nature of what you are looking.
Is it an extra character to your data types? Or does it define what your data type is?
I’ve tried to think of any real world example, but I couldn’t find any as I haven’t done anything with the subject for years now, so I’ll give you a book-like example.
Say we have an abstract class Animal:
This could have been easily an interface.
And choosing it to be an interface will actually won’t have any negative effect on your design.
But in the matter of the nature of it, it must be an abstract class, since
class Dogis an animal. It must beclass Dog : Animalrather thanclass Dog : IAnimal.Being an animal is not an extra character of
Dog. It what defines it.And you want the restrict all the animals to inherit only from
Animal.IAnimallet someone inherit something else. like:Interestingly enough, sometimes one might write an abstract class with nothing at all, just for the sake of the same thing.
For example, if there was no common property for any
Animalyou will still prefer to do: