Tuesday, 3 June 2014

              What and When and Where and How—ABSTRACT CLASS

What is abstract Class?

·         The main purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override.

·         Abstract classes are mostly used as a partially implementation (mind it abstract class and abstract methods are different)

·         Inside abstract class will able to declare normal method as well as abstract method but the abstract method does not have a definition

·         So implementation class must implement the abstract method that is override the abstract method from the abstract class.

·         An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.

·         Abstract classes can have a constructor and destructor but does not support private access specifier.

·         We are also archiving the polymorphism via abstract class method.

·         Ex: once you create abstract methods in abstract class. Wherever you want you can able to implement the abstract class abstract method then add your own functionality based on the classes need. So multiple classes can have the same name of abstract method implementation with different functionality according to the class level. That is called runtime polymorphism

·         Then both abstract and virtual method is override in derived class then what is the different of abstract and virtual method?

Abstract-Method
Virtual-Method
When you consuming the abstract class then An Abstract method must be override in child classes
Virtual method is not compulsory to override.
An abstract method doesn't have implementation detail
But an virtual method have an implementation details
Only abstract class can have abstract method
But any classes can have a virtual method

When and where will go for abstract class/Interface?

While getting a requirement from the client .you feel the most of the functionality same only and small, small things we need to add based on the requirement. that time you will go for Abstract class that is partially implementation
A good abstract class will reduce the amount of code that has to be rewritten because it's functionality or state can be shared.
But there is not much more the common functionality. At that time you will go with interface.

How to implement

·         See the below complete example of abstract class implementation for yours better understanding
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractClass_Implementaton
{
    //abstract class creation
    public abstract class employee
    {
        protected int empid;
        protected string empname;
        protected double salary;
        //constructor creation
        public employee(int empid, string empname, double salary)
        {
            this.empid = empid;
            this.empname = empname;
            this.salary = salary;
        }

        //virtual method creation
        public virtual void display()
        {
            Console.WriteLine("Empid" + empid);
            Console.WriteLine("EmpName" + empname);
            Console.WriteLine("EmpSalary" + salary);
        }

        //abstract method creation
        public abstract void calculatesalary(int days);
   

    }


    //create a derived classes for inherit a abstract classes
    class Indian : employee
    {
     protected int countrycode;
        //sendind a argument to the abstract class constructor using the base keyword
     public Indian(int empid, string empname, double salary, int countrycode) : base(empid, empname, salary)
    {
        this.countrycode = countrycode;     
    }

        //virtuakl class implementation
        public override void  display()
        {
          
            base.display();
            Console.WriteLine("Country Code" + countrycode );
        }

       //abstract class implementation
        public override void calculatesalary(int days)
        {
            double  grosssal = days * salary ;
            Console.WriteLine("salary of the month" + grosssal);
        }

    }


    class China : employee
    {
        protected string state;
        public China(int emid, string empname, double salary, string state)  : base(emid, empname, salary)
        {
            this.state = state;
        }

        public override void display()
        {
            base.display();
            Console.WriteLine("state" + state);
        }

        public override void calculatesalary(int days)
        {
            double grosssal = days * salary / 20;
            Console.WriteLine("slary of the month" + grosssal);
        }
    }

    class europe:employee
    {
        protected string countryname;
        public europe(int empid, string empname, double salary, string countryname) : base(empid, empname, salary)
        {
            this.countryname = countryname;

        }

        public override void display()
        {
            base.display();
            Console.WriteLine("countryname" + countryname);
        }


        public override void calculatesalary(int days)
        {
            double grosssal = 4500 * days / 20;
            Console.WriteLine("salary of the month:" + salary);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            employee e;
            Console.WriteLine("Please enter the value 1-4");
            int ch =Convert.ToInt32(Console.ReadLine());
            switch (ch)
            {
                case 1:
                    Console.WriteLine("*************************************************************");
                    e = new Indian (12, "anand", 200.33, 110);
                    //call the virtual method
                    e.display();
                    //call the abstract method
                    e.calculatesalary(30);
                    Console.WriteLine("*************************************************************");
                    break;
                case 2:
                    Console.WriteLine("*************************************************************");
                    e = new China (33, "Anitha", 445.4, "sangmang");
                    e.display();
                    e.calculatesalary(30);
                    Console.WriteLine("*************************************************************");
                    break;
                case 3:
                    Console.WriteLine("*************************************************************");
                    e = new europe(33, "Arun", 445.4, "Norway");
                    e.display();
                    e.calculatesalary(30);
                    Console.WriteLine("*************************************************************");
                    break;
                case 4:
                    System.Threading.Thread.CurrentThread.Abort();
                    break ;
            }
            Console.ReadLine();
        }
    }
}

 Thanks to All!

No comments:

Post a Comment