Categories

Jan 20 in C Sharp (C#)
Q: What is an Abstract class in C#?

1 Answer

Jan 20

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();

    }

}

Click here to read more about C Sharp
Click here to read more about Insurance

Related questions

Madanswer
Jun 19, 2019 in C++
Jun 16 in C Sharp (C#)
Feb 18 in C Sharp (C#)
...