Thursday, August 21, 2014

Abstract in C#

In this article, we'll look at what abstract in C# is. Also we'll look at 
when and how you can use it to get its benefits.


In C# abstract modifier indicates that the thing being modified has a missing or incomplete implementation. This modifier can be used with classes, methods, properties, indexers and events. 

Abstract classes can contain both abstract and non-abstract members. Abstract member cannot exist outside an abstract class. You can instantiate abstract class by making a concrete class deriving from it (An instance of a derived class is also a instance of its base class). 

Also you can't create abstract constructor because abstract means you must override it in any non-abstract child class. But you can have non-abstract constructors. You can't override a constructor. You can't create sealed abstract classes. 

You cannot have non-abstract method signature inside abstract class. For non-abstract methods you must provide implementation (link). You cannot have private virtual or abstract members inside a abstract class. But You can have non-abstract private methods inside abstract classes.



In abstract class, if you declare a method as virtual, you can override it in a derived class.(Difference between abstract and virtual) If you create a normal method in abstract class it won't be available in derived class because it doesn't need to be overridden (that's why we mark things as virtual)


You cannot have virtual methods without any method body. Also you cannot have a abstract method with a method body. You must implement all abstract methods and accessors in the class you derive from the abstract class.


You cannot create abstract fields. Only abstract properties. Abstract property cannot be private. (virtual/abstract fields, does abstract property create private backing field )


When to use abstract
  • Abstract classes are useful when you need a class for the purpose of inheritance and polymorphism, but it makes no sense to instantiate the class itself, only its subclasses. They are commonly used when you want to define a template for a group of subclasses that share some common implementation code, but you also want to guarantee that the objects of the super class cannot be created.
  • For instance, let's say you need to create Dog, Cat, Hamster and Fish objects. They possess similar properties like color, size, and number of legs as well as behavior so you create an Animal super class. However, what color is an Animal? How many legs does an Animal object have? In this case, it doesn't make much sense to instantiate an object of type Animal but rather only its subclasses.
  • Abstract classes also have the added benefit in polymorphism allowing you to use the (abstract) super class's type as a method argument or a return type. If for example you had a PetOwner class with a train() method you can define it as taking in an object of type Animal e.g. train(Animal a) as opposed to creating a method for every subtype of Animal

0 comments:

Post a Comment

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook