Hello,
What is an abstract method? I know that it has no implementation (so no code in it and no return value (VOID)), but it does not make sense for me. What exactly is the purpose of it? When do you use it for example?
Thanks
pavelJ
01-07-2008, 07:44 PM
An abstract method is a method that is declared without an implementation, like this:
abstract void moveTo(double deltaX, double deltaY);
A subclass usually provides implementations for all of the abstract methods in its parent class.
It is useful then you have several subclasses which implement the same parent class in different ways.
sagasw
01-07-2008, 10:32 PM
It should be in a base class.
For example, Shape::Paint(), it could have nothing. You should define a detailed shape and define detailed Paint function for it.
So basically you use abstract methods to extend them afterwards in a subclass? And as those abstract methods can have different meanings in other subclasses it has no implementation. So Paint() can have different implementations in subclasses. Correct?
stdunbar
01-08-2008, 11:40 AM
So basically you use abstract methods to extend them afterwards in a subclass? And as those abstract methods can have different meanings in other subclasses it has no implementation. So Paint() can have different implementations in subclasses. Correct?
Exactly. If I had a Shape class that had the paint() method in it then derived classes like Circle or Square could implement their own paint() method. This is known as polymorphism and is a very powerful concept in object oriented programming.
An abstract method is a method that is declared without an implementation, like this:
abstract void moveTo(double deltaX, double deltaY);A subclass usually provides implementations for all of the abstract methods in its parent class.
It is useful then you have several subclasses which implement the same parent class in different ways.
each method in different subclasses may have different implementation. Thats the power.
Czaries
01-09-2008, 11:08 AM
Yes, the point of polymorphism is that the code that calls the function is ignorant of what the actual object is. All it needs to know is to call the Paint() function on the given object, and the object itself will handle all the details.
The point of this is that since there are quite obviously different ways to Paint() different objects, you can contain all that code of the knowledge of how to carry out that function in the actual object itself instead of trying to do it in the code that calls the Paint() function.
This allows you to swap objects in and out very easily later down the road without any changes to your base code. Polymorphism is one of the most powerful concepts in OO programming, because if used properly, it allows your code to become very portable without having to change large chunks of base code.
wukas
02-03-2008, 07:51 PM
Basically that is definition without implementation.