locked
Interface vs Abstract class

    Question

  • Greets!

     

    From my reading:

     

    If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method.


    If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.

     

    In the following code snippet, a new method is added in the abstract class with default implementation (Am I right?).

     

    How about adding a new abstract method in the abstract class? Is it possible to provide a default implementation of it?

     

    Thanks.

     

    using System;

    abstract class BaseClass
    {
       public abstract void myMethod();

       public void newMethod()
       {
           Console.WriteLine("This is new method");
       }
    }

    class DerivedClass1 : BaseClass
    {
        public override void myMethod()
        {
            Console.WriteLine("This is abstract method overridden in DerivedClass1");
        }
    }

    class DerivedClass2 : BaseClass
    {
        public override void myMethod()
        {
            Console.WriteLine("This is abstract method overridden in DerivedClass2");
        }
    }

    class MainClass
    {
        static void Main()
        {
            BaseClass bc = new DerivedClass1();
            bc.myMethod();

            bc = new DerivedClass2();
            bc.myMethod();
        }
    }

    Thursday, August 30, 2007 5:32 AM

Answers

  • Hi,

     

    You cannot give default implementation for abstract method in an abstract class, the classes derived from the abstract class should give the implementation of the abstract method.

     

    Example:

    Code Snippet

    public abstract class Vehicle

    {

    public abstract void StartEngine();

    }

     

    public class Car : Vehicle

    {

    public void StartEngine()

    {

    //Implementation of StartEngine

    }

    }

     

     

     

     

    Thursday, August 30, 2007 6:25 AM
  • Well, an abstract method with a default implementation would no longer be abstract. What you are looking for is merely a virtual method. The content of the virtual method is the "default", and child classes can do an override of the method if they want to replace the default.

     

    using System;

    abstract class BaseClass
    {
       public abstract void myMethod();

       public virtual void newMethod()
       {
           Console.WriteLine("This is new method");
       }
    }

    class DerivedClass1 : BaseClass
    {
        public override void myMethod()
        {
            Console.WriteLine("This is abstract method overridden in DerivedClass1");
        }
        public override void newMethod()
        {
            Console.WriteLine("This is virtual method overridden in DerivedClass1");
        }
    }

    class DerivedClass2 : BaseClass
    {
        public override void myMethod()
        {
            Console.WriteLine("This is abstract method overridden in DerivedClass2");
        }

       //Note newMethod will have "default" implementation
    }

    class MainClass
    {
        static void Main()
        {
            BaseClass bc = new DerivedClass1();
            bc.myMethod();

            bc.newMethod();

            bc = new DerivedClass2();
            bc.myMethod();

            bc.newMethod();

        }
    }

    Thursday, August 30, 2007 6:47 AM
  • Note that abstract classes can contain non-abstract members, i.e. you can provide a default implementation as Alberto has shown.

     

    Remember that a type can inherit from at most one other type. This limits the situations where inheritance can be used.

     

    And interfaces have the advantage that members can be declared explicitly in the implementing type, which can be very useful at times.

     

    I use a combination of abstract classes and interfaces. Sometimes I have my abstract base class implement the interfaces with either abstract or concrete members. To some extent this gives me the best of both worlds, although it's not a perfect solution as in some scenarios you have the worst of both worlds.

     

    Sean

    Thursday, August 30, 2007 12:04 PM
  •  Satiz wrote:

    Hi,

     

    You cannot give default implementation for abstract method in an abstract class, the classes derived from the abstract class should give the implementation of the abstract method.

     

    Example:

    Code Snippet

    public abstract class Vehicle

    {

    public abstract void StartEngine();

    }

     

    public class Car : Vehicle

    {

    public void StartEngine()

    {

    //Implementation of StartEngine

    }

    }

     

     

    You can't have a default implementation for an abstract method in an abstract class, but you can have a virtual method in an abstract class (which must have an implementation), which would do what the OP wants.
    Thursday, August 30, 2007 2:03 PM

All replies

  • Hi,

     

    You cannot give default implementation for abstract method in an abstract class, the classes derived from the abstract class should give the implementation of the abstract method.

     

    Example:

    Code Snippet

    public abstract class Vehicle

    {

    public abstract void StartEngine();

    }

     

    public class Car : Vehicle

    {

    public void StartEngine()

    {

    //Implementation of StartEngine

    }

    }

     

     

     

     

    Thursday, August 30, 2007 6:25 AM
  • Well, an abstract method with a default implementation would no longer be abstract. What you are looking for is merely a virtual method. The content of the virtual method is the "default", and child classes can do an override of the method if they want to replace the default.

     

    using System;

    abstract class BaseClass
    {
       public abstract void myMethod();

       public virtual void newMethod()
       {
           Console.WriteLine("This is new method");
       }
    }

    class DerivedClass1 : BaseClass
    {
        public override void myMethod()
        {
            Console.WriteLine("This is abstract method overridden in DerivedClass1");
        }
        public override void newMethod()
        {
            Console.WriteLine("This is virtual method overridden in DerivedClass1");
        }
    }

    class DerivedClass2 : BaseClass
    {
        public override void myMethod()
        {
            Console.WriteLine("This is abstract method overridden in DerivedClass2");
        }

       //Note newMethod will have "default" implementation
    }

    class MainClass
    {
        static void Main()
        {
            BaseClass bc = new DerivedClass1();
            bc.myMethod();

            bc.newMethod();

            bc = new DerivedClass2();
            bc.myMethod();

            bc.newMethod();

        }
    }

    Thursday, August 30, 2007 6:47 AM
  • Note that abstract classes can contain non-abstract members, i.e. you can provide a default implementation as Alberto has shown.

     

    Remember that a type can inherit from at most one other type. This limits the situations where inheritance can be used.

     

    And interfaces have the advantage that members can be declared explicitly in the implementing type, which can be very useful at times.

     

    I use a combination of abstract classes and interfaces. Sometimes I have my abstract base class implement the interfaces with either abstract or concrete members. To some extent this gives me the best of both worlds, although it's not a perfect solution as in some scenarios you have the worst of both worlds.

     

    Sean

    Thursday, August 30, 2007 12:04 PM
  •  Satiz wrote:

    Hi,

     

    You cannot give default implementation for abstract method in an abstract class, the classes derived from the abstract class should give the implementation of the abstract method.

     

    Example:

    Code Snippet

    public abstract class Vehicle

    {

    public abstract void StartEngine();

    }

     

    public class Car : Vehicle

    {

    public void StartEngine()

    {

    //Implementation of StartEngine

    }

    }

     

     

    You can't have a default implementation for an abstract method in an abstract class, but you can have a virtual method in an abstract class (which must have an implementation), which would do what the OP wants.
    Thursday, August 30, 2007 2:03 PM