Abstract modifiers in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractDetails
{
/// <summary>
/// Abstract:
/// Abstract modifiers indicates that the things being
modified has missing or incomplete implementation.
/// Abstract modifiers can be used with classes, indexex,
methods, properties and events.
/// we can use abstarct modifier in class declaration to
indicates that the class is intended to be base
/// class for all other classes.
/// member mared as abstract or included in an abstract class
MUST BE implemented by a class
/// that derives from the abstract class.
/// Abstract class implements interface but in this case all
members of interface MUST BE
/// implemented in abstract class.
///
/// 1. We Cannot create an instance of the abstract class or
interface
/// 2. We cannot use Sealed modifier for abstract class
because both the modifiers have opposite meaning
/// Sealed modifier
prevents the class from being inherited where as abstract modifiers requires
/// a class to be
inherited.
/// 3. abstract class may contains abstract as well as non
abstract methods.
/// 4. abstract methods are implicitly virtual methods.
/// 5. If class is derived from abstract class then abstract
methods must be implemted by that
/// derived class.
/// 6. Abstract methods simply ends with semicolon (;) and
does not includes curly braces ({}).
/// It does not
contains method body.
/// 7. We can not use Static or Virtual modifier with
Abstract Modifier.
/// 8. It is an error to include abstract methods in Non
Abstract class.
/// 9. One abstract class inherits another abstract class.
/// 10.We can not use abstract modifiers with Data members
like public abstract int I; // Will throw error.
/// 11.If base and Derived both the classes are abstract
classes then it is not mandatory
/// to implement
base class methods in derived class.
/// 13.It is an error to use Abstract method in non abstract
class. so below line gives error.
/// so commented.
/// Error:1
'AbstractDetails.NonAbstractPersonClass.SetPerson()' is abstract but it is
/// contained in
non-abstract class 'AbstractDetails.NonAbstractPersonClass'
/// </summary>
public class NonAbstractPersonClass
{
//public
abstract void SetPerson();
public void GetPerson()
{
Console.WriteLine("Print
Person");
}
}
public abstract class AbstractPersonClass
{
public abstract void SetPerson();
public void GetPerson()
{
Console.WriteLine("Print
Person");
}
}
//****************////****************////****************////****************////****************////****************//
//
Examples 1
//****************////****************////****************////****************////****************////****************//
public abstract class Person
{
public abstract string PrintFirstName();
public string PrintLastName()
{
return "Printed Last
Name";
}
}
public class OutOut : Person
{
public override string PrintFirstName()
{
return "Printed First
Name";
}
}
//****************////****************////****************////****************////****************////****************//
//
Examples 2 : Both the classes are abstract so not needed to implement base
class methods. Person is abstract class
//****************////****************////****************////****************////****************////****************//
public abstract class Student : Person
{
}
//****************////****************////****************////****************////****************////****************//
//
Examples 3 : Abstract class can also inherits non abstract class
//****************////****************////****************////****************////****************////****************//
public class NonAbstractClass
{
}
public abstract class AbstractClass : NonAbstractClass
{
}
//****************////****************////****************////****************////****************////****************//
//
Examples 4 : abstract class with static modifier
// Error
is :'AbstractDetails.StaticAbstractClass': an abstract class cannot be sealed
or static
// So
commented below class
//****************////****************////****************////****************////****************////****************//
//public static abstract class StaticAbstractClass
//{
//}
//****************////****************////****************////****************////****************////****************//
//
Examples 5 : Sample example. Working Example
//****************////****************////****************////****************////****************////****************//
abstract class Father
{
public Father()
{
Console.WriteLine("Constructor
of base class which is abstract");
}
public abstract void PrintFirstName(string FirstName);
public void PrintLastName(string LastName)
{
Console.WriteLine(LastName);
}
}
class Child : Father
{
public Child()
{
Console.WriteLine("Constructor
of derived class which is NOT abstract");
}
public override void PrintFirstName(string FirstName)
{
Console.WriteLine(FirstName);
}
}
//****************////****************////****************////****************////****************////****************//
//
Examples 6 : Sample example
//****************////****************////****************////****************////****************////****************//
public abstract class A
{
public abstract void A_Method1();
}
public class B : A
{
public override void A_Method1()
{
Console.WriteLine("Direct
derived from abstract class method");
}
}
public class C : B
{
public override void A_Method1()
{
base.A_Method1();
Console.WriteLine("Derived from
non abstract class method");
}
}
class Program
{
static void Main(string[] args)
{
Child childObject = new Child();
childObject.PrintFirstName("Sameer");
childObject.PrintLastName("Shinde");
C CObject = new C();
B BObject = new B();
BObject.A_Method1();
CObject.A_Method1();
Console.Clear();
Swift f = new Swift();
f.PrintValue();
f.PrintValueOfA(5);
Maruti m = new Swift();
m.PrintValue();
m.PrintValueOfA(8);
Console.ReadLine();
}
}
public abstract class Maruti
{
public abstract void PrintValue();
public void PrintValueOfA(int a)
{
Console.WriteLine("Entered value
is " + a);
}
}
public class Swift : Maruti
{
public override void PrintValue()
{
Console.WriteLine("Called Print
Value");
}
}
}