Abstract
Questions-
When should we use Abstract in programming?
Real world example of abstract in C# programming?
Concept of Abstract keywords in Programming?
How to use abstract in real world programming ?
Programming without abstract ?
When should we use Abstract in programming?
Real world example of abstract in C# programming?
Concept of Abstract keywords in Programming?
How to use abstract in real world programming ?
Programming without abstract ?
Example in C# Programming
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
namespace
AbstractInterface_Concept
{
abstract
class PersonaInformation
{
public string CompanyName;
public string EmployeeID ;
public string Location;
public string First_Name;
public string Last_Name;
public DateTime dataOfBirth;
public string bloodGroup =
"AB+";
public abstract
string GenerateIDCard();
}
class CreateIDCard:PersonaInformation
{
public override string GenerateIDCard()
{
return $" {CompanyName} \n
{EmployeeID} \n {First_Name} {Last_Name}";
}
}
class Program
{
static void Main(string[] args)
{
//Pass the value and create any one
ID within the company
CreateIDCard createIDCard = new
CreateIDCard();
createIDCard.CompanyName =
"ABC techonology";
createIDCard.EmployeeID =
"2244551";
createIDCard.Location =
"Pune";
createIDCard.First_Name =
"Sameer";
createIDCard.Last_Name =
"Bahad";
createIDCard.dataOfBirth =
Convert.ToDateTime("06/09/1990");
createIDCard.bloodGroup =
"AB+";
Console.WriteLine(createIDCard.GenerateIDCard());
Console.ReadLine();
}
}
}
ABC techonology
2244551
Sameer Bahad
Abstract Errors
1.Define abstract variable without get and set
abstract class PersonalInformation
{
public abstract string CompanyName;
}
//Compile Error-
//Compile Error-
//CS0681 The
modifier 'abstract' is not valid on fields. Try using a property instead.
//Use Property
abstract class PersonalInformation
{
public
abstract string CompanyName { get; set; }
}
class
CreateIDCard:PersonalInformation
{
public override
string CompanyName
{
get { return CompanyName;}
set {value = CompanyName;}
}
}
class Program
{
static void Main(string[] args)
{
//Pass the value and create any one
ID within the company
CreateIDCard createIDCard = new
CreateIDCard();
createIDCard.CompanyName =
"ABC techonology";
}
2. Can not create instance of abstract class
abstract class PersonalInformation
{
public abstract string CompanyName { get; set; }
}
class Program
{
static void Main(string[] args)
{
PersonalInformation
personalInformation = new PersonalInformation();
}
}
//Compile
Error-
//Error CS0144 Cannot
create an instance of the abstract class or interface 'PersonalInformation'
//Use
Inheritance
abstract class
PersonalInformation
{
public abstract string CompanyName {
get; set; }
}
class
CreateIDCard:PersonalInformation
{
public override string CompanyName
{
get { return CompanyName;}
set {value = CompanyName;}
}
}
class Program
{
static void Main(string[] args)
{
CreateIDCard
createIDCard = new CreateIDCard();
createIDCard.CompanyName = "ABC
techonology";
}
}
3. Abstract variable not implemented into derived class
abstract class PersonalInformation
{
public abstract string CompanyName {
get; set; }
}
class
CreateIDCard:PersonalInformation
{
//Not implemented company Name
}
class Program
{
static void Main(string[] args)
{
CreateIDCard
createIDCard = new CreateIDCard();
createIDCard.CompanyName = "ABC
techonology";
}
}
//Compile Error-
//Error CS0534 'CreateIDCard'
does not implement inherited abstract member //'PersonalInformation.CompanyName.get'
//Error CS0534 'CreateIDCard'
does not implement inherited abstract member //'PersonalInformation.CompanyName.set'
//Implement
Company Name into CreateIDCard Class
abstract class
PersonalInformation
{
public abstract string CompanyName {
get; set; }
}
class
CreateIDCard:PersonalInformation
{
public override string CompanyName
{
get { return CompanyName;}
set {value = CompanyName;}
}
}
class Program
{
static void Main(string[] args)
{
CreateIDCard
createIDCard = new CreateIDCard();
createIDCard.CompanyName = "ABC
techonology";
}
}
