Why we use abstract class


Why we use abstract class

An abstract class is used to define what is known as a base class. A base class is a class which has the most basic definition of a particular requirement.

Understand more we take an example

Suppose we have a two class Permanent_Employee and contractual_employee

And both class have name (firstName , lastName) , department, in traditional approach we create two class then In main  we call each class method


 Abstract class


In above image we have seen that we have common method name and variable name to minimize the code we just write a class that have common method definition and variable name.
Now we have declared a class Employee as a base class
class Permanent_Employee and contractual_employee are child class, both class inherit base class Employee
Now base class have all method and property, we declare base class method as virtual then may be override in child class

See the code I will explain the problem in this code


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

namespace DPSG_DACK
{
    public class Employee
    {
        static string EmpFName,  EmpLName, Department;
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string department { get; set; }
        public string getName()
        {
            return this.FirstName + " " + this.LastName +" You are working with "+this.department+" department";
        }
        public virtual double calculateSalary()
        {
            throw new NotImplementedException();   
         }
    }
    public class Permanent_Employee:Employee
    {
        public int annualsalry { get; set; }
        public override double calculateSalary()
        {
            return this.annualsalry / 12;

        }
    }
    public class contractual_employee:Employee
    {
        public int perdayRate { get; set; }
        public int NoOfdays { get; set; }
      
        public override double calculateSalary()
        {
            return this.perdayRate * this.NoOfdays;
        }

    }
    class Program
    {
        public static void Main(string[] args)
        {


            Permanent_Employee PEmpObj = new Permanent_Employee()
            {
                FirstName = "shiv",
                LastName = "Kumar",
                department = "IT",
                annualsalry=400000

            };
            Console.WriteLine("Hi MR." + PEmpObj.getName());
            Console.WriteLine("You current Month salry = " + PEmpObj.calculateSalary());
            Console.WriteLine("=============================================================================");
            contractual_employee CempObj = new contractual_employee()
            {
                FirstName = "deepak",
                LastName = "Bhati",
                department = "HR",
                perdayRate = 200,
                NoOfdays=20
            };

            Console.WriteLine("Hi MR." + CempObj.getName());
            Console.WriteLine("You current Month salry = " + CempObj.calculateSalary());
            Console.ReadLine();
        }
    }
}



The above code working fine, but in this code user have rights to create the object of base class Employee and access the calculateSalary() method , if you see the code base class method throw an error

public virtual double calculateSalary()
        {
            throw new NotImplementedException();   
         }


            Employee objEmp = new Employee();
            objEmp.calculateSalary();



To avoid this, we use abstract class by the definition
An Abstract class is never intended to be instantiated directly.”

  
public abstract class Employee
    {
        static string EmpFName,  EmpLName, Department;
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string department { get; set; }
        public string getName()
        {
            return this.FirstName + " " + this.LastName +" You are working with "+this.department+" department";
        }
        public abstract double calculateSalary();
     
    }


==================================================
It means the place where we want, our base class method must define in the child class that inherit a base class, or

No one can create the object of base class method directly we use abstract classes

When don’t want the base class to be instantiated 
========================================================


“In Interface we can’t create a filed in base class ” that’s why we use abstract class for best result






Hope this article is use full for you J
Thanks
Shiv Kumar Rawat

Share:

Try and catch block Interview questions


Try and catch block


Difference btw throw end throw ex.

I am writing this post someone asked to me and I never heard earlier before.
I thought both are same, and one of them give an error, but both are wrong,
Now I have try to give you a proper answer


Similarities:
Let’s first see what is similar in both.

  • Both are used to throw exception in catch block to log message
  • Both contains same message of exception

Difference:
Now let’s see what is difference.

·         throw is used to throw current exception while throw(ex) mostly used to create a wrapper of exception.
  • throw(ex) will reset your stack trace
                            I.        so error will appear from the line where throw(ex) written
                         II.        while throw does not reset stack trace and you will get information about original exception.
  • In MSIL code when you use throw(ex) it will generate code as throw and if you use throw it will create re throw.





Notes: throw will get information about original exception point line no 13 as well as line where throw ex written







Can we have multiple try catch block

Yes, we can write multiple try catch block, but
·         if you write main catch (Exception ex) at the top the in .net other you cannot write it’s show a compile time error to you
·         Every try has at least one catch block



Can we have multiple try block

Yes, we can write multiple try block, but
·         Every try has at least one catch block



Can we skip catch block after try.

No , see above question it will help you  

Can we have same exception class

No, see the below image it will help you 



What happen if we have multiple catch blocks in a class




What happen if we write a finally block after catch blocks in a class









Download PDF
Share:

Popular

Tags

Mobile

Recent Posts