You must Sign In to post a response.
  • Category: .NET

    Abstract class V/S interfaces, which is best for application

    Hi Friends,

    I have a question: if requirement in my project keep on changing daily or /weekly basis then what should be best to use in the application either Abstract class or Interfaces.
    If possible please elaborate with the help of example.

    Thanks,
    Rakesh
  • #765247
    According to OOPs, interfaces are implicitly abstract, abstract class can have instance methods while interface is by default final. Abstract classes cannot be instantiated, but they can be subclassed whereas abstract methods in its parent class.
    Code snippet for interface
     interface Vehicle
    {
    void run();

    int getFuel();
    }

    class Car implements Vehicle
    {

    int fuel;

    void run()
    {
    print("good status");
    }


    int getFuel()
    {
    return this.fuel;
    }
    }


    Code snippet for i Abstract classes

    abstract class Vehicle
    {

    int fuel;


    int getFuel()
    {
    return this.fuel;
    }
    abstract void run();


    }

    class Car extends Vehicle
    {
    void run()
    {
    print(" Best Status");
    }
    }

  • #765262
    Hi,
    Its totally depends on your requirement, here are some of the points to be noted:
    1. If you are implementing a smaller functionality, use interfaces but use abstract class in case of larger applications.
    2. If you want to implement a common functionality throughout the application, use an abstract class. It allows you to easily implement your class/functionality, whereas interface does not allow implementation.
    3. If you are implementing a functionality which will be useful across a whole application and to be compatible with large number of different-different types of objects, use interface. Abstract classes must be used for objects which are of same type.
    4. In abstract classes, just change the base class, change will be reflected in all inheriting classes automatically i.e. less code changes. Whereas in interfaces, you have to create a new interface each time.

    Eg. of interfaces:

    Cricketers who are also Sportsman's, so we can implement interfaces for them as follows:

    public interface Cricketer{ Matches ODI(int iNoOfODIPlayed); }

    public interface Sportsman{ Career Sports(string szTypeofSport); }

    public interface CricketerSportsman extends Cricketer, Sportsman{...}

    Eg. of abstract class:

    Match is an abstract class, with Score() method. You can create ODI, T20, Test as its derived classed and call that method. Or you can make that method abstract and implement it inside each derived class.

    using System;
    namespace abstractDemo
    {
    abstract class Match
    {
    public int Score(int iRuns)
    {
    return iRuns + 50;
    }
    }

    //Child Class of Match
    class ODI:Match
    {
    [STAThread]
    static void Main(string[] args)
    {
    ODI objODI = new ODI();
    int Total = objODI.Score(353);
    Console.WriteLine("Total score is : "+ Total);
    }

    }
    }
    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com

  • #765266
    Hi,

    My suggestion is Interfaces, if you want to keep changes the requirement frequently the best approach is interfaces.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments