|
How does this relate to my question?
Jon
|
|
|
|
|
jon_80 wrote: How does this relate to my question?
This question arose because you need further, deeper reading about polymorphism.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks for reading my mind, but I wasn't actually after a tutorial
Jon
|
|
|
|
|
Oh, and where were you actually?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
following through a c# tutorial...
Jon
|
|
|
|
|
Sorry, I misunderstood what you wrote. I perceived it as ironic. Sorry again
Losely speaking, when you use:
GenericCustomer customer = new Nevermore60Customer("Arabel Jones");
you are creating a fully featured class Nevermore60Customer instance. However you can pass it to any method expecting a GenericCustomer as argument (the method hasn't even to know about Nevermore60Customer class esistence!).
On the other hand, the customer instance will act polymorphically, i.e. each method called on it will have the behaviour defined Nevermore60Customer .
Polymorphism is a rather complex concept and I gave a very poor hint to you. But that's
life.
(Anyway there are a lot of good examples on teh topic, you have only to use Google)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Nevermore60Customer : GenericCustomer denotes inheritance.
Because Nevermore60Customer inherits from GenericCustomer, GenericCustomer can hold a reference to Nevermore60Customer, but only the GenericCustomer members would be accessable.
GenericCustomer customer1 = new Nevermore60Customer("Arabel Jones");
Nevermore60Customer customer2 = customer1;
In the example above both customer1 and customer2 reference the newly created Nevermore60Customer object, but customer1 only allows access to Nevermore60Customers inherited members.
There is alot more to inheritance than this though, for some good tutorials google c# inheritance[^] and you'll find better, more indepth explinations.
topcoderjax - Remember, Google is your friend.
|
|
|
|
|
Thanks. So .NET doesn't make a difference on whether the first class is the actual class or its base class?
GenericCustomer customer1 = new Nevermore60Customer("Arabel Jones");
I mean, I would expect an instantiation to be as follows, so how is the above declaration useful?
Nevermore60customer customer1 = new Nevermore60Customer("Arabel Jones");
Jon
|
|
|
|
|
It's analogy time! Let's go with the stereotypical car analogy.
You have a generic abstract class "Car". It's abstract because there's no such thing as just a plain "Car". There are different brands and models and such, but you can't go to a dealership and buy a product called "Car".
public abstract class Car { ... }
Now, you can buy a new BMW, which is a type of Car...
public class BMW : Car { ... }
Or you can buy a Junker, which is also a type of Car...
public class Junker : Car { ... }
Now, either of these has doors, a gas pedal, a brake pedal, and a steering wheel (Keeping it simple)...
public abstract class Car
{
public abstract bool IsDoorOpen { get; set; }
public abstract void Gas();
public abstract void Brakes();
}
Of course, the BMW is a luxury car, so it has anti-lock brakes, an alarm on the door, etc. The junker doesn't. In code terms, the two classes have different implementations of IsDoorOpen and Brakes.
Also, the BMW has a high-tech music player, with all sorts of cool features with a 500-page manual!
public class BMW : Car {
public bool IsDoorOpen { get { ... } set { ... } }
public void Gas() { ... }
public void Brakes() { ... }
public void PlayMusic(Music m, MusicSettings s) { ... }
}
So, now you're a valet, parking a car for someone. You can park any kind of car, and you really don't care what kind of car it is, as long as it has all of the basics...
public class Valet
{
public void ParkCar(Car c) { ... }
}
Valet v = new Valet();
BMW beamer = new BMW();
v.ParkCar(beamer);
Now, you don't know how to work the futuristic music player with all the satellite feeds and Wi-Max mp3 streaming, because you don't know anything about this BMW (Hey, you're just parking it). All you know is how to work the gas, brakes, and door. The car is still a BMW, but you just look at it as an abstract "Car".
Of course, this means you could just as easily park the Junker.
Junker junk = new Junker();
v.ParkCar(junk);
Since you know how to park a "Car" instead of just how to park a "BMW", it doesn't matter which one you're given. Of course, if you hit the brakes on the "Car", they may or may not be anti-lock (BMWs have it, Junkers don't), and the door may or may not have an alarm, and the gas may be more or less effective (BMW and Junker have different implementations of those members), but they're still there, and you know how to use them.
I probably should submit an article with this analogy... But anyway, hope this gives you an idea of why abstracts/interfaces are good. I mean, this was a better example of an interface than an abstract class, but it should get you started.
|
|
|
|
|
|
Ian Shlasko wrote: I probably should submit an article with this analogy
It is a fantastic analogy. You got my 5!
|
|
|
|
|
Very good analogy, much better than mine, but did you really write that analogy on the fly or did you have it tucked away in a file somewhere? If you did it on the fly I'm doubly impressesed Anyway you got my vote. Cheers.
topcoderjax - Remember, Google is your friend.
|
|
|
|
|
On the fly
I was bored, trying to take my mind off a nasty design flaw in my application... Spend too much time on one bug, and you start to zoom in and lose the big picture.
|
|
|
|
|
To go back to a very bad example in school
if you have an animal class and dog cat and fish inherit from animal. It might be useful in some cases to treat them all as animals. In your example if you had several types of customers it might be useful to treat the all as GenericCustomer, or perhaps you might have a method that takes a GenericCustomer as a paramiter, so casting Nevermore60Customer to GenericCustomer and passing it might make sense.
I have to admit though I only skimmed your example, so I can't say for certain if it is needed there or not. Too often tutorial code shows you what can be done without giving you a good reason why.
If you want a good book on when and why to use inheritace I recommend a book called "Design patterns explained" second edition by Alan Shalloway,James Trott. I don't think it is .net specific but the concepts all apply.
topcoderjax - Remember, Google is your friend.
|
|
|
|
|
Well, what do you think? Looks like there are 3 possibilities:
1) it will not compile at all
2) customer will be instance of abstract class (hint hint) GenericCustomer, and you will lose data referrerName and highCostMinutesUsed
3) customer will be instance of Nevermore60Customer
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
The code compiles actually, my question was different...
Jon
|
|
|
|
|
Yes I know it compiles. This leaves you 2 other possibilities, keep trying...
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
"Real men use mspaint for writing code and notepad for designing graphics."
what does this imply?
|
|
|
|
|
Haha it sure is good sig. What it means? Hmm. That "real men" do things the hard way? Or that real men are like Chuck Norris?
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
i dint understand! whts wrong with Chuck Norris?
|
|
|
|
|
Hey all,
You know when you connect a new piece of hardware to a XP computer you get a little bubble/balloon informing you that your "new hardware is installed and ready for use"? Can some one point me in the direction of manipulating that for my own C# app?
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
|
|
|
|
|
|
JF2015. That is perfect. Even better than what I was looking for.
JF2015 <----Legend.
Thanks.
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
|
|
|
|
|
MicealG wrote: Freedom is the right to say that 2+2=5 if this is so everything else will follow.
So who/what keeps you from shouting out loud that 2+2=5 in the UK? Go ahead. See what happens.
The more I see of men, the better I like my dog. Blaise Pascal.
|
|
|
|
|
Its a quote from one of my favourite books 1985.
P-p-p-p-pick up a book some time.
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
|
|
|
|
|