+1 vote
in C Sharp by
What is an Abstract class in C#?

1 Answer

0 votes
by

An Abstract class is a special class which is majorly used for inheritance and it cannot be instantiated.

•      Cannot be instantiated i.e. object cannot be created using the new keyword.

•      Can contain both Abstract and Non-Abstract members.

•      Abstract members are simply declared and are defined in the classes deriving the Abstract class.

•      Abstract members are defined by using the override keyword.

•      Non-Abstract members are defined within the Abstract class.

•      Non-Abstract members can be accessed within the derived classes only if marked public or protected.

•      Private Non-Abstract members are not accessible outside the Abstract class.

•      Abstract and Non-Abstract members can be accessed using the derived classes.

•      Does not support Multiple Inheritance.

Example:public abstract class A

{

    public abstract void Fun1();

    public void Fun4()

    {

    }

    protected void Fun5()

    {

    }    

private void Fun6()

    {

    }

}

public class B : A

{

    public override void Fun1()

    {

    }

    public void Fun2()

    {

        //Valid

        Fun4();

        Fun5();

 

        //Compiler Error: Cannot create an instance of the abstract class or interface

        Fun6();

    }

}

public class C

{

    public static void Fun3()

    {

        //Compiler Error: Cannot create an instance of the abstract class or interface 'A'

        A a = new A();      //Valid

        B b = new B();

        b.Fun1();

        b.Fun2();        b.Fun4();

    }

}

Related questions

+1 vote
0 votes
asked Jun 16, 2020 in C Sharp by Hodge
+1 vote
+1 vote
asked Oct 18, 2019 in C Sharp by Robin
...