-1

Hi all i have some doubts about abstract class and interface

Am not asking about difference between interface and abstract class . Am just asking about different between abstract method and interface method

Abstract methods are same as interface methods . I know if we inherit the interface and abstract class in child class ,then we must implement the those side methods .But we can't implement the non abstract methods. So

  1. my question is what is the different between abstract method and interface ?

and

2". another question is we can partially implement the Non-abstract methods in abstract class , Is it possible to partially implement the abstract method in abstract class ?

I also referred many sites , but does not one give the solution for second question

Question with code

Here is my abstract class and have one abstract method(xxx) and another one non abstract moethod(yyy) and interface method (xxx)

public abstract class AbstractRam
{
    public abstract int xxx();// What is the difference in interface method ?

    public int yyy()
    {
        return 2;
    }
}

public interface InterfaceRam
{
    int xxx();// What is the difference in abstract method ?


}

and i inherited the both in another class

public class OrdinaryClass : AbstractRam
{
    public OrdinaryClass()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public override int xxx()
    {
        return 1;
    }
}

public class OrdinaryClass2 : InterfaceRam
{
    public OrdinaryClass2()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public int xxx()
    {
        return 1;
    }
}

Let see my xxx method , Both methodes are working same , Not a big difference

question : Is it have any difference ? if is it same , then which one is the best way ?

Community
  • 1
  • 1
Ramesh Rajendran
  • 35,211
  • 40
  • 143
  • 222

2 Answers2

1
  1. Interface methods are abstract methods. Yes they are the same as abstract methods in abstract classes as both are abstract. A caveat here though is how they are handled when polymorphism comes into play. This can be illustrated with the following code:

    interface IA 
    { 
        int xxx(); 
    }
    
    abstract class B 
    { 
        public abstract int yyy();
    }
    
    class C : B, IA
    {
        public int xxx()
        {
            return 1;
        }
    
        public int yyy()
        {
            return 1;
        }
    }
    

The yyy definition actually hides the abstract method in B and needs to be declared as public override int yyy()... to prevent this.

  1. No. you cannot "partially implement the non-abstract methods in abstract class". You can partially implement an abstract class by providing some concrete methods and some abstract methods. You cannot "partially implement" an abstract method though. A method in an abstract class is either abstract or it isn't.

  2. In your code example, OrdinaryClass is providing an implementation of xxx. OrdinaryClass2 is hiding that abstract method though and providing its own xxx. as mentioned in (1). Please read up on method hiding in C# for more details, eg http://www.akadia.com/services/dotnet_polymorphism.html and Overriding vs method hiding.

Community
  • 1
  • 1
David Arno
  • 41,717
  • 15
  • 81
  • 129
-1

Finally I found the answer by my self .

  • The big difference of abstract method and interface method is

We can implement the abstract method in inside of the same abstract class , but we can't implement the interface method in inside of the same interface .

Ramesh Rajendran
  • 35,211
  • 40
  • 143
  • 222
  • You have just provided a "what's the difference between abstract classes and interfaces" answer to your question, which you claimed was not asking "what's the difference between abstract classes and interfaces?" I wrote pretty much the same words for you at the start and you complained that wasn't what you were asking for. Lost for words... :( – David Arno Oct 18 '13 at 13:07