In this article we look at what an abstract class is, its method and properties and how to use them.
The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The classes that can't be initialized are called abstract classes. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. The members of the class don't have an implementation in a class with an abstract modifier. The abstract modifier can be used with classes, methods, properties, indexers, and events. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.
Abstract Class
- An abstract class cannot be instantiated.
- An abstract class not only contains abstract methods and assessors but also contains non-abstract methods, properties and indexers.
- It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings. The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
- When a class contains at least one abstract method, then the class must be declared as an abstract class.
- When a class is declared as an abstract class, then it is not possible to create an instance for that class. It can however be used as a parameter in a method.
- An abstract class can't be a static class.
- An abstract class is a kind of contract that forces all the sub-classes to carry on the same hierarchies or standards.
Abstract Method/Poperties
- An abstract method / property has a signature in an abstract class.
- An abstract method / property has an implementation in a derived class.
- An abstract method / property can't be static.
- An abstract method / property is implicitly a virtual method / property.
- An abstract method / property is impmeneted in a drived class using the override keyword.
Interface versus Abstract Class
- We can't define fields in an interface but can define fields in an abstract class.
- The interface has a signature of methods but an abstract class can contain both types of methods; these have a signature or an implementation.
- Interface members are by default public and can't use an access specifier for them but in an abstract class we can define an access specifier for each member.
- An interface is slow because it needs to find the actual method in the corresponding classes. But an abstract class is fast.
- We can inherit from multiple interfaces for a single class but can't inherit multiple abstract classes.
Example
Assuming "Vehicle" is an abstract class that the "Car" class inherits. The "Vehicle" class has abstract members and these are implemented by the "Car" class. The "Car" class is used in the "Program" class.
using System;
namespace AbstractExample
{
abstract class Vehicle
{
public abstract double Distance { get; set; }
public abstract double Time { get; set; }
public abstract double Speed();
}
class Car : Vehicle
{
double mDistance, mTime = 0.0;
public override double Distance
{
get
{
return mDistance;
}
set
{
if (value <= 0)
{
mDistance = 1;
}
else
{
mDistance = value;
}
}
}
public override double Time
{
get
{
return mTime;
}
set
{
if (value <= 0)
{
mTime = 1;
}
else
{
mTime = value;
}
}
}
public override double Speed()
{
double speed = 0.0;
double hours = mTime / 60;
speed = mDistance / hours;
return speed;
}
}
class Program
{
static void Main(string[] args)
{
double speed = 0.0;
Car objCar = new Car();
Console.WriteLine("Enter the Distance");
objCar.Distance = Double.Parse(Console.ReadLine());
Console.WriteLine("Enter the time in minutes");
objCar.Time = Double.Parse(Console.ReadLine());
speed = objCar.Speed();
Console.WriteLine("Car speed is {0:0.00}", speed);
Console.Read();
}
}
}