What's different between abstract class and interface?
There is one day. I was asked this question by some one. And I was shaken because this is a simple question but I can not used a simple word to interpret it. I have been touching OOP and writing Java several years. I also can use abstract class and interface correctly. But I never think about this question in mind. I went back home and asked this question to myself again and again. Finally, I found an easy understand description. :)
Abstract class is a template, and interface is an object's definition.
I believe every body know what template is. Like a table that you used to apply for something. There are many blanks in table, and you just need to fill in corresponding data for each blank. Abstract class is the same. The blank in abstract class would be a method that need to implement it.
What is object's definition? We have a initial ideal to create one thing, and we know this thing can separated into several parts. These parts may have different behavior depend on different need. So we can define a definition for this thing. Let producer implement it depend on their need. Even different need would produce different thing, but they are in common. Because all they can separated into the same parts. In other word, we can say define a definition is very similar to define a specification. Let different producer to obey one rule and their production can apply to everywhere.
Abstract class is a template, and interface is an object's definition.
I believe every body know what template is. Like a table that you used to apply for something. There are many blanks in table, and you just need to fill in corresponding data for each blank. Abstract class is the same. The blank in abstract class would be a method that need to implement it.
What is object's definition? We have a initial ideal to create one thing, and we know this thing can separated into several parts. These parts may have different behavior depend on different need. So we can define a definition for this thing. Let producer implement it depend on their need. Even different need would produce different thing, but they are in common. Because all they can separated into the same parts. In other word, we can say define a definition is very similar to define a specification. Let different producer to obey one rule and their production can apply to everywhere.
留言