Saturday 22 November 2014

OOPS: 9 points to know about Abstract

Good evening guys, Lets have some quick thought about Abstract in Oops concept. I have been hitting my head to understand and purpose of this concept and its advantages, hopefully Ive deciphered it :). Though I wanted to implement it into my project but I couldnt get the opportunity. So this post will quickly cover up one of the best feature of OOPS.

I have worked around the following codes personally.For the complete understanding, Before running this sample example of Abstract,
i have tried removing important keywords and compiled it. The compilation error occured and also registered those errors for your guidance in case if you make any mistake when implementing abstract. Am all done with my words lets see the code example.  One thing please dont forget to post your comments as your feedback. Happy Abstracting.

The following are the key points which may be helpful for you when you implementing abstract or ofcourse for the interview.
The Abstracts

i). Abstract has both complete and incomplete methods
ii).Abstract method should not have body.
iii).Abstract method should be inside Abstract Class
iv).Derived method must use override for the inherited abstract class
v).We must provide override keyword for all base class abstract method in derived class as abstract methods are implicitly virtual(which means can be overridden)
vi).Cannot instantiate an abstract class, however it can be assigned to the derived class
vii)Cannot create object for abstract classes
viii)Only complete methods have to be static.
ix)Abstract class is designed to act as a base class

Those nine points have explanations are as follows

Sample Program


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

namespace OopsTrial
{
    abstract class AbstractClass
    {
        public int _a;
        public abstract void A();
        public static void B()
        {
            
        }
    }

    class Example1 : AbstractClass
    {
        public override void A()
        {
            Console.WriteLine("Example1Class method A");
            base._a++;
        }
    }

    class Example2 : AbstractClass
    {
        public override void A()
        {
            Console.WriteLine("Example2Class method A");
            base._a--;
        }
    }

   
    class Program
    {
        static void Main()
        {
            // Reference Example1 through AbstractClass type.
            AbstractClass test1 = new Example1();
            test1.A();

            // Reference Example2 through AbstractClass type.
            AbstractClass test2 = new Example2();
            test2.A();

            Console.Read();
        }
    }
}
Output:
Example1Class method A
Example2Class method A



i) Abstract has both complete and incomplete methods
Sample:

    abstract class AbstractClass
    {
        public int _a;
        public abstract void A();
        public static void B()
        {
            
        }
    }
ii). Abstract method should not have body.

Correct Format:
public abstract void A();
Incorrect Format:
public abstract void A()
{

}
Error for Incorrect Format:


'OopsTrial.AbstractClass.A()' cannot declare a body because it is marked abstract



iii). Abstract method should be inside Abstract Class

Correct Format:
    abstract class AbstractClass
    {
        public int _a;
        public abstract void A();
        public static void B()
        {
            
        }
    }
Incorrect Format:
    class AbstractClass
    {
        public int _a;
        public abstract void A();
        public static void B()
        {
            
        }
    }
Error for Incorrect Format:

'OopsTrial.AbstractClass.A()' is abstract but it is contained in non-abstract class 'OopsTrial.AbstractClass'


iv) Derived method must use override for the inherited abstract class
Correct Format:
class Example1 : AbstractClass
    {
        public override void A()
        {
            Console.WriteLine("Example1Class method A");
            base._a++;
        }
    }
Incorrect Format:
class Example1 : AbstractClass
    {
        public void A()
        {
            Console.WriteLine("Example1Class method A");
            base._a++;
        }
    }
Error for Incorrect Format:

'OopsTrial.Example1' does not implement inherited abstract member 'OopsTrial.AbstractClass.A()'

v) We must provide override keyword for all base class abstract method in derived class

Correct Format:
abstract class AbstractClass
    {
        public int _a;
        public abstract void A();
        public abstract void B(); // Included new abstract method
       
    }

    class Example1 : AbstractClass
    {
        public override void A()
        {
            Console.WriteLine("Example1Class method A");
            base._a++;
        }
        public override void B()
        {
            
        }
    }

    class Example2 : AbstractClass
    {
        public override void A()
        {
            Console.WriteLine("Example2Class method A");
            base._a--;
        }
        public override void B()
        {

        }
    }
Incorrect Format:
abstract class AbstractClass
    {
        public int _a;
        public abstract void A();
        public abstract void B(); // Included new abstract method
       
    }

    class Example1 : AbstractClass
    {
        public override void A()
        {
            Console.WriteLine("Example1Class method A");
            base._a++;
        }
        public  void B()
        {
            
        }
    }

    class Example2 : AbstractClass
    {
        public override void A()
        {
            Console.WriteLine("Example2Class method A");
            base._a--;
        }
        public override void B()
        {

        }
    }
Error for Incorrect Format:

'OopsTrial.Example1' does not implement inherited abstract member 'OopsTrial.AbstractClass.B()'

vi)  Cannot instantiate an abstract class, however it can be assigned to the derived class
Correct Format
AbstractClass test1 = new Example1();
            test1.A();
Incorrect Format
AbstractClass test1 = new AbstractClass();
            test1.A();
Error for Incorrect format:

Cannot create an instance of the abstract class or interface 'OopsTrial.AbstractClass'

vii) Cannot create object for abstract classes
Correct Format
AbstractClass test1 = new Example1();
            test1.A();
Incorrect Format
AbstractClass test1 = new AbstractClass();
            test1.A();
Error for Incorrect format:

Cannot create an instance of the abstract class or interface 'OopsTrial.AbstractClass'

viii).Only complete methods have to be static.
Correct Format:
abstract class AbstractClass
    {
        public int _a;
        public abstract void A();
        public static void B() 
        { 

        }
    }
Incorrect Format:
abstract class AbstractClass
    {
        public int _a;
        public abstract void A();
        public static void B(); 
    }
Error for Incorrect format:



A static member 'OopsTrial.AbstractClass.A()' cannot be marked as override, virtual, or abstract

ix) Abstract class is designed to act as a base class
Sample:
class Example1 : AbstractClass
    {
        public override void A()
        {
            Console.WriteLine("Example1Class method A");
            base._a++;
        }
        public  void B()
        {
            
        }
    }
The sample holds the derived class which inherits abstract class,the keyword "base" here denotes the member of abstract class only.


End of tutorial

Thanks for listening people, feedbacks are most welcome.

No comments:

Related Posts