Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOPS query using c#

Tags:

c#

oop

interface

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.
like image 682
Pankaj Avatar asked Jan 24 '26 21:01

Pankaj


2 Answers

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
like image 162
Winston Smith Avatar answered Jan 27 '26 10:01

Winston Smith


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 :)

like image 31
jim tollan Avatar answered Jan 27 '26 11:01

jim tollan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!