Abstract class with real time example using c#


Abstract Class with real time example

What is Abstract Class?

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method. Example:

 

    public abstract class Animal

    {

        public abstract void DoEat(int i);

    }

Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. 

Example 1:  In below example, there is a class bike which has some specific property i.e Bike must have color, bike type , speed and so on. But we can’t say what would be the color of bike, what would be the type of bike(electrical or non-electrical) , what would be the speed of bike. But we would be able to define 

All above property or method when we are going to build some boke model. In the below example Jupiter is a class which is inheriting the bike class and in that class we will be able to set the all the property of bike means Jupiter speed, Jupiter colors, Jupiter bike type and so on… For reference see the below example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace AbstractExample1

{

    public abstract class Bike

    {

        //To check bike type i.e electrical or petrol. This property depends based on derived class.

        public bool bIsElectricType{get;set;}

        //An abstract method, to be 

        //overridden in derived class

        public abstract void run();

    }

 

    public class Jupiter : Bike

    {

      

        //using override keyword,

        //implementing the abstract method

        public override void run()

        {

           Console.WriteLine("Running Safely.");

        }

    }

}

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace AbstractExample1

{

    class Program

    {

        static void Main(string[] args)

        {

        //We can create an of an abstract class

       //Assign the object of derived class so that it can access concrete object of          abstrct class and all member of derived class.

            Bike objBike = new Jupiter ();

            objBike.run();

            objBike.bIsElectricType = true;

            Console.WriteLine("Is this bike is electrical ? : {0}",objBike.bIsElectricType);

            Console.ReadLine();

        }

    }

}

Output :


Recommended Book for MVC : List of Some Important books for .NET Framework, C#, MVC, WCF



(Visited 326 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *