X

CHEEZY CODE


Interested In More?


We have various articles on different topics.
Choose your technology stack from links below.


Video Tutorials C# Android JavaScript


Get Latest Updates


Abstract Classes In C#

Abstract classes in CSharp, are another type of classes which are created just to provide some architectural structure to the code and they have a uniqueness, that these classes can't be initialized but only be derived from. Let's understand about these classes, What are these? Why these abstract classes are used?


abstract class in csharp cheezycode





What Is An Abstract Class?


An abstract class is declared by using the abstract keyword in front of the class name. It is basically created to provide a common definition of a base class that multiple derived classes can share using inheritance. In these classes, we can declare and define methods just like normal base classes but there's an extra benefit. In these classes, you can simply declare methods without providing any definition to them using the abstract keyword. The derived class can then provide a definition for that particular abstract method using override keyword. Too much to munch in..hold on let us have a look at the example first


public abstract class Device
{
 public void PowerOn()
 {
  //Some common basic functionality
 }

 public void PowerOff()
 {
  //Some common basic functionality
 }

 public abstract void PlayVideo();

 public virtual void PlayAudio(){
  //Some functionality to be overriden
 }
}


Now in the above example class Device is an abstract class. It has some basic functionalities written in terms of methods. This class can only be inherited by other classes not instantiated. Even if you try to create an instance of an abstract class, the compiler throws an error.

So, this Device class is actually providing some basic functionalities which various device classes like Mobile, Tablet, Laptop will use, thus providing a structure of functionality. This can be visualized in various other areas, but let's stick to technology. Devices.

Just to give you an idea what an abstract class can contain, we have taken this Device class example. It has a different type of methods like abstract, virtual, basic methods. An abstract class can have any of these types or all of these or mix of these, it's up to us how we want to implement it.

abstract: These methods are just declared here(No definition) and must be implemented inside the class which inherits from Device class. The example here is PlayVideo(). It has no definition.

virtual: These methods are declared and defined here but its definition can be overridden inside derived class using override. We will discuss this keyword in upcoming posts. The example here is PlayAudio().

Rest of the methods are normal ones which we have discussed earlier in methods. Other than this, we can have normal properties and variables & constants inside these abstract classes. Let's have a look at the features of an abstract class.

Key Features Of An Abstract Class

  • It can't be instantiated, can only be used as a base class.
  • It can inherit from other class.
  • It can have properties, constants variables just like a normal class.
  • It is not mandatory to have an abstract method inside Abstract class.
  • You can have abstract methods without definition in it which can be overridden in derived classes.
  • You can have virtual methods with the definition in it which can be overridden in derived classes.

So, that's all for now about abstract classes. There's a lot more about these and we will be introducing the somewhat similar concept as abstract classes like interface in next post. Stay tuned.


Happy Learning!! Follow our full C# Tutorial Index for complete series.


Abstract Class vs Interface: Video Below



No comments:

Post a Comment