I have been into C# and always feel confusion between what should be selected between interfaces and abstract classes. Can some one please help resolve this query ?
Thanks ,
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.
Think of an interface like a
contract, you are specifying something that you are expecting the consumers of that interface to implement.An abstract class, on the other hand, is useful when you have some of the code you need to implement for the class, but not all of it. And you can declare abstract methods for the parts that need to be implemented by the subclasses of the abstract class. Remember that abstract methods must be implemented by the subclasses, but you can also provide your own code within the class itself via normal private/public/protected/etc. methods.
So if you are just writing a contract that you want subclasses to implement, then think interface. But if you are writing something that’s more of a “pattern”, where you might have some of the method implementations (but not all) that will be common to all the child implementations, then you should think abstract class.