Possible Duplicates:
purpose of interface in classes
What is the difference between an interface and abstract class?
Hi I am a php programmer. any body can explain what is the advantage of using interface and abstract class.
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.
The main advantage of an interface is that it allows you to define a protocol to be implemented for an object to have some behavior. For example, you could have a Comparable interface with a compare method for classes to implement, and every class that implements it would have a standardized method for comparison.
Abstract classes allow you to define a common base for several concrete classes. For example, let’s say you wanted to define classes representing animals:
In this case, we define
eat()andsleep()as abstract because different types of animals (e.g. lion, bear, etc.) that will inherit fromAnimaleat and sleep in different ways. But all animals die the same way (don’t hold me to that), so we can define a common function for that. Using an abstract class helped us 1.) declare some common methods that allAnimals should have, and 2.) define common behavior forAnimals. So, when you extendAnimal, you won’t have to rewrite the code fordie().