user982466
user982466

Reputation: 11

Error with instantiation in Java

I'm having issues with the abstract class idea...I've attempted to use Eclipse, but this time the IDE is just messing it up. I've got the files uploaded. They should be attempted to be compiled as this

  1. Container
  2. Base Container
  3. Height, and color exception
  4. Round and square containers
  5. client for containers

I have the files uploaded here: http://bit.ly/nDiObk

Here is the error I'm getting:

ClientForContainers.java:19: SquareBaseContainer is abstract; cannot be instantiated
                BaseContainer aContainer = new SquareBaseContainer(10.0);
                                           ^
ClientForContainers.java:31: RoundBaseContainer is abstract; cannot be instantiated
                aContainer = new RoundBaseContainer(10.0);

Upvotes: 0

Views: 261

Answers (4)

sho222
sho222

Reputation: 722

Abstract classes cannot be instantiated. Extend SquareBaseContainer and RoundBaseContainer and instantiate the subclass instead.

See the java tutorial on abstract classes

Upvotes: 2

David M. Karr
David M. Karr

Reputation: 15215

I don't know why you think the IDE is "just messing it up". Did the IDE write this code?

An abstract class cannot be instantiated. That means you can't do "new AbstractClass()".

Upvotes: 1

Jon Newmuis
Jon Newmuis

Reputation: 26492

The SquareBaseContainer is marked as abstract. Abstract classes cannot be instantiated. They are somewhat like interfaces, except that some methods may be defined, and instance members can be defined.

However, in this case, it appears as if you have defined all methods in the class. If you wish for this class to be instantiable, you should remove the abstract keyword from the class definition.

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

You've declared your subclasses as abstract, too -- don't do that! The base class BaseContainer should be abstract, but then the child classes RoundBaseContainer and SquareBaseContainer, since they provide the missing methods and are intended to be instantiated, should not be.

Upvotes: 9

Related Questions