problem implenting abstract classes

R

Rich P

How to implement these abstract classes?


abstract class Car
{
public abstract string Model { get; set;}
public abstract string Make { get; set;}
CarType carTypes { get; set;}
}

abstract class CarType
{
public abstract string twoDoor { get; set;}
public abstract string FourDoor { get; set;}
}
---------------------------------------------


I tried abstract class Car:CarType with no success.
{
...
}

Then I want to derive a class from these 2 abstract classes. What is
the correct way to do this?

class myDerived:Car
{
...
}

Rich
 
P

Peter Duniho

How to implement these abstract classes?

You have to provide a class that inherits the abstract class, and which
contains an implementation of every abstract member in the abstract class.
abstract class Car
{
public abstract string Model { get; set;}
public abstract string Make { get; set;}
CarType carTypes { get; set;}
}

For example:

class myDerived : Car
{
public string Model { get; set; }
public string Make { get; set; }
}
abstract class CarType
{
public abstract string twoDoor { get; set;}
public abstract string FourDoor { get; set;}
}
---------------------------------------------


I tried abstract class Car:CarType with no success.
{
..
}

Define "no success". What did you actually try? What happened? What did
you expect to happen instead?
Then I want to derive a class from these 2 abstract classes. What is
the correct way to do this?

class myDerived:Car
{
..
}

See my example above. Also see the documentation:
http://msdn.microsoft.com/en-us/library/sf985hc5.aspx

Finally, note that it's not really clear from your examples why you want
to use an abstract class. Not that you definitely shouldn't be, but it
seems like "Model" and "Make" would be properties common to any Car, and
implemented the same way for each. Thus, it seems like a non-abstract
base class "Car" would be fine.

Pete
 
R

Rich P

Define "no success". What did you actually try? What happened? What did
you expect to happen instead?
Then I want to derive a class from these 2 abstract classes. What is
the correct way to do this?

class myDerived:Car
{
..
}

See my example above. Also see the documentation:
http://msdn.microsoft.com/en-us/library/sf985hc5.aspx

Finally, note that it's not really clear from your examples why you want
to use an abstract class. Not that you definitely shouldn't be, but it
seems like "Model" and "Make" would be properties common to any Car, and
implemented the same way for each. Thus, it seems like a non-abstract
base class "Car" would be fine.
<<

Thank you for your reply. This is basically an excersize. I am sure
that abstract is not needed in this case, but I don't even know how to
work with abstract classes. Once I figure that out I will then try to
determine what would be a need to use my own custom abstract classes.
In the meantime (getting ready to leave the shop here) I will go over
your post.



Rich
 
P

Peter Duniho

[...]
Thank you for your reply. This is basically an excersize. I am sure
that abstract is not needed in this case, but I don't even know how to
work with abstract classes. Once I figure that out I will then try to
determine what would be a need to use my own custom abstract classes.
[...]

Okay, well...in addition to the various documentation you might find, I'll
state the brief description of an abstract class: it's basically an
interface that comes with some implementation already done.

Because it's a class, abstract should be used sparingly; you can only
inherit one class in C#, but as many interfaces as you want. But it can
be very helpful in situations where you have a class that you know always
needs to be extended/inherited, and yet for which you can provide a solid
base implementation as a starting point for inheritors.

Pete
 
R

Rich P

See my example above. Also see the documentation:
http://msdn.microsoft.com/en-us/library/sf985hc5.aspx
<<

Thanks again for the help and the article. This has cleared up a lot
and also explained what a sealed class is and how it is the opposite of
an abstract class. Let me regurgitate to make sure I have it straight:
you can inherit from an abstract class but you cannot instantiate it.
You cannot inherit from a sealed class.

Rich
 
P

Peter Duniho

See my example above. Also see the documentation:
http://msdn.microsoft.com/en-us/library/sf985hc5.aspx
<<

Thanks again for the help and the article. This has cleared up a lot
and also explained what a sealed class is and how it is the opposite of
an abstract class. Let me regurgitate to make sure I have it straight:
you can inherit from an abstract class but you cannot instantiate it.
You cannot inherit from a sealed class.

Those two statements are correct, yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top