Abstract Class Vs Interface in C#
Jun 16, 2015 02:00 0 Comments C# (C-Sharp) SATYALA ABHISHEK

Abstract Class Vs Interface in C#

 

Abstract Class:

An abstract class is a way to achieve the abstraction in C#. An Abstract class is never intended to be instantiated directly. This class must contain at least one abstract method, which is marked by the keyword or modifier abstract in the class definition. The Abstract classes are typically used to define a base class in the class hierarchy.

 

using System;

  

// abstract class 'Abstraction'

public abstract class Abstraction  {

  

    // abstract method 'abstract Method()'

    public abstract void 'abstract Method ();

}

  

// class 'Abstraction' inherited in child class 'Child1'

public class Child1 : Abstraction {

  

    // abstract method 'abstract Method()' declared here with 'override' keyword

    public override void abstract Method()

    {

        Console.WriteLine("Class name is Child1");

    }

}

 

Output:

Class name is Child1

Class name is Child2

 

Text Box: Output:Class name is Child1Class name is Child2  

// class 'Abstraction' inherited in another child class 'Child 2'

public class Child2 : Abstraction {

  

    // same as the previous child class

    public override void abstract Method()

    {

        Console.WriteLine("Class name is  Child 2");

    }

}

  

// Main class

public class Program {

  

    // Main Method

    public static void Main(String[] args)

    {

  

        // Abstract class ‘Abstraction’ cannot be instantiated

        Abstraction obj;

  

        // instantiate class 'Child1'

        obj = new Child1();

  

        // call ' abstract Method()' of class ' Child1'

        obj. abstract Method();

  

        // instantiate class ' Child2'

        obj = new G2();

  

        // call ' abstract Method()' of class ' Child 2'

        obj. abstract Method();

    }

}

 

Interface:

Like a class, Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. The implementation of interface’s members will be given by the class who implements the interface implicitly or explicitly.

 

using System;

  

// A simple interface

interface interface 1 {

  

    // method having only declaration not definition

    void show();

}

  

// A class that implements the interface

 

Output:

Welcome to the world of C#

 

Text Box: Output:Welcome to the world of C#class Program : interface 1 {

  

    // providing the body part of function

    public void show()

    {

        Console.WriteLine("Welcome to the world of C#");

    }

  

    // Main Method

    public static void Main(String[] args)

    {

  

        // Creating object

        Program obj = new Program();

  

        // calling method

        obj.show();

    }

}

 

Abstract Class Vs Interface

ABSTRACT CLASS

INTERFACE

It contains both declaration and definition part.

It contains only declaration part.

Multiple inheritance is not achieved by abstract class.

Multiple inheritance is achieved by interface.

It contains a constructor.

It does not contain a constructor.

It can contain static members.

It does not contain static members.

It can contain different types of access modifiers like public, private, protected etc.

It only contains public access modifier because everything in the interface is public.

The performance of an abstract class is fast.

The performance of interface is slow because it requires time to search actual method in the corresponding class.

It is used to implement the core identity of class.

It is used to implement peripheral abilities of class.

A class can only use one abstract class.

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect