473,382 Members | 1,786 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,382 software developers and data experts.

Abstract Class Design

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
5 1601

"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
Can you give me a real time example where i can do this
Thanks in Advance

thomso

May 11 '06 #3
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an...
2
by: Yasutaka Ito | last post by:
Hi folks! I have a BaseForm class that inherits System.Windows.Forms.Form. It has a property, whose value I need supplied by the class that inherits it. The BaseForm usees the value supplied...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
1
by: J.Marsch | last post by:
I am trying to create a base form that all of our application's forms will inherit from. I would like to mark it "abstract", but if I do, I get an error from the form designer when I try to design...
6
by: steve bull | last post by:
I created a usercontrol class, RGBColorSpace, which is derived from an abstract class, ColorSpace, but when I try to click on the design panel for the control I get an error message "Unable to...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
7
by: ankitjain.bvcoe | last post by:
Hi i have the following problem in my design :::: i want to define an abstract class LogBuffer and derive two singleton classes from it i.e AlarmBuffer and FireWallBuffer.For this my design is...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
2
by: Rahul | last post by:
Hey Guys Just go easy on me . I have an abstract class which is pure is a kind of asmx. I know the answer that I am asking but still need somebody's advice. Since it's an abstract class, the...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.