Tuesday, August 20, 2019

Abstract class in oop

Abstract class in one of the concept  oop.

So today i will explain you abstract class.

I am using the console application for this example.

using System;
using System . Collections . Generic;
using System . Text;

namespace ConsoleApplication46
{
    /*1. abstract class mean that the class is incomplete and cannot be used directly
    2. abstract class contain abstract method.
    3. you cannot create instance of an abstract class to use its member functions.
    4. Abstract methods are methods without any body.
    5. The implementation of an abstract method is done by the derived class.
     */

    abstract class Animal
    {
        public abstract void Foodhabbits ( );
    }

    class carnivorous:Animal
    {
        /*when a derived class inherits the abstract method from the abstract class,it must override
         the abstract method to provide it a new functionality */

        public override void Foodhabbits ( )
        {
            Console . WriteLine ( "The carnivorous animals eat only meat " );
        }
   }

    class Herbivorous:Animal
    {
        public override void Foodhabbits ( )
        {
            Console . WriteLine ( "Th herbivorous  animals eat only plants " );
        }
   }

    class Program
    {
        static void Main ( string [ ] args )
        {
            carnivorous cn = new carnivorous ( );
            Herbivorous hb = new Herbivorous ( );
            cn . Foodhabbits ( );
            hb . Foodhabbits ( );

            /* in above programe abstract method is inherited in the carnivorous and herbivorous classes.
             abstract method cannot contain any method body*/
        }
    }
}


Thank you,

Hope my example is help you.

Arjun Walmiki