the big Chaos.....
-
Can any one explain when to use abstract class and when to use interface? Or under which scenario we should use abstract class and interface..... Zxczc
Generally, abstract classes are used when you have some implementation in the base class, which calls virtual methods that can be overridden in derived classes. Interfaces can't have implementation, so they can't be used there. Also, interfaces can be used to express "can also be" kind of relationship. For eg, you can have a concrete class deriving from an abstract class and implementing IDisposable, so your class "can be" disposed. Also, there are versioning issues. You can add additional virtual methods to abstract classes without breaking existing clients, you can't do that with interfaces. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Can any one explain when to use abstract class and when to use interface? Or under which scenario we should use abstract class and interface..... Zxczc
Also, to add to the excellent Kumar answer, I'd add that formally, you should use an abstract class when the concrete class you'll implement is an specialization of the abstract one (aka "is a" relationship). When you define an interface, you often model an specific behavior (aka "-able" suffix: IDisposable, IClonable, IMovable, IComparable, IFormattable). I see dead pixels Yes, even I am blogging now!
-
Generally, abstract classes are used when you have some implementation in the base class, which calls virtual methods that can be overridden in derived classes. Interfaces can't have implementation, so they can't be used there. Also, interfaces can be used to express "can also be" kind of relationship. For eg, you can have a concrete class deriving from an abstract class and implementing IDisposable, so your class "can be" disposed. Also, there are versioning issues. You can add additional virtual methods to abstract classes without breaking existing clients, you can't do that with interfaces. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Awesome answer/explaination! :)
Paul Lyons, CCPL
Certified Code Project Lurker -
Also, to add to the excellent Kumar answer, I'd add that formally, you should use an abstract class when the concrete class you'll implement is an specialization of the abstract one (aka "is a" relationship). When you define an interface, you often model an specific behavior (aka "-able" suffix: IDisposable, IClonable, IMovable, IComparable, IFormattable). I see dead pixels Yes, even I am blogging now!
and INativeWindowable ;) (ok ok, I do agree with the above, but as always , there are scenarios when interfaces are used in the "is a" kind of way) //Roger
-
and INativeWindowable ;) (ok ok, I do agree with the above, but as always , there are scenarios when interfaces are used in the "is a" kind of way) //Roger