473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Is it possible to hide methods to certain classes?

Hey all,

I have 3 classes. Let's call them Plane, Pilot and Passenger.

I'm trying to restrict Passenger's access to Plane's more intimate
methods, but allow Pilot to use 'em.

Right now I have this "useless" interface that creates basic methods,
then Plane implements that interface to add more.

For example:

PlaneInterface
- Enter()
- Exit()

Plane : PlaneInterface
- TakeOff()
- Land()

When the passenger needs to use the Plane, I downcast the object to a
PlaneInterface so that Passenger only sees the Enter and Exit methods.

Is there a better method to restricting access of certain methods to
certain classes?

Jan 13 '07 #1
8 1403
AFAIK, you can't hide methods to specific classes/interfaces. There is two
ways of handling this tha I can think of:

Option 1: supply the pilot as a required parameter:

void TakeOff(PilotInterface Pilot);
void Land(PilotInterface Pilot);

Option 2: control access with a property:

Plane: PlaneInterface
{
PilotInterface Pilot { get; set; }
void TakeOff(); // will only take off if Pilot is assigned
void Land(); // will only land if Pilot is assigned
}

--
Regards, Peter
Jan 13 '07 #2
Hi,
you can't hide methods based on the caller but a way around it would be to
move the passenger into a different project than plane and mark metgos
internal, kind of a yucky way.

Mark.
--
http://www.markdawson.org
"GroZZleR" wrote:
Hey all,

I have 3 classes. Let's call them Plane, Pilot and Passenger.

I'm trying to restrict Passenger's access to Plane's more intimate
methods, but allow Pilot to use 'em.

Right now I have this "useless" interface that creates basic methods,
then Plane implements that interface to add more.

For example:

PlaneInterface
- Enter()
- Exit()

Plane : PlaneInterface
- TakeOff()
- Land()

When the passenger needs to use the Plane, I downcast the object to a
PlaneInterface so that Passenger only sees the Enter and Exit methods.

Is there a better method to restricting access of certain methods to
certain classes?

Jan 13 '07 #3

GroZZleR wrote:
Hey all,

I have 3 classes. Let's call them Plane, Pilot and Passenger.

I'm trying to restrict Passenger's access to Plane's more intimate
methods, but allow Pilot to use 'em.

Right now I have this "useless" interface that creates basic methods,
then Plane implements that interface to add more.

For example:

PlaneInterface
- Enter()
- Exit()

Plane : PlaneInterface
- TakeOff()
- Land()

When the passenger needs to use the Plane, I downcast the object to a
PlaneInterface so that Passenger only sees the Enter and Exit methods.

Is there a better method to restricting access of certain methods to
certain classes?
No... you found the best way to do that.

The interface isn't useless. It represents a subset of functionality of
interest to some consumers of the class but not others. In fact, yours
is a very good design.

Jan 13 '07 #4
"GroZZleR" <gr******@gmail.comha scritto nel messaggio
news:11**********************@l53g2000cwa.googlegr oups.com...
Hey all,

I have 3 classes. Let's call them Plane, Pilot and Passenger.

I'm trying to restrict Passenger's access to Plane's more intimate
methods, but allow Pilot to use 'em.

Right now I have this "useless" interface that creates basic methods,
then Plane implements that interface to add more.

For example:

PlaneInterface
- Enter()
- Exit()

Plane : PlaneInterface
- TakeOff()
- Land()

When the passenger needs to use the Plane, I downcast the object to a
PlaneInterface so that Passenger only sees the Enter and Exit methods.

Is there a better method to restricting access of certain methods to
certain classes?
If you cannot join the puzzle pieces means that... they don't have joinable
shapes.

Is the subject that do actions, not the object:

class FlyingPerson
void EnterIn(Plane)
void Exit()

class Passenger : FlayingPerson
void LockTheBelt()

class Pilot
void BeginPilot()
void EndPilot()

class Plane
List<PassengerPassengers
List<PilotPilots
void TakeOff()
void Land()

In this way your pieces can interact with each others without strange
workarounds.

--
www.neodatatype.net
Jan 13 '07 #5

Fabio wrote:
"GroZZleR" <gr******@gmail.comha scritto nel messaggio
news:11**********************@l53g2000cwa.googlegr oups.com...
Hey all,

I have 3 classes. Let's call them Plane, Pilot and Passenger.

I'm trying to restrict Passenger's access to Plane's more intimate
methods, but allow Pilot to use 'em.

Right now I have this "useless" interface that creates basic methods,
then Plane implements that interface to add more.

For example:

PlaneInterface
- Enter()
- Exit()

Plane : PlaneInterface
- TakeOff()
- Land()

When the passenger needs to use the Plane, I downcast the object to a
PlaneInterface so that Passenger only sees the Enter and Exit methods.

Is there a better method to restricting access of certain methods to
certain classes?

If you cannot join the puzzle pieces means that... they don't have joinable
shapes.

Is the subject that do actions, not the object:

class FlyingPerson
void EnterIn(Plane)
void Exit()

class Passenger : FlayingPerson
void LockTheBelt()

class Pilot
void BeginPilot()
void EndPilot()

class Plane
List<PassengerPassengers
List<PilotPilots
void TakeOff()
void Land()

In this way your pieces can interact with each others without strange
workarounds.
I can see this design getting into a lot of trouble later on.
>From the context of regular life, a person is a passenger only some of
the time... only while they're on a plane. Therefore, making a
passenger a whole separate class doesn't make much sense. Besides, what
do you do when a pilot flies as a passenger? Do you record them as two
separate entities: person-as-pilot being different from
person-as-passenger? You might, or you might not.

If, however, your system only ever sees passengers and doesn't see them
in any other context, then this sort of design will work.

I prefer the original design: just use interfaces to implement the
various capabilities of Person, and then you can write code that deals
with people in their different aspects and roles, while having only one
object representing one person.

Jan 14 '07 #6
"Bruce Wood" <br*******@canada.comha scritto nel messaggio
I can see this design getting into a lot of trouble later on.
>>From the context of regular life, a person is a passenger only some of
the time... only while they're on a plane. Therefore, making a
passenger a whole separate class doesn't make much sense. Besides, what
do you do when a pilot flies as a passenger? Do you record them as two
separate entities: person-as-pilot being different from
person-as-passenger? You might, or you might not.
It depends on what GroZZleR want to do.
He can also get troubles in the way he started with.

I just don't like objects that act as subjects.

--
www.neodatatype.net
Jan 14 '07 #7

Fabio wrote:
"Bruce Wood" <br*******@canada.comha scritto nel messaggio
I can see this design getting into a lot of trouble later on.
>From the context of regular life, a person is a passenger only some of
the time... only while they're on a plane. Therefore, making a
passenger a whole separate class doesn't make much sense. Besides, what
do you do when a pilot flies as a passenger? Do you record them as two
separate entities: person-as-pilot being different from
person-as-passenger? You might, or you might not.

It depends on what GroZZleR want to do.
He can also get troubles in the way he started with.

I just don't like objects that act as subjects.
Well, there's always an easy way around that: just invert the verb.
Instead of having an Enter method on the Plane interface that accepts
an IPassenger argument, you could have an AcceptPassenger method
instead. Subject-object relationships are often invertible: it just
depends upon how you look at it.

(That is to say, often subject-verb-object relationships represent
establishing, modifying, or breaking a relationship, and that
relationship can be seen from either point of view.)

Jan 15 '07 #8
"Bruce Wood" <br*******@canada.comha scritto nel messaggio
news:11**********************@q2g2000cwa.googlegro ups.com...
Well, there's always an easy way around that: just invert the verb.
Yes, you're right :)

--
www.neodatatype.net
Jan 15 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
8
by: Just D | last post by:
All, What are advantages and disadvantages of these two different approaches? 1 . I create a base class with a few virtual methods, then I derive my classes from this class, override these...
4
by: Adam Clauss | last post by:
OK, lets say I have a C# Windows application. In it is a a series of namespaces, all rooted for a certain namespace A. For ex, the "using" directives would read something like: using A; using...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
4
by: Paul Wu | last post by:
Is there a way to constract a derived class that hides certain public members of a base class ? With the following code, a class that derives from DerivedClass can still see the member "Name" in the...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
18
by: Angus | last post by:
Hello We have a lot of C++ code. And we need to now create a library which can be used from C and C++. Given that we have a lot of C++ code using classes how can we 'hide' the fact that it is...
3
by: raylopez99 | last post by:
The headline says it all. Great minds think alike: read the blog below from three years ago, as endorsed by Ritchie, who coinvented C. BTW the below lambda expression code will not work (.Where...
4
by: Rene | last post by:
Hi, I was wondering if anyone could tell me why extension methods must be declared on static classes. I mean, why not allowing them to be declared as static methods in regular instance classes?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.