Monday, April 25, 2016

What is an Abstract Class?

An abstract class is a special class which cannot be instantiated but it needs to be implemented in the sub class. The methods declared within the class needs to be implemented (in the abstract class we only have the declaration for the method) in the sub class using the "override" keyword. The abstract class or method declared needs to have the keyword "abstract".

Features:
    1. An abstract calss can inherit from a class and one or more interfaces.
    2. An abstract class can implement code with non-Abstract methods.
    3. An Abstract class can have modifiers for methods, properties etc.
    4. An Abstract class can have constants and fields.
    5. An abstract class can implement a property.
    6. An abstract class can have constructors or destructors.
    7. An abstract class cannot be inherited from by structures.
    8. An abstract class cannot support multiple inheritance.

Example 1:
    #region
    //An abstract calss can inherit from a class and one or more interfaces.
    interface IVendorTransDetails    {
        void getVendorID();
    }
    interface IClaimsTracker    {
        void getSeqID();
    }
    class ClaimsMaster    {
        string getDCNNO()
        {
            return "PC20100308A00005";
        }
    }
Example 2:

abstract class Abstract : ClaimsMaster, IClaimsTracker, IVendorTransDetails
{
    //Here we should implement modifiers oterwise it throws complie-time error    public void getVendorID()
    {
        int s = new int();
        s = 001;
        Console.Write(s);
    }
    public void getSeqID()
    {
        int SeqID = new int();
        SeqID = 001;
        Console.Write(SeqID);
    }
#endregion

Example 3:
    #region
    //An abstract class can implement code with non-Abstract methods.
abstract class NonAbstractMethod{
    //It is a Non-abstract method we should implement code into the non-abstract method on the class.     public string getDcn()
    {
        return "PS20100301A0012";
    } 
    public abstract void getSeqID(); 
class Utilize : NonAbstractMethod{
    public override void getSeqID()
    {
    }

#endregion

Example 4:

    #region 
    //Abstract class can have modifiers for methods,properties and An abstract class can implement a property
public abstract class abstractModifier{
    private int id; 
    public int ID
    {
        get { return id; }
        set { id = value; }
    } 
    internal abstract void Add();
#endregion

Example 5:

    #region 
    //Abstract class can have constant and fields    public abstract class ConstantFields    {
        public int no;
        private const int id = 10;
    }
    #endregion

Example 6:

    #region 
    //An abstract class can have constructors or destructors    abstract class ConsDes    {
        ConsDes()
        {
        }
        ~ConsDes()
        {
        }
    }
    #endregion

Example 7:


    #region
 
    //An abstract class cannot be inherited from by structures    public struct test    {
    }
    //We can't inheritance the struct class on the abstract class    abstract class NotInheritanceStruct    {
    }
    #endregion
Example 8:

    #region 
    //An abstract class cannot support multiple inheritance    class A    {
    }
    class B : A    {
    }
    abstract class Container : B //But we can't iherit like this : A,B    {
    }
    #endregion

No comments:

Post a Comment