Hi,
How do I decide when I shuld create an Abstract method?
Abstract methods
Page 1 of 14 Replies - 1080 Views - Last Post: 26 January 2007 - 02:09 AM
Replies To: Abstract methods
#2
Re: Abstract methods
Posted 22 January 2007 - 02:37 AM
hope13, on 19 Jan, 2007 - 05:08 PM, said:
Hi,
How do I decide when I shuld create an Abstract method?
How do I decide when I shuld create an Abstract method?
HI
Abstract methods are intended to never be instantiated on their own, sometimes the term base is used in reference to a class that contains an abstract method (though its not always true).
For example, you want to create a class that manages file open/close and some error management, as a "base" class for a series of classes. The base class is made abstract by making one method abstract. This will ensure if you distribute the "object" that attempts to use the base class are picked up by the compiler, preventing the creation of the object unless it is inherited and the abstract method overridden in the derived class.
eg
class FileBase // where it all starts
{
// only opens/closes - manages just open/close problems
FileBase(){};
~FileBase(){};
Open(string FileName);
Read()=0;// Abstract method
ErrorManagement();
}
class FileTextRead : public FileBase // now we nake this a text file reader
{
typedef FileBase Inherited;
FileTextRead(){};
~FileTextRead(){};
Read(); // programmer writes "text" reader method - over rides abstract
}
class FileTextRead : public FileBase // now we nake this a text file reader
{
typedef FileBase Inherited;
FileTextRead(){};
~FileTextRead(){};
Read(); // programmer writes "binary" reader method - over rides abstract
}
class FileBinaryRead : public FileBase // now we nake this a text file reader
{
typedef FileBase Inherited;
FileBinaryRead(){};
~FileBinaryRead(){};
Open(string FileName); // opens/reads "binary" files
}
#3
Re: Abstract methods
Posted 22 January 2007 - 07:44 AM
gregoryH, on 22 Jan, 2007 - 02:37 AM, said:
hope13, on 19 Jan, 2007 - 05:08 PM, said:
Hi,
How do I decide when I shuld create an Abstract method?
How do I decide when I shuld create an Abstract method?
HI
Abstract methods are intended to never be instantiated on their own, sometimes the term base is used in reference to a class that contains an abstract method (though its not always true).
For example, you want to create a class that manages file open/close and some error management, as a "base" class for a series of classes. The base class is made abstract by making one method abstract. This will ensure if you distribute the "object" that attempts to use the base class are picked up by the compiler, preventing the creation of the object unless it is inherited and the abstract method overridden in the derived class.
eg
class FileBase // where it all starts
{
// only opens/closes - manages just open/close problems
FileBase(){};
~FileBase(){};
Open(string FileName);
Read()=0;// Abstract method
ErrorManagement();
}
class FileTextRead : public FileBase // now we nake this a text file reader
{
typedef FileBase Inherited;
FileTextRead(){};
~FileTextRead(){};
Read(); // programmer writes "text" reader method - over rides abstract
}
class FileTextRead : public FileBase // now we nake this a text file reader
{
typedef FileBase Inherited;
FileTextRead(){};
~FileTextRead(){};
Read(); // programmer writes "binary" reader method - over rides abstract
}
class FileBinaryRead : public FileBase // now we nake this a text file reader
{
typedef FileBase Inherited;
FileBinaryRead(){};
~FileBinaryRead(){};
Open(string FileName); // opens/reads "binary" files
}
Just to be clear, this is C++ code in Java Forum
but it's cool cuz both R OOP languages
This post has been edited by PennyBoki: 22 January 2007 - 07:46 AM
#4
Re: Abstract methods
Posted 25 January 2007 - 06:14 PM
If you feel your class can be subclassed and the implementation of a method in that class varies for different subclasses then we need to make this method as abstract.
#5
Re: Abstract methods
Posted 26 January 2007 - 02:09 AM
sreedharsanni, on 26 Jan, 2007 - 02:14 AM, said:
If you feel your class can be subclassed and the implementation of a method in that class varies for different subclasses then we need to make this method as abstract.
I beg to differ. In this case it doesn't have to be abstract, it is possible, that the base class can be instantiated on it's own, too. It must be abstract, if you don't want to, or can not define the method in the base class. An example can be the draw method in a class called Shape. You don't know how to draw a shape, but once you subclass Rectangle or Circle classes from it, then you can define the Draw method completely for them.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|