deepak carter wrote:HI all,
can anyone let me know real time example when to use Inheritence and when to use abstract class??
Ryan Sykes wrote:An abstract class cannot be instantiated, and must have at least one unimplemented (i.e. abstract) method.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Specifically, the answer to the question "is (child) a (parent)" MUST be true in all cases. For example, if you have two classes: Person and Employee, it's quite likely that Employee should extend (ie, inherit from) Person, because an Employee is a Person.
You should be aware though that there are other ways of implementing this kind of relationship (eg, composition), which are often better than a strict hierarchy.
Jeff Verdegan wrote:Composition is a HAS-A relationship, not IS-A.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Jeff Verdegan wrote:Composition is a HAS-A relationship, not IS-A.
Yes, but it can be used to simply extend a class's capabilities, especially when you're not really sure if you want to tie them together or not (or publicize the relationship to clients). I guess what I was trying to say is that inheritance should be used sparingly.
Winston
Winston Gutkowski wrote:
Ryan Sykes wrote:An abstract class cannot be instantiated, and must have at least one unimplemented (i.e. abstract) method.
The second part of that is not true (although more often than not an abstract class will have at least one abstract method); perhaps you're thinking of C++.
Jeff Verdegan wrote:
Winston Gutkowski wrote:
Jeff Verdegan wrote:Composition is a HAS-A relationship, not IS-A.
Yes, but it can be used to simply extend a class's capabilities, especially when you're not really sure if you want to tie them together or not (or publicize the relationship to clients). I guess what I was trying to say is that inheritance should be used sparingly.
Winston
Exactly. Prefer composition over inheritance, as they say.
deepak carter wrote:Hi All
Thanks for your response
There was a typo in the question title.It was Interface or Abstract class i mean when we should use interface or when we should abstract class.
I read a previous blog stating that it all depends upon the project or requirement...but there might be scenarios where only interface will be suitable or abstract class will be suitable???
Thanks in advance
deepak carter wrote:It was Interface or Abstract class i mean when we should use interface or when we should abstract class.
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|