Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2005
    Location
    The Netherlands
    Posts
    94

    Abstract method?

    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

  2. #2
    An abstract method is a method that is declared without an implementation, like this:

    Code:
    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.
    Software Freelancer - custom software solutions

  3. #3
    It should be in a base class.
    For example, Shape:aint(), it could have nothing. You should define a detailed shape and define detailed Paint function for it.

  4. #4
    Join Date
    Apr 2005
    Location
    The Netherlands
    Posts
    94
    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?

  5. #5
    Join Date
    Aug 2002
    Location
    Superior, CO, USA
    Posts
    633
    Quote Originally Posted by deef View Post
    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.
    Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums.

  6. #6
    Join Date
    Sep 2004
    Location
    Chennai , India
    Posts
    4,608
    Quote Originally Posted by rei12 View Post
    An abstract method is a method that is declared without an implementation, like this:

    Code:
    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.

  7. #7
    Join Date
    Aug 2001
    Location
    Central USA
    Posts
    200
    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.
    InvoiceMore - Online Billing & Invoicing
    phpDataMapper - Object-Oriented PHP5 Data Mapper ORM

  8. #8
    Basically that is definition without implementation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •