Abstract
Abstract is one of the principles of Object-Oriented Programming. it is one of the pillar object-oriented programs. what is necessary and encapsulates the unnecessary things outside the world. i.e showing the necessity of the other class and hide the unnecessary thing.
Abstract is a process of hiding the implementation details from the user.
Example: if you buy a mobile you just know the feature of mobile like what px camera is there and what processor is there etc. but you don’t need to know how internally all the parts are working. here the mobile is an object which is designed to expose only required features by hiding its implementation details.
Note :
- which necessary method and properties are showing to the user/other class is Abstraction.
- which data is not showing to the user/other class is encapsulation but encapsulation many more things there we will discuss later.
class Employee
{
public int EmpId;
public string Empname;
public double Grosspay;
public double netSal;
double taxDeducation=0.1;
public Employee(int Eid,string Ename,double Egrosspay)
{
this.EmpId = Eid;
this.Empname = Ename;
this.Grosspay = Egrosspay;
}
//unscesary details which are hiding the other class
void CalculateSalary()
{
if (Grosspay >=30000)
{
netSal = Grosspay - (taxDeducation * Grosspay);
Console.WriteLine(netSal);
}
else
{
Console.WriteLine(Grosspay);
}
}
//only call the method not showing implementation details
public void ShowEmployeeSalary()
{
this.CalculateSalary();
}
}
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee(101,"Csharp-tutorial-point",32000);
//not showing the CalculateSalary() method hiding the //implementation details but we can access the functionality of
//that method using ShowEmployeeSalary()
emp.ShowEmployeeSalary();
Console.ReadLine();
}
}
Abstract :
- Abstract is a keyword.
- Abstract class allows you to create functionality that subclasses can implement or override.
- The abstract modifier can be used with classes, methods, properties, indexers, and events.
- Abstract is "to represent the essential feature without representing the background details".
- Abstraction lets you focus on what object does instead of how it does it. as we see over the example .
Classes can be declared as abstract by putting the keyword abstract before the class definition.
For example:
public abstract class Program
{
}
1. Abstract class can contain abstract members and non- abstract members .
2. A class can only inherit from one abstract Class.does not support multiple Inherit.
3. We cannot create object of an abstract class.
4. Abstract class can has access modifiers like private, protected, internal with class members.
5. Abstract class can has instance variables (like constants and fields).
6. An abstract class can has constructors and destructor.
Abstract members:
1. It must be a function member. That is, fields and constants cannot be abstract members.
2. It must be marked with the abstract modifier.
3. abstract members cannot have private access modifier
4. Abstract method declared with an
abstract
modifier. it doesn't contain body. public abstract void Method1();
5. Abstract methods are internally treated as virtual methods so those methods need to be overridden in the derived class.
6. It is an error to use the static or virtual modifiers in an abstract method declaration.
namespace Abstraction
{
abstract class Parent
{
public abstract void m1();
}
class Child : Parent
{
public override void m1()
{
Console.WriteLine("Csharp-tutorial-point");
}
}
class Program
{
static void Main(string[] args)
{
Child c1 = new Child();
c1.m1();
Console.ReadLine();
}
}
}
IMPORTANT POINT
Can abstract class have constructors in C#?
Yes, abstract class can have constructor in C#.
Can abstract class be Sealed in C#?
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.
Can we declare abstract methods as private in C# ?
No. Abstract methods can not be private in C#.
Can abstract class contain main method in C#?
Yes, Abstract class can contain main method in C#.
Does Abstract class support multiple Inheritance?
No, Abstract class does not support multiple Inheritance.
Abstract class must have only abstract methods. True or false?
False. Abstract methods can have both abstract and non abstract methods.