SubClass And SuperClass

  • Thread starter Thread starter RajkiranPro
  • Start date Start date
R

RajkiranPro

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
 
RajkiranPro said:
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<Car> you 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
 
The point of polymorphism is that you should not !

By declaring a List<Car> you 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;
//carlist is an Object Of Cars and it is already initialized
Benz b;
Porsche p;

if(obj is Benz)
{
b = obj;
}
else
{
p = obj;
}
 
The point of polymorphism is that you should not !

By declaring a List<Car> you 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...
 
is this piece of code correct


Car obj = carlist;
//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
 
RajkiranPro said:
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
 
Well what is the Exact procedure to do it such that it comes under
best programming practices
 
RajkiranPro said:
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
 
Michael said:
Arne Vajhøj said:
RajkiranPro said:
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<Car> you 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;
//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
 
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.
 
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<Action> actions = 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
 
Back
Top