Interface implementation and access with class object.

Posted by Allemahesh under C# on 11/22/2013 | Points: 10 | Views : 1623 | Status : [Member] [MVP] | Replies : 5
I have an interface and that interface contain one method test(). I have class and I have implemented that interface and provided the definition for that interface method i.e test(). I have created an object of that class and I try to access the the test() method with that class object. I am successfully able to access the test() method with class object.
Now My questions is how to present that test() methods not accessible with my class object. Is this possible?
If yes, then how?

Code:-


interface test
{
void test();
}

class a : test
{
public void test()
{

}
}

class Program
{
static void Main(string[] args)
{
a obj = new a();
obj.test(); //Here I don't want to display test(). This should give error for me.
}
}





Responses

Posted by: Bandi on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Yes you can do as follows:

interface test

{

void test();

}



class a : test

{

void test.test() // implement InterfaceName.Method()

{



}

}



class Program

{

static void Main(string[] args)

{

a obj = new a();

obj.test(); //Here you will get error

// To access test() method of type a. use as follows
//((test)obj).test();

}

}



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/22/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Chandu I got you solution. But this is not I am looking as you are using the obj is ultimately object of class a. Is there any other way to this?

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Without using object reference means you can define it as static.. then you can access directly through class name itself...

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer this link
http://tutorials.csharp-online.net/CSharp_Coding_Solutions%E2%80%94Implementing_Interfaces

Let me know the purpose of your requirement?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/22/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Chandu, as you said "Without using object reference means you can define it as static.. then you can access directly through class name itself...", can you post code with respect to above code for this?

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response