Pankaj
Pankaj

Reputation: 4503

OOPS query using c#

I have some doubt to implementation of class and interface

I have 2 class like this

Public Class A:IFinal
  {
      private string name=string.Empty;

        A()
        {
            name = "Pankaj";
        }

        public string MyName()
        {
            return name;
        }

        public string YourName()
        {
            return "Amit";
        }
   }

Public  Class B:IFinal
 {
     private string name=string.Empty;

        B()
        {
            name = "Amit";
        }

        public string GetNane()
        {
            return name;
        }

        public string YourName()
        {
            return "Joy";
        }
   }

Question:

  1. Now i have a interface IFinal and i want to implement this interface in class A & B for method YourName() like this

    public interface IFinal {

         string YourName();// Class A & Class B
    
    
    }
    

Is it possible to implement on this way? if yes then How can i declare YourName() in interface and how can i use this?

  1. Is it possible to declare virtual method in interface?like in class A & B we have a virtual method which need to be declare in interface.

Upvotes: 2

Views: 257

Answers (5)

Enigmativity
Enigmativity

Reputation: 117084

If you're trying to make IFinal have two methods, one that returns YourName from class A and one that returns YourName from class B then you'll need to introduce a third class (let's call it C) and then change the YourName methods on IFinal to be distinct - you can't have two methods with exactly the same signature on an interface.

So:

public interface IFinal
{
    string YourNameA();
    string YourNameB();
}

public class C : IFinal
{
    public A A {get; set;}
    public B B {get; set;}

    public string YourNameA()
    {
        return this.A.YourName();
    }

    public string YourNameB()
    {
        return this.B.YourName();
    }
}

Now, having said that, I'm not really sure if that's what you're asking or if doing this makes sense. You'll have to let us know more detail about what you're trying to do.

Upvotes: 0

Gopi
Gopi

Reputation: 5887

You have the reverse view of an Interface. First you have to declare the method in the Interface and then you have to move to implementation in the Classes that implements the Interface. You have taken the other way.

First declare the methods in the Interface

interface IFinal { 
string YourName(); 
}

Then

Public Class A:IFinal
{
    public string YourName()
    {
        return "Amit";
    }

}

and

Public Class B:IFinal
{
    public string YourName()
    {
        return "Joy";
    }
}

Here the Class A and B implements the interface IFinal and its method YourName().

To call the methods in the respective classes

IFinal clsA= new A();
IFinal clsB= new B();

Upvotes: 0

Winston Smith
Winston Smith

Reputation: 21892

You can make the method virtual in your implementation eg:

interface IFinal
{
    string YourName();
}

class A: IFinal
{
    public virtual string YourName() { return "Amit"; }
}

class B: IFinal
{
    public virtual string YourName() { return "Joy"; }
}

Or you could use a common base implementation which both A and B derive from, eg

interface IFinal
{
    string YourName();
}

abstract class FinalBase : IFinal
{
    public virtual string YourName() { return string.Empty; }
}

class A : FinalBase
{
    public override string YourName()
    {
        return "A";
    }
}

class B : FinalBase
{
    public override string YourName()
    {
        return "B";
    }
}

class C : A
{
    public override string YourName()
    {
        return "C";
    }
}

new A().YourName(); // A
new B().YourName(); // B

IFinal b = new B();
b.YourName(); // B

FinalBase b = new C();
b.YourName(); // C

Upvotes: 5

jim tollan
jim tollan

Reputation: 22485

Pankaj - the code formatting and values in the IFinal are making it pretty hard to figure out what you're attempting to do. based on what is supplied, then the sample simply would not compile for the obviuos reason that you've got the same property (string YourName();) defined twice.

can you redo the question to clarify your intentions plz... thanks

[edit] - i think i maybe 'understand' what you're asking - i.e. HOW to define the interface. here you go:

public interface IFinal
{   
    string YourName{ get; set; }
}

then, declare your variables along the lines of:

IFinal classA = new A();
IFinal classB = new B();

then, party hard :)

Upvotes: 3

Chuck Haines
Chuck Haines

Reputation: 692

If you declare your interface as

interface IFinal {
string YourName();
}

both classes will have to implement that function which I think is what you are asking.

Upvotes: 1

Related Questions