Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

computer science: (Java) error keeps showing, Can someone help me fix it?

Take a look at this code:

public class MainClass

{

public static void main( String [] args )

{

Startable obj = new Startable( );

int n = 5;

runForAWhile( obj, n );

}

public static void runForAWhile( Startable obj, int n)

{

obj.start( );

obj.step ( n );

obj.stop ();

}

When I execute this, the error:

"MainClass.java:9: Startable is abstract; cannot be instantiated"

The lab question that I solved is:

The public static method runForAWhile inputs a Startable object and an int n, and then

calls its start method;

calls its step method n times; and finally

calls its stop method.

Complete the following definition of runForAWhile. (which is the above code)

How can I fix this error??

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    You can not create an object of type Startable because you must have declared it to be abstract. If you want it to remain abstract, you need to create a new class which inherits from Startable (and provides the necessary code). You can then create an object of the new class.

  • 1 decade ago

    Since Startable is abstract you cannot create an instance of it directly, you need to create a subclass of it and use that instead. In my sources I have a link from the Oracle website that goes into detail about abstract classes and has an example of creating a subclass for them, this should help you with your error.

    Source(s): Covers abstract classes: http://download.oracle.com/javase/tutorial/java/Ia...
Still have questions? Get your answers by asking now.