Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class instances with unique implamentation

Status
Not open for further replies.

Yesuslave

Programmer
Sep 24, 2003
28
US
Hi all,

OK, I'm trying to find out whether or not this is possible. What I want to do is have a general class where a basic list of methods are predefined, let's say "Car." Car will have the "Start" method, the "Accelerate" method, and the "Turn lights on" method.

Now, I want all instances of Car to have the same basic methods, however, when I instantiate a particular instance of car I want to override the "Turn lights on" method with a more complicated method that checks other factors.

Now, if I was going to make a whole class of these cars that have a different way of checking the "Turn lights on" method, I would make a class and inherit from Car. However, I am only going to make one with that particular difference. I will be making a multitude of these "almost-cars" that share, for the most part, the same methods and implamentation. However, none of the methods will always be the same, and all of them could change slightly instance to instance.

Is there a way, at instantiation, to override the standard method in the class? Something like:

Dim SpyCar as Car
SpyCar = New Car
Override SpyCar.TurnLightsOn()


I know that probably looks nothing like the actual syntax would (if there even is a syntax for this), but I've put it there just to give you an idea of what I'm thinking.

Thanks very much,
Joshua Wise
 
I would:

1. Make a Class of type car.
2. Make a Class of type SpyCar
3. Override method in new SpyCar class
Code:
Public Overrides Sub TurnLightsOn()
     'Do Stuff
End Sub
4. In your code where you are creating these objects, declare the appropriate type.
5. A SpyCar will still be a Car.
 
So, bascially, for every object I create, if its implamentation is even slightly different, I need a new class? Therefore, if I had 50 cars, and each car had all of the same features except how to turn the lights on, I would need 50 different classes?
 
OK, I assumed that I would have to implament a class for every different type. The problem is that it just seems wasteful, as each unique object will only be instantiated once. There will be a large number of just ordinary cars, but (for argument's sake) there will only be one SpyCar, one Humvee (with a different Start method), one PoliceCar (with a different accelearate method) and so on.

I guess, in the end, it's almost the same amount of coding though.

Thanks for your help,
Joshua Wise
 
It's not really wasteful. How many times do you only have one of each object on a form? This is an ideal situation for Inheritance IMO.

Nope.

You need a new Class that inherits from car.

John, that's what I meant with #2 if you were talking to me.
 
OK, I assumed that I would have to implament a class for every different type"

In .Net Framework OO Model, Class and Type are synonomous.

There is only one type of police Car or only one instance?

I doubt it. And Police car might have to be an Interface because a Police Car would have to inherit from more than one model, Crown Victoria, SUV, Chevy Lumina etc.

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
And Police car might have to be an Interface because a Police Car would have to inherit from more than one model, Crown Victoria, SUV, Chevy Lumina etc.

Don't forget the special edition Z28 and Mustang 5.0. highway patrol cars. [thumbsup]
 
John...

I see your point with "class" and "type." I will very likely have only one "type" of PoliceCar and only one Instance of Police Car. This is why it seemed somewhat wasteful to me.

To have a class for each slightly different car which only gets instantiated once seems a bit wasteful. And yes, each class will definately inherrit from Car, or from some child of car (thus a Crown Vic, or SUV, or even Z28, can be intermediate classes between Car and Police Car).

Its just the One Class - One Instance that seems a bit wasteful to me. However, I guess I can't avoide it.

 
thus a Crown Vic, or SUV, or even Z28, can be intermediate classes between Car and Police Car"

Actually, that can not be true, because then a Police Car would have to inherit from multiple classes, which is not allowed. That s why I said a "Police Car" may have to be an interface that a CrownVicPolicecar could implelment.

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Nope. They have Z28s in the Nevada Highway Patrol fleet painted patrol colors, and California used to have the same with 5.0 Mustangs.
 
Actually, they can be intermediate classes. If you implament it that the Z28 inherrits the Car class, and then the PoliceCar inherrits the Z28 class. That's what I meant by intermediate classes.

Class Car
End class

Class Z28
Inherrits Car
End class

Class PoliceCar
Inherrits Z28
End Class
 
No....I haven't been on those videos....Driving for long stretches in rural desert gets boring....so one notices the colors of cop cars, along with the particular location of rocks and bushes, etc.
 
Actually, they can be intermediate classes. If you implament it that the Z28 inherrits the Car class, and then the PoliceCar inherrits the Z28 class. That's what I meant by intermediate classes.

But what when you want to make a Crown Vic cop car....It won't work that way. In this case, the model of car, and the cop/citizen type are two totally separate things, not dependant on each other. Thefore, one shouldn't inherit from the other.
 
I was only giving an example of how you can inherrit from more than one class without using an interface. I'm not a particular fan of interfaces, as I so far haven't really seen their usefulness in my own programming.

I get that they help you to create a uniform way of calling a method across classes, but since the don't actually allow you to give code defining the method, I don't see much use for them so far.

Of course, I may just be missing the point entirely of Interfaces.

Thanks!
 
Whoa.
"If you implament it that the Z28 inherrits the Car class, and then the PoliceCar inherrits the Z28 class"

If the preceding statement is true then a Crown Victoria can not be a Police Car because Police car has already inherited Z28.



Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top