Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ASP.Net : Abstract class with real time example in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.78k
    Comment on it

    ASP.Net : Abstract class with real time example in C#

     

    1) A class is called an abstract class if it is marked with an abstract keyword.

     

    2)The reason for which abstract class came into existence is that it provides a default functionality to its subclasses. Therefore, an abstract class need to be inherited.

     

    3)It is mandatory to mark a class as abstract if a class contains an abstract method. An abstract method is a method with only declaration but no definition. But an abstract class need not to have an abstract method. Therefore an abstract class may or may not contain abstract method along with methods with complete implementation.

     

    4)It is not possible to create object of abstract class.

     

    5)The abstract method declared in abstract class need to be implemented in subclasses.

     

    6)An Abstract class cannot be instantiated but still it can have a Constructor and Destructor. The Constructor of abstract class is called by constructor of subclass that is inheriting the abstract class. The child class first calls the constructor of parent class followed by it's own Constructor.

     

    7)Sealed and abstract keyword cannot be used together with a class since sealed prevents a class from being inherited whereas abstract class needs to be inherited.

     

    8)The class members within abstract class can have modifiers like private,public etc but abstract members cannot have private modifier.

     

    9)Multiple inheritance is not supported by abstract class.

     

    Program to demonstrate Abstract class:

    using System;
    
    namespace AbstractClassDemo
    {
       
        public abstract class Employee
        {
            public int EmpId { get; set; }
            public string EmpName { get; set; }
            public int working_hours { get; set; }
            public int days { get; set; }
            protected Employee(int eid,string ename,int whours,int days)
            {
                EmpId = eid;
                EmpName = ename;
                working_hours = whours;
                this.days = days;
            }
            public abstract double salary_calculate();
        }
        public class PermanentEmployee : Employee
        {
            public PermanentEmployee(int eid,string ename,int whours,int days): base(eid,ename,whours,days)
            {
    
            }
            public override double salary_calculate()
            {
                return working_hours * days + 4000;
            }
        }
        public class TemporaryEmployee : Employee
        {
            public int probation_period { get; set; }
            public TemporaryEmployee(int eid,string ename,int whours,int days,int tempprobation): base(eid,ename,whours,days)
            {
                probation_period = tempprobation;
            }
            public override double salary_calculate()
            {
                return working_hours * days + 3600;
            }
        }
        class TestAbstractClassDemo
        {
            static void Main(string[] args)
            {
                Employee emp;
                emp = new PermanentEmployee(1,"Deepak",10,100);
                var sal = emp.salary_calculate();
                Console.WriteLine("The Employee id of Permanent Employee is:{0}",emp.EmpId);
                Console.WriteLine("The Employee name of Permanent Employee is:{0}",emp.EmpName);
                Console.WriteLine("The salary of Permanent Employee is:{0}", sal);
    
                TemporaryEmployee temporaryemployee = new TemporaryEmployee(2,"Suraj",8,90,180);
                emp = temporaryemployee;
                var salary = emp.salary_calculate();
                Console.WriteLine("The probation period for Temporary Employee is:{0} days", temporaryemployee.probation_period);
                Console.WriteLine("The Employee id of Temporary Employee is:{0}", emp.EmpId);
                Console.WriteLine("The Employee name of Temporary Employee is:{0}", emp.EmpName);
                Console.WriteLine("The salary of Temporary Employee is:{0}", salary);
                Console.ReadKey();
            }
    
    
        }
    }
    

    Output:

    The Employee id of Permanent Employee is:1
    The Employee name of Permanent Employee is:Deepak
    The salary of Permanent Employee is:5000
    The probation period for Temporary Employee is:180 days
    The Employee id of Temporary Employee is:2
    The Employee name of Temporary Employee is:Suraj
    The salary of Temporary Employee is:4320

     

    Explanation of Abstract class example:

    In a company Employees can be categorised into permanent employee and temporary employee. These employees have some common features like name,employee id,address etc but salary or calculation of salary will differ. Here comes the concept of Abstract class,in above situation there can be Abstract class Employee with common fields and an abstract method for calculating salary.

    The method for calculating salary has to be abstract because without abstract Employee class need to implement calculate salary method and this implementation cannot be used by either kind of Employee,since permanent and temporary employee need to have their own implementation of salary calculation, here, Employee class should be treated as base class with two subclasses PermanentEmployee class and TemporaryEmployee class.

     

    In above program PermanentEmployee class and TemporaryEmployee class have common property such as name, id, working_hours, workin_days. These properties therefore declared in base class with abstract method salary_calculate(). The two subclasses inherit base class Employee and have their own implementation of abstract method. The child class first calls the constructor of parent class followed by it's own Constructor therefore initializing common properties. The subclasses can also have properties which are unique to them e.g TemporaryEmployee is on probation period therefore TemporaryEmployee class contains a property probation_period which is unique to that class.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: