By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,449 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,449 IT Pros & Developers. It's quick & easy.

SubClass And SuperClass

P: n/a
well i have the following classes

class Car
{
//some piece of code
}

class Benz : Car
{
//some piece of code
}

class Porsche : Car
{
//some piece of code
}

public class Cars : List<Car>
{
//some piece of code
}

now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..

Please Help Me

Thanks In Advance

Rajkiran
Jul 6 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
RajkiranPro wrote:
well i have the following classes

class Car
{
//some piece of code
}

class Benz : Car
{
//some piece of code
}

class Porsche : Car
{
//some piece of code
}

public class Cars : List<Car>
{
//some piece of code
}

now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..
The point of polymorphism is that you should not !

By declaring a List<Caryou express an intent to store all kinds
of Car's in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.

You *can* test with:
if(o is Benz)
but don't !

Arne

Jul 6 '08 #2

P: n/a
On Jul 6, 6:55*am, Arne Vajh°j <a...@vajhoej.dkwrote:
RajkiranPro wrote:
well i have the following classes
class Car
{
*//some piece of code
}
class Benz : Car
{
//some piece of code
}
class Porsche : Car
{
//some piece of code
}
public class Cars : List<Car>
{
//some piece of code
}
now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..

The point of polymorphism is that you should not !

By declaring a List<Caryou express an intent to store all kinds
of Car's in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.

You *can* test with:
* * if(o is Benz)
but don't !

Arne
Thank you for the reply..

is this piece of code correct
Car obj = carlist[i];
//carlist is an Object Of Cars and it is already initialized
Benz b;
Porsche p;

if(obj is Benz)
{
b = obj;
}
else
{
p = obj;
}

Jul 6 '08 #3

P: n/a
On Jul 6, 6:55*am, Arne Vajh°j <a...@vajhoej.dkwrote:
RajkiranPro wrote:
well i have the following classes
class Car
{
*//some piece of code
}
class Benz : Car
{
//some piece of code
}
class Porsche : Car
{
//some piece of code
}
public class Cars : List<Car>
{
//some piece of code
}
now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..

The point of polymorphism is that you should not !

By declaring a List<Caryou express an intent to store all kinds
of Car's in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.

You *can* test with:
* * if(o is Benz)
but don't !

Arne
Is there any other way to find the type of object using the
System.Reflection.... If yes how...
Jul 6 '08 #4

P: n/a
RajkiranPro wrote:
On Jul 6, 6:55 am, Arne Vajh°j <a...@vajhoej.dkwrote:
>RajkiranPro wrote:
>>well i have the following classes
class Car
{
//some piece of code
}
class Benz : Car
{
//some piece of code
}
class Porsche : Car
{
//some piece of code
}
public class Cars : List<Car>
{
//some piece of code
}
now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..
The point of polymorphism is that you should not !

By declaring a List<Caryou express an intent to store all kinds
of Car's in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.

You *can* test with:
if(o is Benz)
but don't !
is this piece of code correct
Car obj = carlist[i];
//carlist is an Object Of Cars and it is already initialized
Benz b;
Porsche p;

if(obj is Benz)
{
b = obj;
}
else
{
p = obj;
}
You need to cast when assigning. Then it should compile.

But you will not pass your OOP exam with it.

Arne
Jul 6 '08 #5

P: n/a
RajkiranPro wrote:
On Jul 6, 6:55 am, Arne Vajh°j <a...@vajhoej.dkwrote:
>RajkiranPro wrote:
>>well i have the following classes
class Car
{
//some piece of code
}
class Benz : Car
{
//some piece of code
}
class Porsche : Car
{
//some piece of code
}
public class Cars : List<Car>
{
//some piece of code
}
now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..
The point of polymorphism is that you should not !

By declaring a List<Caryou express an intent to store all kinds
of Car's in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.

You *can* test with:
if(o is Benz)
but don't !

Is there any other way to find the type of object using the
System.Reflection.... If yes how...
if(o.GetType().Name == "Benz")

It is just as ugly and I wpuld not be surprised if it was even
slower.

[it is also slightly different in functionality since "is" will
match with sub classes of Benz also]

Arne
Jul 6 '08 #6

P: n/a
Well what is the Exact procedure to do it such that it comes under
best programming practices
Jul 6 '08 #7

P: n/a
"Arne Vajh°j" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
RajkiranPro wrote:
>On Jul 6, 6:55 am, Arne Vajh°j <a...@vajhoej.dkwrote:
>>RajkiranPro wrote:
well i have the following classes
class Car
{
//some piece of code
}
class Benz : Car
{
//some piece of code
}
class Porsche : Car
{
//some piece of code
}
public class Cars : List<Car>
{
//some piece of code
}
now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..
The point of polymorphism is that you should not !

By declaring a List<Caryou express an intent to store all kinds
of Car's in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.

You *can* test with:
if(o is Benz)
but don't !
>is this piece of code correct
Car obj = carlist[i];
//carlist is an Object Of Cars and it is already initialized
Benz b;
Porsche p;

if(obj is Benz)
{
b = obj;
}
else
{
p = obj;
}

You need to cast when assigning. Then it should compile.

But you will not pass your OOP exam with it.

Arne
Maybe not, but he'll be able to put both Mercedes Benz and Porches on the
same car carrier with it, which is definitely more important from a
standpoint of shipping to your local European Sports Cars dealer.

Mike.
Jul 6 '08 #8

P: n/a
RajkiranPro wrote:
Well what is the Exact procedure to do it such that it comes under
best programming practices
Use the capabilities of polymorphism and create methods
virtual/abstract that you can override/implement in the
sub classes.

Arne
Jul 6 '08 #9

P: n/a
Michael D. Ober wrote:
"Arne Vajh°j" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
>RajkiranPro wrote:
>>On Jul 6, 6:55 am, Arne Vajh°j <a...@vajhoej.dkwrote:
RajkiranPro wrote:
well i have the following classes
class Car
{
//some piece of code
}
class Benz : Car
{
//some piece of code
}
class Porsche : Car
{
//some piece of code
}
public class Cars : List<Car>
{
//some piece of code
}
now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..
The point of polymorphism is that you should not !

By declaring a List<Caryou express an intent to store all kinds
of Car's in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.

You *can* test with:
if(o is Benz)
but don't !
>>is this piece of code correct
Car obj = carlist[i];
//carlist is an Object Of Cars and it is already initialized
Benz b;
Porsche p;

if(obj is Benz)
{
b = obj;
}
else
{
p = obj;
}

You need to cast when assigning. Then it should compile.

But you will not pass your OOP exam with it.

Maybe not, but he'll be able to put both Mercedes Benz and Porches on
the same car carrier with it, which is definitely more important from a
standpoint of shipping to your local European Sports Cars dealer.
He can get both on the carrier without testing on which subclass
an instance is.

Arne
Jul 6 '08 #10

P: n/a
Maybe not, but he'll be able to put both Mercedes Benz and Porches on the
same car carrier with it, which is definitely more important from a
standpoint of shipping to your local European Sports Cars dealer.
[Car] (Cars) *------1 (Model) [Model]
[Model] (Models) *----1 (Manufacturer) [Manufacturer]

More importantly now he can have a generic Car which identifies which
manufacturer/model it is. This is a much better way of doing it.

Jul 6 '08 #11

P: n/a
A better example is this

public abstract class Action
{
public abstract void Execute();
}

public class BackupDB : Action
{
public override void Execute()
{
Console.WriteLine("Backing up the DB");
}
}

public class RestoreDB : Action
{
public override void Execute()
{
Console.WriteLine("Restoring the DB");
}
}

public class SomethingImportant : Action
{
public override void Execute()
{
Console.WriteLine("Doing something important");
}
}
List<Actionactions = new List<Action>();
actions.Add(new BackupDB());
actions.Add(new RestoreDB());
actions.Add(new SomethingImportant());

then either

foreach (Action currentAction in actions)
currentAction.Execute();

or

actions.ForEach(a =a.Execute());
Pete
"RajkiranPro" <Ra*********@gmail.comwrote in message
news:18**********************************@x41g2000 hsb.googlegroups.com...
well i have the following classes

class Car
{
//some piece of code
}

class Benz : Car
{
//some piece of code
}

class Porsche : Car
{
//some piece of code
}

public class Cars : List<Car>
{
//some piece of code
}

now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..

Please Help Me

Thanks In Advance

Rajkiran
Jul 6 '08 #12

P: n/a
"Arne Vajh°j" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
Michael D. Ober wrote:
>"Arne Vajh°j" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk. ..
>>RajkiranPro wrote:
On Jul 6, 6:55 am, Arne Vajh°j <a...@vajhoej.dkwrote:
RajkiranPro wrote:
>well i have the following classes
>class Car
>{
> //some piece of code
>}
>class Benz : Car
>{
>//some piece of code
>}
>class Porsche : Car
>{
>//some piece of code
>}
>public class Cars : List<Car>
>{
>//some piece of code
>}
>now in the object of the class Cars i can add objects of classes Benz
>And Porsche... However when accessing the objects in the list how can
>i identify whether they Belong to Benz or Porsche..
The point of polymorphism is that you should not !
>
By declaring a List<Caryou express an intent to store all kinds
of Car's in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.
>
You *can* test with:
if(o is Benz)
but don't !

is this piece of code correct
Car obj = carlist[i];
//carlist is an Object Of Cars and it is already initialized
Benz b;
Porsche p;

if(obj is Benz)
{
b = obj;
}
else
{
p = obj;
}

You need to cast when assigning. Then it should compile.

But you will not pass your OOP exam with it.

Maybe not, but he'll be able to put both Mercedes Benz and Porches on the
same car carrier with it, which is definitely more important from a
standpoint of shipping to your local European Sports Cars dealer.

He can get both on the carrier without testing on which subclass
an instance is.

Arne
When the car carrier gets there, the dealer will need to test to update
their inventory. My point is that OOP must be tempered with real world
requirements.

Mike.
Jul 6 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.