Sunday, August 1, 2010

Few notes about Abstract class and Interfaces...

when you believe that derive class behavior & action would be similar to base in most of the case you use abstract; and when you think that behavior is same but action can change for different derived classes you use interface.

Abstract base class shows is-a relationship while interface shows "can do" relationship for ex: humans and fish can swim while human and horse can run and human, horse and fish are (is a) animals.

So abstract class Animal can be a base class inherited by all humans, horse and fish. While animal itself doesn't represent any physical entity so abstract. A behavior "breath" will be applied to all in animals if defined in animal abstract class itself. May be later on you may want to add "Eat" behavior in abstract class which would not create any consequences on derived class. Or you might support another version of animal which can represent the abstract properties of aliens too.

while Interface IRun will be inherited by human and horse only while ISwim can be inherited by human and Fish only and all these derived classes will define their own style of swimming and running while if you add another behavior in same interface you have to define it in all the derived class.

Sample implementation will be like

Class Human: Animals, IRun, ISwim{}

Class Horse: Animals, IRun{}

Class Fish: Animals,ISwim{}

Class Bird: Animals, IFly{}

later on updated versions of animal

Class Alien: Animals, IFly, ISwim, IRun, ISpace {} //though hypothetical considered just for example :)
REF: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/0ab0d038-61b6-43e0-82da-33338f52f3bf

Why abstract allows constructor? What is the use of it?

So that derived class can call it where creating it's object and abstract base class can initialize it's members.

"Because abstract classes should never be instantiated, it is important to correctly define their constructors. It is also important to ensure that the functionality of your abstract class is correct and easily extended. The following guidelines help ensure that your abstract classes are correctly designed and work as expected when implemented.

Do not define public or protected internal (Protected Friend in Visual Basic) constructors in abstract types &define a protected or an internal constructor in abstract classes:

Constructors with public or protected internal visibility are for types that can be instantiated. Abstract types can never be instantiated. If you define a protected constructor in an abstract class, the base class can perform initialization tasks when instances of a derived class are created. An internal constructor prevents the abstract class from being used as the base class of types that are not in the same assembly as the abstract class. "

http://msdn.microsoft.com/en-us/library/ms229047.aspx

REF: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/175aad18-4514-4320-8195-be480dee4627

Why use reference of abstract class to create object

It is more related to OOPS concept rather than how we do it C#. Liskov substitution principle explain the relation in the best way

"Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T". Here you can assume T as TextWriter and S as Stream Writer

http://en.wikipedia.org/wiki/Subtype#Subtyping_schemes

In Particular to .net concept TextWriter is more generic offer basic functionality to write Streams (StreamWriter) and strings (StringWriter) to a file

REF: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/aa53228f-6d2d-4475-8d7c-8081057f22f9