Abstract Classes

Self-Innovator
Posted by in OOPS category on for Intermediate level | Points: 250 | Views : 1560 red flag

This article explains the core concept of OOPS ie:-Abstract Classes, which are all the ways the abstract classes can be useful during programming.

Introduction

This article explains the core concept of OOPS (Abstract Classes), which plays a vital role in programming world.

Background

Abstract Classes in OOPS is a key concept which is widely being used in programming. An abstract class has a wide range of features which can be utilized or fulfilled the programming need in terms of achieving a result in an efficient manner by programmers.

Using the code

Abstract class are much similar to an interfaces, In this article we will try to see some different scenarios of how and where the abstract classes can be useful and we will try to differentiate the different behavioral states of an abstract class. 

Scenario 1: Abstract classes cannot be instantiated
In the below code snippet we are trying to create an instance of an abstract class (Student) which results in a compilation error
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Abstract_Classes
{
    abstract class Student
    {
        public void Name()
        {
            Console.WriteLine("Calling a Non Abstract Method");
        }
   
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student();
        }
    }
}


Scenario 2: Abstract classes must inherit
All the abstract classes must inherit by derived class in order to access the properties of an abstract class. In the below code snippet class (Program) is inheriting an abstract class (Student)

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

namespace Abstract_Classes
{
    abstract class Student
    {
        public void Name()
        {
            Console.WriteLine("Calling a Non Abstract Method");
        }
   
    }
    class Program:Student
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Name();
            Console.ReadKey();
        }
    }
}

Scenario 3: Abstract Methods cannot be declared as private
The beauty of an abstract class is the abstract class can have abstract and non-abstract methods, the abstract methods of abstract class must be public by default and it cannot be made as private, because they are being inherited by derived classes and implementation is must for abstract methods. In the code snippet below, the abstract method (Marks) is not implemented by derived class (Program) which results in a compilation error

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

namespace Abstract_Classes
{
    abstract class Student
    {      
        public void Name()  //non-abstract method
        {
            Console.WriteLine("Calling a Non Abstract Method");
        }     
        abstract void Marks();   //abstract method as private
    }
    class Program:Student
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Name();
            Console.ReadKey();
        }
    }
}


Scenario 4: Abstract methods cannot be implemented as normal class methods
The abstract methods of abstract class must be implemented by override keyword unlike normal class implementation. The abstract method act as an redefining the implementation of a method. In the code snippet below when we try to implement the abstract method as normal implementation throws an compilation error.

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

namespace Abstract_Classes
{
    abstract class Student
    {       
        public void Name() //non-abstract method
        {
            Console.WriteLine("Calling a Non Abstract Method");
        }     
       public abstract void Marks();   //abstract method 
    }
    class Program:Student
    {
        public void Marks()
        {
            Console.WriteLine("Calling a Abstract Method Marks");
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Name();
            Console.ReadKey();
        }
    }
}


Scenario 5: Abstract methods cannot be implemented using a new keyword
The normal methods of a normal class can redefine the implementation of the methods using the NEW keyword but the abstract methods cannot be use/redefine the implementation wit NEW keyword. In the below code snippet when we try to redefine the implementation of an abstract method with a NEW keyword results in a compilation error.



Scenario 6: Abstract methods should be override
The implementation of an abstract methods must override is implementation in derived class. The below code snippet uses an override keyword for implementing the abstract method.

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

namespace Abstract_Classes
{
    abstract class Student
    {      
        public void Name()  //non-abstract method
        {
            Console.WriteLine("Calling a Non Abstract Method");
        }
       public abstract void Marks();        //abstract method 
    }     class Program:Student     {         public override void Marks()         {             Console.WriteLine("Calling a Abstract Method Marks");         }         static void Main(string[] args)         {             Program p = new Program();             p.Name();             p.Marks();             Console.ReadKey();         }     } }

Scenario 7: Creating a reference of abstract Class
Abstract classes cannot be instantiated but we can create a references for abstract classes. In the below code snippet we are creating an reference as s for an abstract class (Student) and accessing the properties of class (Student)

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

namespace Abstract_Classes
{
    abstract class Student
    {        
        public void Name()//non-abstract method
        {
            Console.WriteLine("Calling a Non Abstract Method");
        }
       public abstract void Marks();        //abstract method 
    }
    class Program:Student
    {
        public override void Marks()
        {
            Console.WriteLine("Calling a Abstract Method Marks");
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            Student s = p;
            s.Marks();
            s.Name();
            Console.ReadKey();
        }
    }
}


Scenario 8: Abstract methods cannot be virtual
The abstract methods of abstract class cannot be declared as virtual. In the below code snippet we are trying to make an abstract method as virtual which results in compilation error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Abstract_Classes
{
    abstract class Student
    {     
        public void Name()   //non-abstract method
        {
            Console.WriteLine("Calling a Non Abstract Method");
        }
       public abstract virtual void Marks();        //abstract method 
    }
    class Program:Student
    {
        public override void Marks()
        {
            Console.WriteLine("Calling a Abstract Method Marks");
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Name();
            p.Marks();
            Console.ReadKey();
        }
    }
}


Scenario 9: Multiple inheritance is not supported
Multiple inheritance is not supported in abstract class, In the below code snippet we are trying to inherit two base classes (Student,Teacher) by a derived class (Program) which results in a compile time error.

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

namespace Abstract_Classes
{
    abstract class Student
    {
        public abstract void Department();
    }
    abstract class Teacher
    {
        public abstract void School();
    }
    class Program:Student,Teacher
    {
        public override void Department()
        {
            Console.WriteLine("Calling a Abstract Method Department");
        }
        public override void School()
        {
            Console.WriteLine("Calling a Abstract Method School");
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Department();
            p.School();
            Console.ReadKey();
        }
    }
}


Scenario 10:Abstract Class supports Multilevel inheritance 
Multilevel inheritance is supported by abstract class where a single derived class can inherit a single base class and vice verse, In the below code snippet we are trying to inherit the a single derived class with a single base class

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

namespace Abstract_Classes
{
    abstract class Student
    {
        public abstract void Department();
    }
    abstract class Teacher:Student
    {
        public override void Department()
        {
            Console.WriteLine("Calling a Abstract Method Department");
        }
        public abstract void School();
    }
    class Program:Teacher
    {       
        public override void School()
        {
            Console.WriteLine("Calling a Abstract Method School");
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Department();
            p.School();
            Console.ReadKey();
        }
    }
}


Conclusion

In this article we have been looked in different scenarios of abstract class like the uses and it's implementations. With these features of abstract class we can utilize it's features in our applications according to the requirement.

Reference

https://msdn.microsoft.com/en-us/library/sf985hc5.aspx

Page copy protected against web site content infringement by Copyscape

About the Author

Self-Innovator
Full Name: Sayeed Ahmed
Member Level: Bronze
Member Status: Member
Member Since: 12/22/2011 7:45:35 AM
Country: India
Join Hands Change lives Thanks & Regards Straight Edge Society


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)