For those of us out there who are self-taught programmers, this is a definite hurdle. For me the real difficulty was not the concepts behind polymorphism, but rather the lack of decent textbooks describing the concept. I can't count the number of textbooks that did not use any type of visual diagram when explaining these concepts...for me it would have been so much easier.Dr Herbie wrote:
W3bbo wrote:
Dr Herbie wrote:
"What is polymorphism and why is is useful?"
I was shocked at the number of candidates that couldn't separate polymorphism from class inheritance and I don't think I've met one yet who could explain why it was useful (other than inheriting functionality, which is not the correct answer).
It's useful because it enables code-reuse and overriding older/irrelevant methods.
But they're not so much separate as they are dependent. Polymorphism depends on inheritance. That's why VB6 isn't a true OOP language.
OK, firstly polymorphism has nothing to do with code-reuse. That's where you're confusing class inheritance with polymorphism (although you're right, polymorphism does depend on inheritance). Polymorphism can also be done through pure interface inheritance.
Polymorphism is where different classes can be made to appear the same through casting to a base class/interface. This allows all the references to be treated the same, thereby simplifying code that uses those objects.
The classic example is a vector drawing program there all the shapes derive from the base 'BaseShape'. The specific instances of shapes that the user creates (Like CircleShape, TriangleShape and RectangleShape) are stored in a collection of BaseShape instances and the picture is drawn by iterating through all the BaseShape instances and caling the 'Draw' method. The main program has no knowledge of which shape it is drawing and it treats them all the same.
The contents of the 'BaseShape' class are irrelevant to the technique of polymorphism; BaseShape may containa lot of shared code, or it may be an interface with no shared code. Polymorphism and code-reuse are two separate aspects of inheritance.
I had been doing OOP for nearly 10 years before I realised this distinction.
-
-
What does the keyword virtual mean? When would you use it?
When would you use an abstract class over using an interface?
What can you do to ensure that your objects are properly disposed of by GC?
What happens when you call GC.Collect()? Why would you call it? What's the cost? What happens?
Implement ToString()
C
-
My approach
Start off by asking a few softball programming trivia questions that anyone with any experience should be able to answer. If they fail these simple questions, it's obviously time to say "Next!".
Then follow up by asking them to describe the project they're currently working on and some of the specific techniques they're using. Ask them specific, open ended, questions about it like, "Why did you use generic List rather than an array?" or "How did you decide that XML serialization was the best approach?" The idea is to get a taste of how they organize their thoughts on familiar ground.
The last part, if they've done well so far, is to present them with a sample problem that fits within your domain. For example, let's say you load a lot of oddly formatted raw data files into database tables. Ask them how they would approach the problem and have them pseudo-code out their solution. This will give you an idea of how they think when presented with something new.
You can also ask off-beat thinking questions like "Everybody knows why manhole covers are round but why are septic tank covers square?"
-
Mine does not have one
public class LittleguruFoo
{
public new bool ToString
{
get { throw new NotImplementedException("I don't have a ToString method!"); }
}
}
Hehe
Foo f = new Foo();
f.ToString(); // Compiler error.
object f2 = new Foo();
f2.ToString(); // Now I have one again. Holy cow, I can't escape. -
pathfinder wrote:
Good point. What about P/Invoke? I like the suggestion about GC. These are good .NET questions, how about any questions that are specific to C# language structure?
When you use P/Invoke the result is always pseudo-typed into the correct struct / primative.
E.g. if a P/Invoke signature returns an int, you get something which can be conceptually thought of as a System.Int32 which extends System.Object.
Maddus Mattus wrote:
"What is a delegate?"
*void.
-
Double posty post post.
-
evildictaitor wrote:

Maddus Mattus wrote:
"What is a delegate?"
*void.
I'm hoping that's a c++ question,...
Because I would fail miserably
-
Maddus Mattus wrote:

evildictaitor wrote:

Maddus Mattus wrote:
"What is a delegate?"
*void.
I'm hoping that's a c++ question,...
Because I would fail miserably
I think what he is getting as is that at the root of it, a delegate is a function pointer. -
Ion Todirel wrote:
good questions, also you may ask when Dispose method is called
Dr Herbie wrote:
"What is polymorphism and why is is useful?"
I was shocked at the number of candidates that couldn't separate polymorphism from class inheritance and I don't think I've met one yet who could explain why it was useful (other than inheriting functionality, which is not the correct answer).
Also, for C#, ask them to explain garbage collection.
Herbie

And if that supports deterministic finalisation. Then ask about the Dispose pattern. (which is what i do, and did today)
-
Charles wrote:What does the keyword virtual mean? When would you use it?
How does new differ from override; best illustrated by a piece of sample code; where each class has a Message method printing the class name; then declare all 3 classes as the base type.
In the last 3 months only 1 person has gotten the final output right.
-
blowdart wrote:

Charles wrote:
What does the keyword virtual mean? When would you use it?
How does new differ from override; best illustrated by a piece of sample code; where each class has a Message method printing the class name; then declare all 3 classes as the base type.
In the last 3 months only 1 person has gotten the final output right.
Well you haven't phrased your question properly. Write it out fully and I'll give it a stab.
-
Dr Herbie wrote:
OK, firstly polymorphism has nothing to do with code-reuse. That's where you're confusing class inheritance with polymorphism (although you're right, polymorphism does depend on inheritance). Polymorphism can also be done through pure interface inheritance.
Polymorphism is where different classes can be made to appear the same through casting to a base class/interface. This allows all the references to be treated the same, thereby simplifying code that uses those objects.
The classic example is a vector drawing program there all the shapes derive from the base 'BaseShape'. The specific instances of shapes that the user creates (Like CircleShape, TriangleShape and RectangleShape) are stored in a collection of BaseShape instances and the picture is drawn by iterating through all the BaseShape instances and caling the 'Draw' method. The main program has no knowledge of which shape it is drawing and it treats them all the same.
The contents of the 'BaseShape' class are irrelevant to the technique of polymorphism; BaseShape may containa lot of shared code, or it may be an interface with no shared code. Polymorphism and code-reuse are two separate aspects of inheritance.
I had been doing OOP for nearly 10 years before I realised this distinction.
The next question would be:
Other than inheritance, how else can you re-use code in an OOP fashion?
Herbie
Good points!
We use polymorphism when we are passing objects between the different tiers in our application. Any object that is exposed on the web tier and is passed back to the business tier MUST inherit off of an interface. The business tier and data access tier MUST use the same interface. In this case polymorphism is not used for code reuse but instead to keep our heads on straight. Especially since there are different programmers working in the different tiers.
-
W3bbo wrote:
Well you haven't phrased your question properly. Write it out fully and I'll give it a stab.
I'll do it in the morning; if someone else hasn't done it.
Basically in BaseClass have a vitual method Message which prints "BaseClass". In SubClass1, derived from BaseClass override that method and print SubClass1, in SubClass2 new the method and print SubClass2. Now declare 3 variables of class BaseClass, one an instance of BaseClass, one an instance of SubClass1, one an instance of SubClass2. Call Message on each. What gets printed.
-
pathfinder wrote:In this case polymorphism is not used for code reuse but instead to keep our heads on straight. Especially since there are different programmers working in the different tiers.
Pah, shift to late bound duck typed objects and remove that dependancy on a base object
-
i don't know, sounds very clear to me...W3bbo wrote:
blowdart wrote:

Charles wrote:
What does the keyword virtual mean? When would you use it?
How does new differ from override; best illustrated by a piece of sample code; where each class has a Message method printing the class name; then declare all 3 classes as the base type.
In the last 3 months only 1 person has gotten the final output right.
Well you haven't phrased your question properly. Write it out fully and I'll give it a stab.
-
I like questions that let the candidate and interviewer to run with them. It puts people at ease and it only gets as complicated as you want/need it to. So, in their basic form if the candidate can't even answer it to begin with then you do not have to go farther. But as they progress take them down a path and let the conversation kind of build on itself.
You could white board or on paper (which I hate personally) offer several items and start simple and ask to see them related programmatically. Just a half-dozen objects should be good. Just make sure they are different. So maybe some would be properties like type or color and some are obviously extended from other base classes to anyone who has actually written code. Make sure it's something simple and could go different directions:
1. Helicopter
2. Bird
3. Operator
4. Dump Truck
5. Seagull
6. Green
If they can't see a seagull and extending bird its over. Maybe they pose a question "Is an operator a person? Can they drive and fly?" Whatever. You should be looking intelligence and problem solving abilities. Posing the right questions and then constucting the answers in C#.
You could draw out a scenario all the way to building a small app on a white board. What methods would these objects have? How would they work? When?
-
I'm with Mr. Awesome here. I much prefer asking programmers questions around, "How would you design or program something" rather than really specific nit-pick topics.
There's so much an engineer has to know that it's impossible (for me at least) to keep it all in my head. I'd want to see creativity, how they approach the problem. Who cares what the enums for some specific API are, that's what MSDN is for.
Generally too I find that even just having a casual conversation about software and programming has been enough for me in the past to weed genuine candidates from BS'ers. -
Massif wrote:why "this " + someint.ToString() + " is stupid" is a bad idea
Ooh, good. Are you asking because of the use of ToString() here or because you think there's an inefficinet concat happening--because it's really not. C# 2 will use String.Concat(string,string,string) here. It's probably not any worse than using String.Format.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.