I have following program and program compiles if I change the protection of playRadio in radio object to public. I want to know why it doesnt compile as it is against the principles of OO design that the caller should be unaware of underlying object in this
case radio. The car should do a message passing to the radio object and it should then invoke a response.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace ConsoleApplication1
{
class Program
{
static Point p = new System.Drawing.Point(20, 30);
struct Cycle
{
int _val, _min, _max;
public Cycle(int min, int max)
{
_val = min;
_min = min;
_max = max;
}
public int Value
{
get { return _val; }
set
{
if (_val > _max)
{
_val = _min;
}
else
{
if (_val < _min)
{
_val = _max;
}
else
_val = value;
}
}
}
public override string ToString()
{
return Value.ToString();
}
public int ToInteger()
{
return Value;
}
public static Cycle operator +(Cycle arg1, int arg2)
{
arg1.Value += arg2;
return arg1;
}
public static Cycle operator -(Cycle arg1, int arg2)
{
arg1.Value -= arg2;
return arg1;
}
}
public class car
{
internal class radio
{
enum power{on, off};
protected void playRadio()
{
//power=power.on;
Console.WriteLine("Radio is", power.on);
}
}
public void playCarRadio()
{
car.radio r = new car.radio();
r.playRadio();
}
}
static void Main(string[] args)
{
Cycle degrees = new Cycle(0, 359);
Cycle quarters = new Cycle(1,4);
car Audi = new car();
Audi.playCarRadio();
for (int i = 0; i <= 8; i++)
{
degrees += 90;
quarters += 1;
Console.WriteLine("degrees = {0}, quarters = {1}", degrees,quarters);
}
//object o;
p.Offset(-1, -1);
Console.WriteLine("Point X {0}, Y {1}", p.X, p.Y);
//Console.WriteLine(o == (null ? 0 : Convert.ToInt32(o)));
}
}
}
Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
If you make the method protected for the internal class radio in this case, it is only available to itself and classes that derive from it, so that won't work because car doesn't inherit from radio. If you make it protected internal void playRadio(), that
will work. Also, to retain the radio power setting, car should have a property of type radio, so it can retain the value you set.
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).
Car object (in my opinion due to principles of message passing) does not need to create a property either, because creating a property will make it accessible publicly. A user should be unaware of radio object. The user presses a button in the car, the car
responds by passing message internally to the radio object and radio object does something. Although both achieve the same result, but concept of message passing--Encapsulation hides the underlying object, property exposes the underlying object.
Please reply...and help me clear my doubts if I am wrong.
Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
Car object (in my opinion due to principles of message passing) does not need to create a property either, because creating a property will make it accessible publicly. A user should be unaware of radio object. The user presses a button in the car, the car
responds by passing message internally to the radio object and radio object does something. Although both achieve the same result, but concept of message passing--Encapsulation hides the underlying object, property exposes the underlying object.
Please reply...and help me clear my doubts if I am wrong.
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 didn't say change protected to internal, use protected internal combined (another level of exposure). Properties can be internal or protected, or protected internal as well; realize that you set the car.radio method, but nothing is keeping track of radio
so the value's aren't retained... So you work with the radio object, but after the car's radio method is done, nothing knows about it.
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).
public CarRadio Radio
{
get
{
if (_radio == null)
_radio = new CarRadio();
return _radio;
}
}
public class CarRadio
{
private power _power;
internal CarRadio() { _power = power.off; }
enum power{on, off}; public void playRadio() { _power=power.on; Console.WriteLine("Radio is {0}", _power);
}
}
Then, all you have to do to play the radio is Audi.Radio.playRadio() instead of the redirection... And no one can freely instantiate Radio because it's constructor is internal...
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).
In Java, protected is strictly more permissive than "package". In C# the equivalent of Java's protected is "protected internal". protected makes a member visible in derived classes, internal makes it visible in the same assembly, "protected internal" makes
it visible in both places.
Thanks for the elaboration Mads; I didn't know anything about Java.
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).
Guys thanks all again for replying, however I am sorry if the internal has confused the debate. I shouldnt have put the internal over there. My simple question is why a simple protected method of a class inside a class is not accessible to this class as
in my above example. By making protected internal, I am merely changing the visibility to make it visible in all assembly, but thats not what I want to understand, what I want to understand is why the method is inaccessible. My making it internal--its visibile
to everyone in the assembly (against encapsulation.) But that I dont want to do. here is it again as below.
using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Drawing;
5
6 namespace ConsoleApplication1
7 {
8 class Program
9 {
10 static Point p = new System.Drawing.Point(20, 30);
11 struct Cycle
12 {
13 int _val, _min, _max;
14 public Cycle(int min, int max)
15 {
16 _val = min;
17 _min = min;
18 _max = max;
19 }
20
21 public int Value
22 {
23 get { return _val; }
24 set
25 {
26 if (_val > _max)
27 {
28 _val = _min;
29 }
30 else
31 {
32 if (_val < _min)
33 {
34 _val = _max;
35 }
36 else
37 _val = value;
38 }
39 }
40 }
41 public override string ToString()
42 {
43 return Value.ToString();
44 }
45 public int ToInteger()
46 {
47 return Value;
48 }
49 public static Cycle operator +(Cycle arg1, int arg2)
50 {
51 arg1.Value += arg2;
52 return arg1;
53 }
54 public static Cycle operator -(Cycle arg1, int arg2)
55 {
56 arg1.Value -= arg2;
57 return arg1;
58 }
59 }
60 public class car
61 {
62 class radio
63 {
64 enum power{on, off};
65
66 protected void playRadio()
67 {
68 //power=power.on;
69 Console.WriteLine("Radio is", power.on);
70 }
71
72 }
73 public void playCarRadio()
74 {
75 car.radio r = new car.radio();
76 r.playRadio();
77 }
78 }
79 static void Main(string[] args)
80 {
81 Cycle degrees = new Cycle(0, 359);
82 Cycle quarters = new Cycle(1,4);
83 car Audi = new car();
84 Audi.playCarRadio();
85 for (int i = 0; i <= 8; i++)
86 {
87 degrees += 90;
88 quarters += 1;
89 Console.WriteLine("degrees = {0}, quarters = {1}", degrees,quarters);
90 }
91 //object o;
92 p.Offset(-1, -1);
93 Console.WriteLine("Point X {0}, Y {1}", p.X, p.Y);
94 //Console.WriteLine(o == (null ? 0 : Convert.ToInt32(o)));
95 }
96 }
97 }
98
Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
protected only allows you to access the member, method, etc from WITHIN the class.
this will work:
public class Car
{
protected void AProtectedMethod()
{
}
protected class Radio
{
Radio(Car parent)
{
parent.AProtectedMethod();
}
}
}
but this will not:
public class Car
{
Car()
{
Radio radio = new Radio();
radio.AProtectedMethod() ; //THIS WILL NOT COMPILE
}
protected class Radio
{
protected void AProtectedMethod()
{
}
}
}
naturehermit
Star
14610 Points
3046 Posts
OO Concept--general question about Encapsulation.
Jun 14, 2007 09:50 AM|LINK
I have following program and program compiles if I change the protection of playRadio in radio object to public. I want to know why it doesnt compile as it is against the principles of OO design that the caller should be unaware of underlying object in this case radio. The car should do a message passing to the radio object and it should then invoke a response.
using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace ConsoleApplication1 { class Program { static Point p = new System.Drawing.Point(20, 30); struct Cycle { int _val, _min, _max; public Cycle(int min, int max) { _val = min; _min = min; _max = max; } public int Value { get { return _val; } set { if (_val > _max) { _val = _min; } else { if (_val < _min) { _val = _max; } else _val = value; } } } public override string ToString() { return Value.ToString(); } public int ToInteger() { return Value; } public static Cycle operator +(Cycle arg1, int arg2) { arg1.Value += arg2; return arg1; } public static Cycle operator -(Cycle arg1, int arg2) { arg1.Value -= arg2; return arg1; } } public class car { internal class radio { enum power{on, off}; protected void playRadio() { //power=power.on; Console.WriteLine("Radio is", power.on); } } public void playCarRadio() { car.radio r = new car.radio(); r.playRadio(); } } static void Main(string[] args) { Cycle degrees = new Cycle(0, 359); Cycle quarters = new Cycle(1,4); car Audi = new car(); Audi.playCarRadio(); for (int i = 0; i <= 8; i++) { degrees += 90; quarters += 1; Console.WriteLine("degrees = {0}, quarters = {1}", degrees,quarters); } //object o; p.Offset(-1, -1); Console.WriteLine("Point X {0}, Y {1}", p.X, p.Y); //Console.WriteLine(o == (null ? 0 : Convert.ToInt32(o))); } } }bmains
All-Star
29116 Points
5886 Posts
MVP
Re: OO Concept--general question about Encapsulation.
Jun 14, 2007 12:17 PM|LINK
Hey,
If you make the method protected for the internal class radio in this case, it is only available to itself and classes that derive from it, so that won't work because car doesn't inherit from radio. If you make it protected internal void playRadio(), that will work. Also, to retain the radio power setting, car should have a property of type radio, so it can retain the value you set.
"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 14, 2007 01:48 PM|LINK
Thanks for replying, however protected member is accessible from class with which it is declared http://msdn2.microsoft.com/en-us/library/bcd5672a(VS.71).aspx. Even if I change internal to protected at class level it doesnt work.
The same example runs fine in Java.
Car object (in my opinion due to principles of message passing) does not need to create a property either, because creating a property will make it accessible publicly. A user should be unaware of radio object. The user presses a button in the car, the car responds by passing message internally to the radio object and radio object does something. Although both achieve the same result, but concept of message passing--Encapsulation hides the underlying object, property exposes the underlying object.
Please reply...and help me clear my doubts if I am wrong.
naturehermit
Star
14610 Points
3046 Posts
Re: OO Concept--general question about Encapsulation.
Jun 14, 2007 01:59 PM|LINK
Thanks for replying, however protected member is accessible from class with which it is declared http://msdn2.microsoft.com/en-us/library/bcd5672a(VS.71).aspx. Even if I change internal to protected at class level it doesnt work.
The same example runs fine in Java.
Car object (in my opinion due to principles of message passing) does not need to create a property either, because creating a property will make it accessible publicly. A user should be unaware of radio object. The user presses a button in the car, the car responds by passing message internally to the radio object and radio object does something. Although both achieve the same result, but concept of message passing--Encapsulation hides the underlying object, property exposes the underlying object.
Please reply...and help me clear my doubts if I am wrong.
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: OO Concept--general question about Encapsulation.
Jun 14, 2007 04:56 PM|LINK
Hey,
I didn't say change protected to internal, use protected internal combined (another level of exposure). Properties can be internal or protected, or protected internal as well; realize that you set the car.radio method, but nothing is keeping track of radio so the value's aren't retained... So you work with the radio object, but after the car's radio method is done, nothing knows about it.
"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).
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: OO Concept--general question about Encapsulation.
Jun 14, 2007 05:03 PM|LINK
Hey,
What if you did this instead:
public class Car
{
private CarRadio _radio;
public CarRadio Radio
{
get
{
if (_radio == null)
_radio = new CarRadio();
return _radio;
}
}
public class CarRadio
{
private power _power;
internal CarRadio() { _power = power.off; }
enum power{on, off};
public void playRadio()
{
_power=power.on;
Console.WriteLine("Radio is {0}", _power);
}
}
Then, all you have to do to play the radio is Audi.Radio.playRadio() instead of the redirection... And no one can freely instantiate Radio because it's constructor is internal...
"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 14, 2007 07:01 PM|LINK
Just to elaborate:
In Java, protected is strictly more permissive than "package". In C# the equivalent of Java's protected is "protected internal". protected makes a member visible in derived classes, internal makes it visible in the same assembly, "protected internal" makes it visible in both places.
Thanks,
Mads Torgersen, MS C# Language PM
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: OO Concept--general question about Encapsulation.
Jun 14, 2007 07:12 PM|LINK
Thanks for the elaboration Mads; I didn't know anything about Java.
"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 15, 2007 08:18 AM|LINK
Guys thanks all again for replying, however I am sorry if the internal has confused the debate. I shouldnt have put the internal over there. My simple question is why a simple protected method of a class inside a class is not accessible to this class as in my above example. By making protected internal, I am merely changing the visibility to make it visible in all assembly, but thats not what I want to understand, what I want to understand is why the method is inaccessible. My making it internal--its visibile to everyone in the assembly (against encapsulation.) But that I dont want to do. here is it again as below.
using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Drawing; 5 6 namespace ConsoleApplication1 7 { 8 class Program 9 { 10 static Point p = new System.Drawing.Point(20, 30); 11 struct Cycle 12 { 13 int _val, _min, _max; 14 public Cycle(int min, int max) 15 { 16 _val = min; 17 _min = min; 18 _max = max; 19 } 20 21 public int Value 22 { 23 get { return _val; } 24 set 25 { 26 if (_val > _max) 27 { 28 _val = _min; 29 } 30 else 31 { 32 if (_val < _min) 33 { 34 _val = _max; 35 } 36 else 37 _val = value; 38 } 39 } 40 } 41 public override string ToString() 42 { 43 return Value.ToString(); 44 } 45 public int ToInteger() 46 { 47 return Value; 48 } 49 public static Cycle operator +(Cycle arg1, int arg2) 50 { 51 arg1.Value += arg2; 52 return arg1; 53 } 54 public static Cycle operator -(Cycle arg1, int arg2) 55 { 56 arg1.Value -= arg2; 57 return arg1; 58 } 59 } 60 public class car 61 { 62 class radio 63 { 64 enum power{on, off}; 65 66 protected void playRadio() 67 { 68 //power=power.on; 69 Console.WriteLine("Radio is", power.on); 70 } 71 72 } 73 public void playCarRadio() 74 { 75 car.radio r = new car.radio(); 76 r.playRadio(); 77 } 78 } 79 static void Main(string[] args) 80 { 81 Cycle degrees = new Cycle(0, 359); 82 Cycle quarters = new Cycle(1,4); 83 car Audi = new car(); 84 Audi.playCarRadio(); 85 for (int i = 0; i <= 8; i++) 86 { 87 degrees += 90; 88 quarters += 1; 89 Console.WriteLine("degrees = {0}, quarters = {1}", degrees,quarters); 90 } 91 //object o; 92 p.Offset(-1, -1); 93 Console.WriteLine("Point X {0}, Y {1}", p.X, p.Y); 94 //Console.WriteLine(o == (null ? 0 : Convert.ToInt32(o))); 95 } 96 } 97 } 98scobrown
Member
338 Points
49 Posts
Re: OO Concept--general question about Encapsulation.
Jun 15, 2007 08:22 PM|LINK
protected only allows you to access the member, method, etc from WITHIN the class.
but this will not: