- An abstract class can contain abstract and non-abstract member.
- An abstract class can be used as a base class
- An abstract method does not have body/definition.
- An abstract class cannot be marked as a sealed class.
- We can not create an object of abstract class.
- We can create abstract class and method by using "abstract" keyword.
- We can not provide the static keyword to the abstract method.
- Once we inherit an abstract class in derived class then we must provide definition to the abstract member.
- We can implement the abstract method by using “override” keyword.
Syntax: Creating abstract class.
<modifier> abstract class <class_name>
{
// member
}
Syntax: Creating the abstract method.
<modifier> abstract return_type <method_name>(<parameter_list>);
Example: Simple example to understand the basic concept of abstract class and abstract method.
using System;
namespace AbstractClassProject
{
public abstract class DiscountedCustomer
{
public int CustId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Price { get; set; }
public int Discount { get; set; }
public string GetCustomer() // Non Abstract Method
{
return this.FirstName + " " + this.LastName;
}
public abstract int CalculateDiscount(); // Abstract Method
}
public class Customer:DiscountedCustomer
{
public override int CalculateDiscount()
{
return this.Price * this.Discount/100;
}
}
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer() {
CustId=101,
FirstName="Mauli",
LastName ="Gadewar",
Price = 500,
Discount =20
};
Console.WriteLine("Customer Full Name is {0}", customer.GetCustomer());
Console.WriteLine("Total Price is {0}", customer.Price);
Console.WriteLine("Customer Discount Rs is {0} ", customer.CalculateDiscount());
Console.ReadLine();
}
}
}
More information please watch video as follow:
0 comments:
Post a Comment