articles

Home / DeveloperSection / Articles / Abstract Class

Abstract Class

Anchal Kesharwani4811 25-Jun-2014

In this article, I’m explaining abstract class concept.

Abstract class is a special type of class which cannot be instantiated and acts as a super class for other classes. An Abstract method must be implemented in the non-Abstract class using the override keyword. The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.

When a method is declared as abstract in the base class then every derived class of that class must provide its own definition for that method.

An abstract class can also contain methods with complete implementation, besides abstract methods.

When a class contains at least one abstract method, then the class must be declared as abstract class.

It is mandatory to override abstract method in the derived class.

When a class is declared as abstract class, then it is not possible to create an

instance for that class. But it can be used as a parameter in a method.

Features

 

  •  An abstract class can inherit from a class or one or more interfaces.
  •  An abstract class can implement code with non-Abstract methods.
  •  An Abstract class can have modifiers for methods, properties etc.
  •  An Abstract class can have constants and fields.
  •  An abstract class can implement a property.
  •  An abstract class can have constructors or destructors.
  •  An abstract class cannot be inherited from by structures.
  •  An abstract class cannot support multiple inheritance.

 

Example 1

 

//An abstract class can inherit from a class and one or more interfaces.
    interface Mouse
    {
        void getMouseID();
    }
    interface Keyboard
    {
        void getKeyboardID();
    }
    class Computer
    {
        public string getComputerID()
        {
            return "COMP1001";
        }
    }
    abstract class Abstract : ComputerMouseKeyboard
    {
        //Here we should implement modifiers override oterwise it throws complie-time error
        public void getKeyboardID()
        {
            int s = new int();
            s = 001;
            Console.Write(s);
        }
 
        public void getKeyboardID()
        {
            int SeqID = new int();
            SeqID = 001;
            Console.Write(SeqID);
        }
    }

In this example, an abstract class can inherit from a class and one or more

interfaces and if we should implement modifiers override otherwise it throws

compile-time error.

 

Example 2

 

abstract class NonAbstractMethod
    {
        //It is a Non-abstract method we should implement code into the non-abstract method on the class.
        public string getOS()
        {
            return "Windows 8";
        }
        public abstract void getMouseID();
    }
    class DerivedAbstract : NonAbstractMethod
    {
        public abstract void getMouseID()
        {
        
        }
    }

In this example, an abstract class can contain non-abstract method.

Example 3

 

//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();
    }
    class DerivedAbstract : NonAbstractMethod
    {
        public abstract void getMouseID()
        {
       
        }
    }

In this example, abstract class can have modifiers for methods, properties and an

abstract class can implement a property and an abstract class can have constant

fields.

Example 4

 

abstract class ConsDesExample
    {
        ConsDesExample()
        {
        }
        ~ConsDesExample()
        {
        }
    }

In this example, an abstract class can contain Constructor and Destructor. 

Example 5

 

//An abstract class cannot be inherited from by structures
    public struct structTest
    {
       
    }
    //An abstract class cannot support multiple inheritance
    class A
    {
    }
    class B : A
    {
    }
    abstract class AbstractExample
    {
 
    }

In this example, an abstract class cannot inherited by struct class and cannot

support multiple inheritance.


Updated 29-Nov-2017

Leave Comment

Comments

Liked By