DEV Community

Kiron Roy
Kiron Roy

Posted on • Updated on

Abstract Classes

An abstract class is a concept, something that is not tangible. It acts as a base class which one can built upon. Other classes can inherit from an abstract class.

Abstract classes are different from interfaces in that they have full methods that are automactically inherited from another class. Below is a small simple C# console application about pliers.

Below:

  • The abstract HomeImprovementTool class implements the IHomeImprovementToolMaterial interface. This interface has the rule that a tool's material has to be made out of steel.
  • The Pliers class describes a type of pliers. Specifically, needle nose pliers an object that one can touch. It inherits from the abstract HomeImprovementTool which has some properties that are not physical.
Abstract Class (1)
HomeImprovementTool public abstract class
SerialNumber property of string type
Manufacturer property of string type
IsMadeOutOfSteel property of bool type
Class (1)
Pliers public class
PliersType property of string type
Interface (1)
IHomeImprovementToolMaterial public interface
IsMadeOutOfSteel property of bool type
HomeImprovementTool as an Abstract class
namespace Tools
{
    public abstract class HomeImprovementTool : IHomeImprovementToolMaterial
    {
        public string SerialNumber { get; set; }
        public string Manufacturer { get; set; }
        public bool IsMadeOutOfSteel { get; set; }
    }
}

Enter fullscreen mode Exit fullscreen mode
Pliers as a class
namespace Tools
{
    public class Pliers : HomeImprovementTool
    {
        public string PliersType { get; set; }

    }
}
Enter fullscreen mode Exit fullscreen mode
IHomeImprovementToolMaterial
namespace Tools
{
    public interface IHomeImprovementToolMaterial
    {
        bool IsMadeOutOfSteel { get; set; }
    }
}

Enter fullscreen mode Exit fullscreen mode
Program class (Main Entry point of this small console application)
using System;

namespace Tools
{
    class Program
    {
        static void Main(string[] args)
        {
            Pliers pliers = ChannelLockPliersObject();

            ChannelLockOutputToConsole(pliers);

            Console.ReadLine();
        }

        private static void ChannelLockOutputToConsole(Pliers pliers)
        {
            Console.WriteLine($" The product is a {pliers.PliersType} plier. " +
                            $"The serial number is \n" +
                            $" {pliers.SerialNumber} and " +
                            $" the manufacturer is {pliers.Manufacturer}." +
                            $" \n More importantly it is {pliers.IsMadeOutOfSteel}, " +
                            $"the pliers are made out steel.");
        }

        private static Pliers ChannelLockPliersObject()
        {
            return new Pliers
            {
                PliersType = "Needle nose",
                SerialNumber = "201ef3",
                Manufacturer = "Channel Lock",
                IsMadeOutOfSteel = true
            };
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
Console Output
The product is a Needle nose plier. The serial number is
201ef3 and  the manufacturer is Channel Lock.
More importantly it is True, the pliers are made out steel.
Enter fullscreen mode Exit fullscreen mode
On Repl.it

Discussion (0)