Java Abstract Methods

abstract-class, java

Solution

abstract String orderDescription()
{
    return null;
}

should be

abstract String orderDescription();

As error says, your abstract method declaration shouldn't contain any body.

Above syntax mandates the implementation (which ever class extends the abstract class and provides implementation) to return a String.

You can't instantiate abstract class, so some class need to extend abstract class and provide implementation for this abstract method.

Example:

class MyabsClass 
{
  abstract String orderDescription();
}

class MyImplementation extends MyabsClass
{
   public String orderDescription()
    {
    return "This is description";
    }
}



 class MyClient
   {
     public static void main(String[] args)
      {
         MyImplementation imple = new MyImplementation();
         imple.orderDescription();
      }
   } 

Problem

I am slightly confused with the keyword `abstract` here. My compiler is telling me that I am not allowed to have a body for a method that's abstract. However my assignment says: The abstract method orderDescription() returns a String giving details about a particular order. ``` abstract String orderDescription() { return null; } ``` However my code returns an error, as I mentioned above. So my question is what should I do for this problem? Up to now I've just removed the keyword abstract and it works fine.

Original source

Related problems