4 Replies - 1160 Views - Last Post: 26 January 2007 - 02:09 AM Rate Topic: -----

#1 hope13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-January 07

Abstract methods

Posted 19 January 2007 - 05:08 PM

Hi,
How do I decide when I shuld create an Abstract method?
Is This A Good Question/Topic? 0
  • +

Replies To: Abstract methods

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Abstract methods

Posted 22 January 2007 - 02:37 AM

View Posthope13, on 19 Jan, 2007 - 05:08 PM, said:

Hi,
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
}

Was This Post Helpful? 0
  • +
  • -

#3 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Abstract methods

Posted 22 January 2007 - 07:44 AM

View PostgregoryH, on 22 Jan, 2007 - 02:37 AM, said:

View Posthope13, on 19 Jan, 2007 - 05:08 PM, said:

Hi,
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

Was This Post Helpful? 0
  • +
  • -

#4 sreedharsanni  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-January 07

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.
Was This Post Helpful? 0
  • +
  • -

#5 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Abstract methods

Posted 26 January 2007 - 02:09 AM

View Postsreedharsanni, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1