let me assure you that radio object should not expose any public methods. This is what is known as datahiding.
Saying radio object should not expose public methods is your design choice, but there isn't one way to do things. Actually, there are very good reasons for exposing the radio object, but not exposing certain methods. As you saw in the example I gave you,
I exposed Radio object, made its constructor internal, made it as a property of car, and that way you could interface with the radio directly.
naturehermit
Here it is radio object, imagine in a bank..The classes should work by passing message to the underlying object.
That would be a design pattern choice, like using observer pattern and such. That is a choice but there isn't one way to do things. Having an internal object is not passing messages to the underlying object, but working with an internal object.
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
Brian, I did some experimentation and found that despite the method being public in the radio class, it actually is not a modifier risk in the initial sense because the class is declared as protected. So any other class cannot use that method anyway, but
if somebody was able to modify just one keyword of the class from protected to public, the whole class will be undermined. Whereas if in the real case where class and its methods are protected, then all of them will need to be changed for having a public expose.
So it seems c# has some issues with it that may not become apparent so quickly but will come out during a hacking session or program mistakes. Java on the other hand seems to work with it and would not have those issues. There is another thread I am running
with variable scoping, which again seem works fine in Java and has issues with c# and nobody has been able to understand why or provide explanation. I have this thread looked up by the Team as well.
More and more I am testing this, I m beginning to belive that C# is a weak language in terms of specifications of OO and will hurt someone, somewhere badly, unless somebody can tell me its designed like that or its supposed to be. I am running a test with
generics and here again...different issue though..but ...Anyway the language fails formal methods tests so I should have understood.
As per a choice of design pattern, my friend--Best practices is not a design choice and If you subscribe to a methodology OO in this case, then your tools should subscribe to that as well. i wish I am wrong because it hurts to know that such a beautiful
toolset with so much rich support in tools has problem with spec.
Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
I don't mean the radio class, I mean car's playCarRadio is not declared public, as well as the static void main method. The whole class will be undermined if public, not necessarily. Even best practices are a design pattern of choice, as one best practice
may vary from another. The pattern you employ is one specific design pattern and there are others that work in a different layout. It's all relative.
Out of curiosity, how will C# hurt someone?
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
If you are tellling me that this is some sort of feature of C#, perhaps that is the answer. But 'MADSTORGERSEN MSFT' let me assure you that radio object should not expose any public methods. This is what is known as datahiding. Why should anybody be aware
of radio object. Here it is radio object, imagine in a bank..The classes should work by passing message to the underlying object. Dont tell me guys at Microsoft again misunderstood encapsulation just like they misunderstood all W3C recommendations.
Please could someone let me know if this is a feature to .net or a bug..
Access control works a little differently in C# than Java because nested classes were part of C# from the start, whereas they were grafted onto Java as a later addition. We were able to design the whole thing together.
In C# when a nested class such as your "radio" is declared "protected" that means that the whole class is only accesible within its surrounding class ("car") and the subclasses of that. Outside of "car" and its derived classes
noone can access the radio class or its members, regardless of whether those members are public or not. in C# public on a member simply means "visible to everyone who can see my enclosing type".
The problem in Java is that it is hard for a nested type to hide something from the enclosing type. We do in fact understand encapsulation very well at MS [:)] to the degree that we give you proper encapsulation of nested types as well.
So keep the radio class protected, and make its methods public. They won't escape to anyone outside the car, because the protected-ness of the car class - well -
protects them. It does not expose public methods to anyone but car and its subclasses. Never ever.
Yes Brian you are right that the car's playCarRadio should be public in real life but because I am calling the main from with in the same class so it was hardly an issue. Brian best practices are not design pattern of choice but way how a thing should be
done. If you read the OO spec from OMG you will find that an encapsulated element should not expose anything public. If the language subscribes to OO then it should follow up.
Pattern is all together a different issue as different people have different ways of doing things. Like some go for SOA, some go for repetitivness, some for individuality...different topic. though..
How will c# hurt someone--One of the guy I know wrote a code that led to death of 250 people because of the problem in the spec of the language itself (code was used in an airliner) and he is very old and still regrets it. You and I are very young but we
have a responsibility towards our code to the whole society and we cannot feign ignorance. A company like microsoft which is super rich can live with it, because they were formed out of ..you know...but we should do our best..
Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
I am obviously not starting a debate between java and c# but it suffices to say that they have done a brilliant job and followed things to the spec. Its like IE and Mozilla and despite IE7--Firefox is way ahead of times then IE7 --reason software designed
to the spec.
Well If you are from Microsoft, please tell me that its a feature that in a protected class you need to have public methods to work with it. Although according to OO spec, an encapsulated class should not expose any public methods period.
If you read my earlier post I have mentioned the downside of keeping class protected and methods public although I agree that whatever the expose of the class is..it will apply.
As per your saying about encapsulation of nested types, check this out-- I will put comments to illustrate my point.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class car
{
protected class radio
{
enum power : int { on, off };
private power _power;
public void playRadio()
{
_power = power.on;
Console.WriteLine("Radio is", _power.ToString());
test t = new test();
//Look here its no longer protected because I just got it...(if the method were not public this would never happen)
t.test2();
}
//The method is protected
protected void test1()
{
Console.WriteLine("running test1");
}
protected class test
{
public void test2()
{
Console.WriteLine("running test");
radio r = new radio();
//called internally as it should be....
r.test1();
}
}
}
void playCarRadio()
{
car.radio r = new car.radio();
r.playRadio();
}
static void Main(string[] args)
{
car Audi = new car();
Audi.playCarRadio();
gen Oa = new gen("Hello", "World");
Console.WriteLine((string)Oa.t + (string)Oa.u);
GenM<string, string> ga = new GenM<string, string>("Hello ", "World ");
Console.WriteLine(ga.t + ga.u);
gen ob = new gen(10.124, 2005);
Console.WriteLine((double)ob.t + (int)ob.u);
GenM<double, int> gb = new GenM<double, int>(10.124, 2005);
Console.WriteLine(gb.t + gb.u);
}
}
}
Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: OO Concept--general question about Encapsulation.
Jun 19, 2007 11:44 AM|LINK
Saying radio object should not expose public methods is your design choice, but there isn't one way to do things. Actually, there are very good reasons for exposing the radio object, but not exposing certain methods. As you saw in the example I gave you, I exposed Radio object, made its constructor internal, made it as a property of car, and that way you could interface with the radio directly.
That would be a design pattern choice, like using observer pattern and such. That is a choice but there isn't one way to do things. Having an internal object is not passing messages to the underlying object, but working with an internal object.
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
naturehermit
Star
14610 Points
3046 Posts
Re: OO Concept--general question about Encapsulation.
Jun 19, 2007 11:59 AM|LINK
Brian, I did some experimentation and found that despite the method being public in the radio class, it actually is not a modifier risk in the initial sense because the class is declared as protected. So any other class cannot use that method anyway, but if somebody was able to modify just one keyword of the class from protected to public, the whole class will be undermined. Whereas if in the real case where class and its methods are protected, then all of them will need to be changed for having a public expose.
So it seems c# has some issues with it that may not become apparent so quickly but will come out during a hacking session or program mistakes. Java on the other hand seems to work with it and would not have those issues. There is another thread I am running with variable scoping, which again seem works fine in Java and has issues with c# and nobody has been able to understand why or provide explanation. I have this thread looked up by the Team as well.
More and more I am testing this, I m beginning to belive that C# is a weak language in terms of specifications of OO and will hurt someone, somewhere badly, unless somebody can tell me its designed like that or its supposed to be. I am running a test with generics and here again...different issue though..but ...Anyway the language fails formal methods tests so I should have understood.
As per a choice of design pattern, my friend--Best practices is not a design choice and If you subscribe to a methodology OO in this case, then your tools should subscribe to that as well. i wish I am wrong because it hurts to know that such a beautiful toolset with so much rich support in tools has problem with spec.
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: OO Concept--general question about Encapsulation.
Jun 19, 2007 05:24 PM|LINK
I don't mean the radio class, I mean car's playCarRadio is not declared public, as well as the static void main method. The whole class will be undermined if public, not necessarily. Even best practices are a design pattern of choice, as one best practice may vary from another. The pattern you employ is one specific design pattern and there are others that work in a different layout. It's all relative.
Out of curiosity, how will C# hurt someone?
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
MadsTorgerse...
Member
8 Points
4 Posts
Re: OO Concept--general question about Encapsulation.
Jun 19, 2007 05:48 PM|LINK
Access control works a little differently in C# than Java because nested classes were part of C# from the start, whereas they were grafted onto Java as a later addition. We were able to design the whole thing together.
In C# when a nested class such as your "radio" is declared "protected" that means that the whole class is only accesible within its surrounding class ("car") and the subclasses of that. Outside of "car" and its derived classes noone can access the radio class or its members, regardless of whether those members are public or not. in C# public on a member simply means "visible to everyone who can see my enclosing type".
The problem in Java is that it is hard for a nested type to hide something from the enclosing type. We do in fact understand encapsulation very well at MS [:)] to the degree that we give you proper encapsulation of nested types as well.
So keep the radio class protected, and make its methods public. They won't escape to anyone outside the car, because the protected-ness of the car class - well - protects them. It does not expose public methods to anyone but car and its subclasses. Never ever.
Mads
MadsTorgerse...
Member
8 Points
4 Posts
Re: OO Concept--general question about Encapsulation.
Jun 19, 2007 05:52 PM|LINK
Sorry, typo: That should be the protected-ness of the radio class, obviously.
naturehermit
Star
14610 Points
3046 Posts
Re: OO Concept--general question about Encapsulation.
Jun 20, 2007 08:38 AM|LINK
Yes Brian you are right that the car's playCarRadio should be public in real life but because I am calling the main from with in the same class so it was hardly an issue. Brian best practices are not design pattern of choice but way how a thing should be done. If you read the OO spec from OMG you will find that an encapsulated element should not expose anything public. If the language subscribes to OO then it should follow up.
Pattern is all together a different issue as different people have different ways of doing things. Like some go for SOA, some go for repetitivness, some for individuality...different topic. though..
How will c# hurt someone--One of the guy I know wrote a code that led to death of 250 people because of the problem in the spec of the language itself (code was used in an airliner) and he is very old and still regrets it. You and I are very young but we have a responsibility towards our code to the whole society and we cannot feign ignorance. A company like microsoft which is super rich can live with it, because they were formed out of ..you know...but we should do our best..
naturehermit
Star
14610 Points
3046 Posts
Re: OO Concept--general question about Encapsulation.
Jun 20, 2007 08:56 AM|LINK
Dear Mads,
I am obviously not starting a debate between java and c# but it suffices to say that they have done a brilliant job and followed things to the spec. Its like IE and Mozilla and despite IE7--Firefox is way ahead of times then IE7 --reason software designed to the spec.
Well If you are from Microsoft, please tell me that its a feature that in a protected class you need to have public methods to work with it. Although according to OO spec, an encapsulated class should not expose any public methods period.
If you read my earlier post I have mentioned the downside of keeping class protected and methods public although I agree that whatever the expose of the class is..it will apply.
As per your saying about encapsulation of nested types, check this out-- I will put comments to illustrate my point.
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class car { protected class radio { enum power : int { on, off }; private power _power; public void playRadio() { _power = power.on; Console.WriteLine("Radio is", _power.ToString()); test t = new test();//Look here its no longer protected because I just got it...(if the method were not public this would never happen) t.test2(); }//The method is protected protected void test1() { Console.WriteLine("running test1"); } protected class test { public void test2() { Console.WriteLine("running test"); radio r = new radio();//called internally as it should be.... r.test1(); } } } void playCarRadio() { car.radio r = new car.radio(); r.playRadio(); } static void Main(string[] args) { car Audi = new car(); Audi.playCarRadio(); gen Oa = new gen("Hello", "World"); Console.WriteLine((string)Oa.t + (string)Oa.u); GenM<string, string> ga = new GenM<string, string>("Hello ", "World "); Console.WriteLine(ga.t + ga.u); gen ob = new gen(10.124, 2005); Console.WriteLine((double)ob.t + (int)ob.u); GenM<double, int> gb = new GenM<double, int>(10.124, 2005); Console.WriteLine(gb.t + gb.u); } } }