473,395 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

SubClass And SuperClass

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
12 5735
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
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
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
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
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
Well what is the Exact procedure to do it such that it comes under
best programming practices
Jul 6 '08 #7
"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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
0
by: flupke | last post by:
I have the following test code setup, trying to get the class name of a subclass in the super class. (Reason why i want this is described below) file class_name_start.py ========================...
4
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
5
by: spike grobstein | last post by:
So, I've got this project I'm working on where the app defines various classes that are subclassed by module packages that act like plugins... I'd like the packages to define a file path for...
4
by: ingoweiss | last post by:
Hi, I am having trouble passing parameters of a Javascript subclass constructor through to it's superclass constructor. I am trying all sorts of things, including the below, but nothing...
1
by: s.lipnevich | last post by:
Hi All, Is anything wrong with the following code? class Superclass(object): def __new__(cls): # Questioning the statement below return super(Superclass, cls).__new__(Subclass) class...
1
by: shivapadma | last post by:
1..In java we can refer superclass constructor by super()keyword,but Where as in c++ how can we refer to that???. In java the following code is used for referring superclass...
2
by: Mark Brading | last post by:
Hi all, I am trying to declare a Map object with an abstract superclass (TableFields_v2) object as follows:- private Map<Integer, TableFields_v2> elementList; When I come to create the object...
3
by: bassman2112 | last post by:
I'm having issues with calling a method defined in a subclass on a superclass object. This program is an exercise using inheritance, with an Employee superclass, Salaried and Hourly classes that...
1
by: jimismint | last post by:
Hi guys, i'm stuck on this problem. only just recently started coding java using blue J. Can you guys help me. import java.util.*;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.