Why abstract is required in C#

Posted by Rajesh081725 under C# on 5/25/2017 | Points: 10 | Views : 400 | Status : [Member] | Replies : 5
Why abstract is required in C# provide real time example.

Which situation is particular required abstract class in real time project?

Time is Gold
Thanks & Regards,
Rajesh Kumar,
9962038582.



Responses

Posted by: A2H on: 5/25/2017 [Member] [MVP] Silver | Points: 25

Up
0
Down
One real time scenario is creating a baseclass for aspx page. Consider if you have a common method which you want to share across all pages in your application. Instead of duplicating the code at multiple locations you can create absract base class and then add it your page using inheritance like below


public partial class SamplePage : DemoBasePage


Thanks,
A2H
My Blog

Rajesh081725, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajesh081725 on: 6/19/2017 [Member] Bronze | Points: 25

Up
0
Down
But every derived class should be implementing the function. so this is not one location function implement,we can access it.

Abstract class dont have rights to implement function.

Time is Gold
Thanks & Regards,
Rajesh Kumar,
9962038582.

Rajesh081725, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Morganfowler on: 6/30/2017 [Member] Starter | Points: 25

Up
0
Down
Abstract training are instructions that comprise one or more summary strategies. An summary method is a technique this is declared, but carries no implementation. Abstract training won't be instantiated, and require subclasses to offer implementations for the abstract strategies. https://www.ukassignmentshelp.co.uk/law-assignment-writing.php

Rajesh081725, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shreedar on: 9/19/2017 [Member] Starter | Points: 25

Up
0
Down
Hi

When you want a method to be must and should implemented in every child class, then we declare that method as abstract method. If you have a abstract method in a class then the class should be declared as abstract class.
In real time for example we need to calculate monthly salaries for employees, employees are of three types
1. Contract Employee
2. Permanent Employee
3. Part time Employee
Here we have to calculate salary for three of them. So we need one common method to calculate salary based on parameters passed.
We declare a abstract method calculate like below. By declaring method as abstract it will make sure every child class has implementation for calculate method since we must and should calculate salary for all types of employees.
If we don't provide implementation for calculate method in any one of child class it gives you error.

Public abstract class Employee
{
Public abstract calculate();

}
public class Contract:Employee
{
public void calculate()
{
//calculation logic for contract employee
}
}

public class Permanent:Employee
{
public void calculate()
{
//calculation logic for Permanent employee
}
}

public class Parttime:Employee
{
public void calculate()
{
//calculation logic for Parttime employee
}
}


Regards

Sridhar Thota.
www.dotnet-sridhar.blogspot.com

Rajesh081725, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amatya on: 9/19/2017 [Member] Silver | Points: 25

Up
0
Down
I think I can understand your question in more practical way, as I also got the same doubt before 2 year.
Hi Rajesh Kuma r ,
Abstract class can be used in this particular real time Project.
You can take an example of Mobile Company Redmi.
Now they have launched Redmi1, 2,3,4 ....Right?
In all the mobile they have Call and Message functionality
but the Size of mobile, Storage and Camera functionality is not common.
So Call and Message method will be common to all the Redmi phones (i.e Redmi 1, Redmi 2, Redmi 3S, redmi 4, 4A) but the Size of mobile, Storage and Camera functionality will not be shared.
So, size of mobile, Storage and Camera functionality method we can make abstract method.
Therefore in abstract class you will have abstract method (related to size of mobile, Storage and Camera) and non abstract methods (Call, Message)

Hope I have cleared your doubts . :)

Thanks & Regards,
Amatya

Feel free to share informations 9731764134
mail Id ' adityagupta200@gmail.com
Thanks

Rajesh081725, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response