OOP concepts - Access level

X

Xarky

Hi,
I have the following scenario.

// This namespace is a class library
namespace test
{
Inteface A
{
void method1(...);
}

public abstract class B : A
{
public void method1(...)
{
...
}
}

public class DoIt : B
{
...
}
}

DoIt c = new DoIt(...);
c.method1(...); // can be accessed

From a console Application, I need to access the methods in Class
DoIt, but not the methods inherited from the interface and implemented
in the abstract class B. As they are now, method1 can be accessed
from a console application.

I tried to define these methods as internal or protected, but the
compiler complained with the following error.

'test.B' does not implement interface member 'test.A.method1()'.
'test.B.method1()' is either static, not public, or has the wrong
return type.


Can someone help me solve this out.
Thanks in Advance
 
J

Joanna Carter \(TeamB\)

// This namespace is a class library
namespace test
{
Inteface A
{
void method1(...);
}

public abstract class B : A
{
public void method1(...)
{
...
}
}

public class DoIt : B
{
...
}
}

DoIt c = new DoIt(...);
c.method1(...); // can be accessed

From a console Application, I need to access the methods in Class
DoIt, but not the methods inherited from the interface and implemented
in the abstract class B. As they are now, method1 can be accessed
from a console application.

I tried to define these methods as internal or protected, but the
compiler complained with the following error.

'test.B' does not implement interface member 'test.A.method1()'.
'test.B.method1()' is either static, not public, or has the wrong
return type.

Use "explicit interface implementation"

public abstract class B : A
{
void A.method1(...)
{
...
}
}

Then the only way to get at that method is through the interface.

Joanna
 
J

Joanna Carter \(TeamB\)

Can than method1(...) be accessed from class DoIt.

Yes, you just need to cast 'this' to A :

public class DoIt : B
{
((B) this).method1();
}

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top