Wednesday, May 23, 2018

When To Use Abstract Class In Real Projects



Note: Most of the interview persons ask What is the Use Of Abstract Class and Interface.

Few people may go indepth and ask you that can we do with the same with Class and what is the use of Abstract Class and Interface.

So with this article I am going to explain , what will happened exactly if we use Class and when to use interface and Abstract


Abstract Class An abstract class is a special class that contains both abstract and non-abstract members in it.

Abstract Method
A method without any particular body is known as an abstract method. These methods contain only declaration of the method.To declare an abstract method, we have to use abstract modifier on the method. The implementation of abstract method is done by a derived class. When the abstract class inherits the derived class, the derived class must implement the abstract methods using override keyword in it.


namespace ConsoleApplication2
{
    public abstract class Bikes
    {
        //Abstract Methods 
        public abstract double priceOfTheBike();
        public abstract int getTotalSeatCount();

    }
}

Now, I have  class like Shine and  Unicorn which are derived from a parent class called Bike. Here, I have the Bike methods as follow:


public class Bike
    {
        public string Wheel()
        {
            return "2 wheeler";

        }
        public string Disc()
        {
            return "Disc Facility Available";
        }
        public string FiveGearSystem()
        {
            return "Five Gear System";
        }
    }

public class Shine : Bike
    {
        static void Main(string[] args)
        {
            Shine objShine = new Shine();

            Console.WriteLine(objShine.Desc());
            Console.WriteLine(objShine.Wheel());
            Console.WriteLine(objShine.FiveGearSystem());
            Console.ReadLine();
        }
    }


Now I am going to Add one more class Unicorn inheriting Bike Class
   public class Unicorn : Bike
    {
        static void Main(string[] args)
        {
            Unicorn objUnicorn = new Unicorn();

            Console.WriteLine(objUnicorn.Desc());
            Console.WriteLine(objUnicorn.Wheel());
            Console.WriteLine(objUnicorn.FiveGearSystem());
            Console.ReadLine();
        }
    }


If we are taking class, then we can only write normal methods having common implementation (i.e I mean Bike Class )there. But, this will not satisfy our requirement because we need separate implementations in both the classes(Shine and Unicorn Classes). Thus, we will go forward by using Class and Interface

Now  what we will do is have common methods in the Interface and Inherits that interface as shown below. So to achieve with class Now, let me first inherit the Unicorn class from Bike class and IExtra interface class to achieve our goal.

The code is given below.


Example and Output:
namespace ConsoleApplication2
{
    
    public interface IExtra
    {
        double price();
        string colors();
    }

    public class Bike
    {
        public string Wheel()
        {
            return "2 wheeler";
        }
        public string Desc()
        {
            return "Desc Facility Available";
        }
        public string FiveGearSystem()
        {
            return "Five Gear System";
        }
    }

    public class Unicorn : Bike,IExtra
    {
        public string DiscountPrice()
        {
            return "20% discount on buying Unicorn Bike";
        }
       
        static void Main(string[] args)
        {
            Unicorn objUnicorn = new Unicorn();
            Console.WriteLine("Below are the Common properties defined in the Bike Class");
            Console.WriteLine(objUnicorn.Desc());
            Console.WriteLine(objUnicorn.Wheel());
            Console.WriteLine(objUnicorn.FiveGearSystem());
            Console.WriteLine("Below are the Common methods but implementation is Different");
            Console.WriteLine("Total ONRoad Price:" + objUnicorn.price());
            Console.WriteLine(objUnicorn.colors());
            Console.WriteLine("Below is the Own Method defined in the Unicorn Class");
            Console.WriteLine(objUnicorn.DiscountPrice());
            Console.ReadLine();
        }

        public string colors()
        {
            return "Black with Gray Strips";
        }

        public double price()
        {
            return 60000;
        }
    }
}

Output:


Picotrial Way:

Note: Now you can answer, the interview question if any one ask , you why cant we do  with Classes.

Example :



public class Bike
    {
        public string Wheel()
        {
            return "2 wheeler";
        }
        public string PetrolVehicle()
        {
            return "This is Petrol Vehicle";
        }
    }

    public class Unicorn : Bike

    {
        public string DiscountPrice()
        {
            return "20% discount on buying Unicorn Bike";
        }
        
        static void Main(string[] args)
        {
            Unicorn objUnicorn = new Unicorn();
            Console.WriteLine(objUnicorn.PetrolVehicle());
            Console.WriteLine(objUnicorn.Wheel());
            Console.WriteLine(objUnicorn.DiscountPrice());
            Console.ReadLine();
        }

    }


Here we know only  the Unicorn class, its methods, and properties. The user can access the Bike class property and method by creating the object of Unicorn class because it is a child class.

Now, the main drawback of this kind of Implementation is that it allows the user to create the object of the Parent class (which the user should not be aware of and this is not his strategy). Let's see what happens when the user creates the object.




From the above example it is very clear that we are not able to create the access the child method.



Now this is very simple to solve with Abstract Class, Now we will see how to solve this with Abstract Class, even if the user will create an object of Parent class, we need to check them because using that object, he can't able to access the child class method.


So, the main problem we face here is,

As the user is unaware of parent class, he is able to create the object of parent class and unable to access the child class method using parent class. So, to restrict this, we have to do the following things-

We should not give the permission to create the object of parent class in child class


We can only allow the user to create the child class object to access both parent class methods and child class methods.

Simple to do this

Now i am going to create a Abstract Class, and when i am going to create a Object of Abstract Class, i will get an Error as Shown Below




Second Example

Create a Abstract Class as Bike.

Add all the common functionality in simple methods and all the methods whose implementation is different but name is same. Make them Abstract method.








Example and output:

namespace ConsoleApplication2
{
    public abstract class Bike
    {
        //put all the common functions but different implementation in  abstract method. 
        public abstract double price();
        public abstract string colors();

        public string Wheel()
        {
            return "2 wheeler";
        }

        public string Desc()
        {
            return "Desc Facility Available";
        }

        public string FiveGearSystem()
        {
            return "Five Gear System";
        }
    }

    public class Unicorn : Bike
    {
        public string DiscountPrice()
        {
            return "20% discount on buying Unicorn Bike";
        }
       
        static void Main(string[] args)
        {
            Unicorn objUnicorn = new Unicorn();
            Console.WriteLine("Below are the Common properties defined in the Bike Class");
            Console.WriteLine(objUnicorn.Desc());
            Console.WriteLine(objUnicorn.Wheel());
            Console.WriteLine(objUnicorn.FiveGearSystem());
            Console.WriteLine("Below are the Common methods but implementation is Different");
            Console.WriteLine("Total ONRoad Price:" + objUnicorn.price());
            Console.WriteLine(objUnicorn.colors());
            Console.WriteLine("Below is the Own Method defined in the Unicorn Class");
            Console.WriteLine(objUnicorn.DiscountPrice());
            Console.ReadLine();
        }

        public override string colors()
        {
            return "Black with Gray Strips";
        }

        public override double price()
        {
            return 60000;
        }
    }
}


Output :


Finally the Result is 




No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...