-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
  • 37,412
  • 45
  • 153
  • 234
  • 2
    What do you mean with _partially_ ? – H H Oct 18 '13 at 11:50
  • This has been asked before, many times. So a new question must be much more specific. – H H Oct 18 '13 at 11:52
  • Also, _"we can't implement the non abstract methods"_ is not really true. – H H Oct 18 '13 at 11:54
  • @HenkHolterman hmm We can partially implement the non abstract method in parent(abstract) class and then we can fully implement that methods in child class , Is it possible to abstract method ? understood ? – Ramesh Rajendran Oct 18 '13 at 11:54
  • csc.exe: `MyMethod()` cannot declare a body because it is marked abstract. Abstract (or MustInherit in VB) methods cannot contain implementation. "Partially" implemented methods, or the use of the same, can be marked as `virtual` and be overridden in derived classes as long as the overridden method calls the base implementation. – Mikael Östberg Oct 18 '13 at 11:55
  • @RameshRajendran - No, not understood. We have partial methods (in partial classes) and default implementation (virtual methods) but nothing called "partial implementation". Do not make up your own terminology. – H H Oct 18 '13 at 11:55
  • Just wait a mins , I will asking with code example's . Any one does not understand my question . – Ramesh Rajendran Oct 18 '13 at 12:00
  • Hey why is it duplicate ? Read my question one another time . – Ramesh Rajendran Oct 18 '13 at 12:01
  • Shouting won't help, start by fixing the title. – H H Oct 18 '13 at 12:02
  • Okay let see . abstract method and interface method are same or not ? – Ramesh Rajendran Oct 18 '13 at 12:05
  • Okay all , See my updated question . Now i explained with code . – Ramesh Rajendran Oct 18 '13 at 12:15
  • Yes, there are similarities, So we are back to interface vs abstract class, like in the linked dupe and many others. – H H Oct 18 '13 at 13:43

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
  • 42,717
  • 16
  • 86
  • 131
-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
  • 37,412
  • 45
  • 153
  • 234
  • 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