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

Abstract Class Design

P: n/a
Hi
Some design queries on Abstract Class

abstract Class thomson
{
abstract void MyMethod();

virtual void NewMethod()
{

}

}
When in the base class i extend the above Abstract class , you have to
implement the MyMethod , and may be you can override the NewMethod if
necessary,

But Both looks the same thing , the class which extends can make use of
the two methods,
Why this, Can anyone letme know, why both these things
Thanks in Advance

thomson

May 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"thomson" <sa**********@yahoo.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
Hi
Some design queries on Abstract Class

abstract Class thomson
{
abstract void MyMethod();

virtual void NewMethod()
{

}

}
When in the base class i extend the above Abstract class , you have to
implement the MyMethod , and may be you can override the NewMethod if
necessary,

But Both looks the same thing , the class which extends can make use of
the two methods,
Why this, Can anyone letme know, why both these things


It's the difference between "CAN override virtual method" and "MUST
implement abstract method".

If there is a sensible default implementation then it is common to go for
virtual methods otherwise you need abstract methods.
May 11 '06 #2

P: n/a
Can you give me a real time example where i can do this
Thanks in Advance

thomso

May 11 '06 #3

P: n/a
See there http://www.codeproject.com/csharp/jmabstractclasses.asp
Can you give me a real time example where i can do this


--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

May 11 '06 #4

P: n/a
public class Dog {
public virtual void Speak() {
Console.WriteLine("Woof");
}
}

most our dogs just go woof ...

public class Rottweiler : Dog {}
public class Mastiff : Dog {}
public class Shitzu : Dog {}

some need to do something completely different than the default
public class DogThatThinksItIsACat : Dog {
public override void Speak() {
Console.WriteLine("Meow");
}
}

some need to extend the default
public class TalkativeDog : Dog {
public override void Speak() {
Console.WriteLine("WoofWoofWoof");
base.Speak();
}
}

In this case 99% of the dogs will go "woof" so implementing the same method
in every one of their derived classes would not make alot of sense ...
however when we get to one that needs to change or extend the default
behavior we can override the default and provide our own.

Cheers,

Greg Young
MVP - C#
"thomson" <sa**********@yahoo.com> wrote in message
news:11*********************@q12g2000cwa.googlegro ups.com...
Can you give me a real time example where i can do this
Thanks in Advance

thomso

May 11 '06 #5

P: n/a
"Greg Young" <Dr*************@hotmail.com> a écrit dans le message de news:
e%****************@TK2MSFTNGP02.phx.gbl...

| public class Dog {
| public virtual void Speak() {
| Console.WriteLine("Woof");
| }
| }
|
| most our dogs just go woof ...

That may explain virtual methods, but abstract methods are akin to declaring
:

public class Animal
{
public abstract void Speak();
}

All animals speak, abstract says we don't know how at this level of
inheritance.

public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Woof");
}
}

Then, because override also implies virtual, we can then go on to declare
subclasses of Dog that may speak in a non-default manner as in Greg's
example.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.