HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

Abstract Classes and Interfaces
In the example below:
  • If an abstract class inherits from an interface, the abstract class has to provide implementations for all the interface methods. In our example Class1 is inheriting from interface "Inter". So Class1 has to provide implementation for InterMethod1().
  • Method1() in Class1 is an abstract method. MainClass is inheriting from Class1 and provided the implementation for Method1(). So, as there are no more interface members or abstract methods that are not implemented we can create an instance of the MainClass and invoke the respective methods.

using System;

interface Inter

{

    void InterMethod1();

}

abstract class Class1 : Inter

{

    public void InterMethod1()

    {

        Console.WriteLine("InterMethod1 Implemented");

    }

    public abstract void Method1();

}

class MainClass : Class1

{

    //MainClass implementing inherited abstract method

    public override void Method1()

    {

        Console.WriteLine("Method1 Implemented");

    }

    public static void Main()

    {

        MainClass MC = new MainClass();

        MC.Method1();

        MC.InterMethod1();

    }

}
Abstract classes and Properties
Abstract classes can also have abstract properties. Abstract properties are properties that can have get and set accessors declarations without body.

In the example below:
  • Class1 has 2 abstract properties Property1 and Property2.
  • Property1 has only the get accessor. This means Property1 is readonly and any class inheriting from Class1 should provide implementation only for the get accessor. It is a compile time error to provide implementation for the set accessor for Property1.
  • Property2 is also an abstract class that has both set and get accessors. A class inheriting from class1 should provide implementations for both get and set accessors. Property2 is a read write property.

using System;

abstract class Class1

{

    public int Number;

    public abstract string Property1

    {

        get;

    }

    public abstract int Property2

    {

        get;

        set;

    }

}

class MainClass : Class1

{

    public override string Property1

    {

        get

        {

            return "Prasad Cherukuri";

        }

        //set

        //{

            //Error, Should not provide implementation for set accessor as the

            //property is declared as read only in the base abstract class

        //}

    }

    public override int Property2

    {

        get

        {

            return Number;

        }

        set

        {

            Number = value;

        }

    }

    public static void Main()

    {

        MainClass MC = new MainClass();

        Console.WriteLine(MC.Property1);

        MC.Property2 = 100;

        Console.WriteLine(MC.Property2.ToString());

    }

}

Further Reading
Other related articles

Njoy Programming
   ByPrasad Cherukuri