What is an abstract class, and when it should be used?

//Abstract classes are classes that contain one or more abstract methods.

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

namespace AbstractClass
{
    class Program
    {
        static void Main(string[] args)
        {

            Dog obj = new Dog();
            obj.makeNoise();
        }


    }

    // Note that the abstract keyword is used to denote both an abstract method, and an abstract class.
    // we can not create instance of Abstract class
    // By using abstract classes, you can inherit the implementation of other (non-abstract) methods.
    // You can't do that with interfaces - an interface cannot provide any method implementations.
 
 public abstract class Animal
    {
        public void eat(string food)
        {
            // do something with food....
        }

        public void sleep(int hours)
        {
            try
            {
             
                Console.WriteLine(hours);
            }
            catch (Exception)
            { /* ignore */
         
            }
        }


        // An abstract method is a method that is declared, but contains no implementation.
        public abstract void makeNoise();

    }
    public class Dog : Animal
    {
        public  override void  makeNoise()
        { Console.WriteLine("Bark! Bark!");
         Console.ReadLine();
        }
    }

    public class cow : Animal
    {
        public override void makeNoise()
        { Console.WriteLine("Moo! Moo!");
        Console.ReadLine();
        }
    }
}

Comments

Popular posts from this blog

Validate Mobile Number with 10 Digits in ASP.Net