Reputation: 4503
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:
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?
Upvotes: 2
Views: 257
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
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
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
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
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