Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Today's Posts
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 15-04-2005, 09:23 AM   #1
Jim6
Abandonia nerd

 
Join Date: Mar 2005
Location: ,
Posts: 77
Send a message via AIM to Jim6 Send a message via Yahoo to Jim6
Default

I'm sitting in a lecture right now, and I'm wondering - what's the point of Abstract Classes*?





* Ones that can not, or should never be created
Jim6 is offline                         Send a private message to Jim6
Reply With Quote
Old 15-04-2005, 09:34 AM   #2
Data
retired
 
Data's Avatar


 
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
Default

inherritance
so you can create 2 classes which share the same base.
which is handy if you have virtual functions
__________________
Flowing with the stream of life
Data is offline                         Send a private message to Data
Reply With Quote
Old 15-04-2005, 09:43 AM   #3
Marek
Abandonia nerd

 
Join Date: Mar 2005
Location: ,
Posts: 72
Default

I use them all the time.

If you have a whole bunch of classes with properties or methods in common then put those properties or methods in an abstract base class.
That base abstract class can have both abstract and non abstract methods.

So when you derive from the base abstract class you can effectively force your derived classes to behave in a certain way…

i.e. tell them which functions to override and which ones to not override…

That’s how I use them anyway
:whistle:
Marek is offline                         Send a private message to Marek
Reply With Quote
Old 15-04-2005, 11:21 AM   #4
Reup
10 GOSUB Abandonia
20 GOTO 10
 
Reup's Avatar

 
Join Date: Dec 2004
Location: Eindhoven, Netherlands
Posts: 1,508
Default

If you translate them to the real world:

'Vehicle' could be an abstract class, since nowhere in the world would you find a 'vehicle' without some further specilisation.
You'd find a 'Car', which would be a certain implementation of the abstract 'Vehicle' class or a 'Bike', or 'Airplane'.

so you'd have

Code:
class Car extends Vehicle
class Bike extends Vehicle
etc.

EDIT: This way you can define some behaviour or properties of a class without actually specifying the implementation. E.g. all vehicle have a speed, some sort of propulsion mechanism etc.

PS. You could also do this with an interface (say 'Drivable') but that's another way to model the world.
Reup is offline                         Send a private message to Reup
Reply With Quote
Old 15-04-2005, 11:37 AM   #5
Marek
Abandonia nerd

 
Join Date: Mar 2005
Location: ,
Posts: 72
Default

Quote:
You could also do this with an interface
Yes and what make abstract classes even better is that it can perform the role of both an interface and a normal base class in one, in that common functions can actually be performed in the abstract class itself, like:

Code:
public void ChangeDirection(DirectionTypeEnum oDir)
{
 * //Implementation here
}
or something like that...

Abstract classes are cool...
Marek is offline                         Send a private message to Marek
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do You Like Idea Of Online Classes? Rogue Blah, blah, blah... 14 30-11-2004 03:30 PM


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 11:12 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.