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?
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
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With