Abstract-OOPS
//Abstract
// An abstract class is a class that cannot be instantiated, but must be inherited from.
// An abstract class may be fully implemented, but is more usually partially implemented or
// not implemented at all, thereby encapsulating common functionality for inherited classes.
// When you have a class that contains methods that have no implementation (i.e. abstract methods),
// the class is said to be abstract as well. An abstract method is simply a shell or place-marker
// for a method that will be defined later in a derived class. The intention of abstract methods is
// to force every derivation of the class to implement those methods. If a derived class does not implement
// an abstract method, then that class must also be declared as abstract.
abstract public class Person
{
abstract public void Eat();
public void Sleep()
{
Console.WriteLine("Person:Sleep");
}
abstract public int Weight
{
set;
get;
}
}
// You can inherit the abstract class just like any other class.
public class Man : Person
{
int iWeight;
public Man()
{
}
override public void Eat()
{
Console.WriteLine("Man:Eat");
}
override public int Weight
{
set
{
iWeight = value;
}
get
{
return iWeight;
}
}
static void Main()
{
Man i = new Man();
i.Eat();
i.Sleep();
Console.ReadLine();
}
}
}
// You get the following result when you execute the above code.
// Man:Eat
// Person:Sleep
// So why would you declare a class abstract? It’s actually a very powerful class hierarchy
// design tool since you can provide the structure for something that is not very specific —just
// like our Person class. You will never create an Person object; but you will create Man and Woman
// objects. The other advantage is that you are moving the code to where it actually belongs. This helps
// you locate program logic problems.
// A class that is derived from an abstract class may still implement interfaces.
// Abstract classes are useful when creating components because they allow you specify an invariant
// level of functionality in some methods, but leave the implementation of other methods until a specific
// implementation of that class is needed. If additional functionality is needed in derived classes, it can
// be added to the base class without breaking code.
// In the above example, an abstract class is declared with one implemented method, one unimplemented
// method and one unimplemented property. A class inheriting from this class would have to implement the
// Eat method and Weight property
// When implementing an abstract class, you must implement each abstract method in that class, and each
// implemented method must receive the same number and type of arguments, and have the same return value,
// as the method specified in the abstract class.
//Recommendations on using Abstract Classes and Interfaces
//1. If you anticipate creating multiple versions of your component, create an abstract class. Abstract
// classes provide a simple and easy way to version your components. By updating the base class, all
// inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot
// be changed once created. If a new version of an interface is required, you must create a whole new interface.
//2. If the functionality you are creating will be useful across a wide range of disparate objects,
// use an interface. Abstract classes should be used primarily for objects that are closely related,
// whereas interfaces are best suited for providing common functionality to unrelated classes.
//3. If you are designing small, concise bits of functionality, use interfaces. If you are designing
// large functional units, use an abstract class.
//4. If you want to provide common, implemented functionality among all implementations of your component,
// use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces
// contain no implementation for any members.