Monday, 25 September 2017

Abstract Class


The abstract modifier can be used with classes, methods, properties, indexers, and events.

Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes.

Abstract classes have the following features:
  • An abstract class cannot be instantiated.
  • An abstract class may contain abstract methods and accessors.
  • It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
  • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation.

Abstract methods have the following features:
  • An abstract method is implicitly a virtual method.
  • Abstract method declarations are only permitted in abstract classes.
  • Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no braces ({ }) following the signature. For example: 
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. For example:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractCLass
{

    class AbstractClassDemo
    {
        public abstract void Animal(string strAnimal)
        {
            Console.WriteLine("Animal Name is :-" +strAnimal);
        }
    }
}
when u compile this u ll get error as
Can not declare a body because it marked as Abstract
Instead u use following code to make a abstract Function.
Second thing is class is not abstract so we have to make it abstract

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractCLass
{

    abstract class AbstractClassDemo
    {
        public abstract void Animal(string strAnimal);
    }
}

it will compile 
Now u Run below Code.

1)  Class 1
namespace AbstractCLass
{

    abstract class AbstractClassDemo
    {
        public abstract void Animal(string strAnimal);
    }

    class Derived_abstract : AbstractClassDemo
    {
        public override void Animal(string strAnimal)
        {
            Console.WriteLine("This animal is :=  "+strAnimal);
        }
    }
}
2)  Class 2
namespace AbstractCLass
    class Class_Main
    {
        static void Main(string[] args)
        {
            Derived_abstract objDerived_abstract = new Derived_abstract();
            objDerived_abstract.Animal("C# .Net");
            Console.ReadLine();
        }
    }
}

This is a simple Demo for abstract Class 
I also Refer Some concepts of Microsoft MSDN
Its helps me Lot to clear My Concepts

Inheritance In C#

When you derive a class from a base class, the derived class will inherit all members of the base class except constructors, though whether the derived class would be able to access those members would depend upon the accessibility of those members in the base class. C# gives us polymorphism through inheritance. Inheritance-based polymorphism allows us to define methods in a base class and override them with derived class implementations. Thus if you have a base class object that might be holding one of several derived class objects, polymorphism when properly used allows you to call a method that will work differently according to the type of derived class the object belongs to.
The Following are the classes


class Animal
{
    public Animal()
    {
        Console.WriteLine("Animal constructor");
    }
    public void Greet()
    {
        Console.WriteLine("Animal says Hello");
    }
    public void Talk()
    {
        Console.WriteLine("Animal talk");
    }
    public virtual void Sing()
    {
        Console.WriteLine("Animal song");
    }
}; 
Now Try this one
 
class Dog : Animal
{
    public Dog()
    {
        Console.WriteLine("Dog constructor");
    }
    public new void Talk()
    {
        Console.WriteLine("Dog talk");
    }
    public override void Sing()
    {
        Console.WriteLine("Dog song");
    }
}; 
Now call above methods
Animal a1 = new Animal();
a1.Talk();
a1.Sing();
a1.Greet();

//Output

Animal constructor
Animal talk
Animal song
Animal says Hello
Okay Now call this one
Animal a2 = new Dog();
a2.Talk();
a2.Sing();
a2.Greet();

//Output

Animal constructor
Dog constructor
Animal talk
Dog song
Animal says Hello