Interface does not alter class, or does it? [modified]
-
This was a question on a students exam: Shown was a static void main, and a second class, titled Punta. namespace Test { class Test { static void Main(string[] args) { Punta punt = new Punta(); double bla = punt.Search(); } } class Punta { double Search() { return 5.0; //assume we calculated something here and found it } } } Question was: Is this code executable? (Exam) Answer: No Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why? And, how can you fix that, without changing the code of Punta-class. The expected answer for the teacher was: An interface. Followed by lines of code to make it accessible. BUT... it did say, "you may NOT change the code of the class 'Punta'". But how is that some people say: If you use an interface, you're actually modifying the class. And some say, you aren't. Which is it? -- modified at 23:04 Wednesday 5th September, 2007
-
This was a question on a students exam: Shown was a static void main, and a second class, titled Punta. namespace Test { class Test { static void Main(string[] args) { Punta punt = new Punta(); double bla = punt.Search(); } } class Punta { double Search() { return 5.0; //assume we calculated something here and found it } } } Question was: Is this code executable? (Exam) Answer: No Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why? And, how can you fix that, without changing the code of Punta-class. The expected answer for the teacher was: An interface. Followed by lines of code to make it accessible. BUT... it did say, "you may NOT change the code of the class 'Punta'". But how is that some people say: If you use an interface, you're actually modifying the class. And some say, you aren't. Which is it? -- modified at 23:04 Wednesday 5th September, 2007
Unless something was lost in translation, and certainly some code was, the problem is that
'Punta.Search()' is inaccessible due to its protection level
which can't be fixed without changing class Punta. An interface won't fix the situation as posted. -
Unless something was lost in translation, and certainly some code was, the problem is that
'Punta.Search()' is inaccessible due to its protection level
which can't be fixed without changing class Punta. An interface won't fix the situation as posted.Agree, either you add a public or if you use an interface and use it explicit you have to add the interface name plus a . to "Search"
-^-^-^-^-^- no risk no funk ................... please vote ------>
-
Agree, either you add a public or if you use an interface and use it explicit you have to add the interface name plus a . to "Search"
-^-^-^-^-^- no risk no funk ................... please vote ------>
... and you'd still have to add public to the method.
-
This was a question on a students exam: Shown was a static void main, and a second class, titled Punta. namespace Test { class Test { static void Main(string[] args) { Punta punt = new Punta(); double bla = punt.Search(); } } class Punta { double Search() { return 5.0; //assume we calculated something here and found it } } } Question was: Is this code executable? (Exam) Answer: No Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why? And, how can you fix that, without changing the code of Punta-class. The expected answer for the teacher was: An interface. Followed by lines of code to make it accessible. BUT... it did say, "you may NOT change the code of the class 'Punta'". But how is that some people say: If you use an interface, you're actually modifying the class. And some say, you aren't. Which is it? -- modified at 23:04 Wednesday 5th September, 2007
"double void" ? I want one of those!
-
... and you'd still have to add public to the method.
not when you explicitly implement it, then no modifier is allowed.
-^-^-^-^-^- no risk no funk ................... please vote ------>
-
This was a question on a students exam: Shown was a static void main, and a second class, titled Punta. namespace Test { class Test { static void Main(string[] args) { Punta punt = new Punta(); double bla = punt.Search(); } } class Punta { double Search() { return 5.0; //assume we calculated something here and found it } } } Question was: Is this code executable? (Exam) Answer: No Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why? And, how can you fix that, without changing the code of Punta-class. The expected answer for the teacher was: An interface. Followed by lines of code to make it accessible. BUT... it did say, "you may NOT change the code of the class 'Punta'". But how is that some people say: If you use an interface, you're actually modifying the class. And some say, you aren't. Which is it? -- modified at 23:04 Wednesday 5th September, 2007
mizitras wrote:
Question was: Is this code executable? (Exam) Answer: No
Correct, it won't even compile.
mizitras wrote:
Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why?
You can suppress warnings, not the errors. No, you still won't be able to compile this thing since the "double void Search()" will confuse the compiler. It won't make any assumptions about what you really mean here. You cannot have two return types.
mizitras wrote:
And, how can you fix that, without changing the code of Punta-class.
You can't. The Punta class MUST be changed to specify a single return type for the Search method. Also, since the default protection level is private, you must specify the Search method as public or internal for it to be seen outside the Punta class. An interface will NOT fix this problem since interfaces do not modify a class. Interfaces specify the methods that an implementing class must define. The class must still be modified to tell it which interface it's implementing. Either way, the Punta class must be modified in order for it to be fixed. -- modified at 12:04 Tuesday 4th September, 2007
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
mizitras wrote:
Question was: Is this code executable? (Exam) Answer: No
Correct, it won't even compile.
mizitras wrote:
Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why?
You can suppress warnings, not the errors. No, you still won't be able to compile this thing since the "double void Search()" will confuse the compiler. It won't make any assumptions about what you really mean here. You cannot have two return types.
mizitras wrote:
And, how can you fix that, without changing the code of Punta-class.
You can't. The Punta class MUST be changed to specify a single return type for the Search method. Also, since the default protection level is private, you must specify the Search method as public or internal for it to be seen outside the Punta class. An interface will NOT fix this problem since interfaces do not modify a class. Interfaces specify the methods that an implementing class must define. The class must still be modified to tell it which interface it's implementing. Either way, the Punta class must be modified in order for it to be fixed. -- modified at 12:04 Tuesday 4th September, 2007
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007