0

I am learning Java and don't understand why this code generates the following error: "ArrayListClass is abstract; cannot be instantiated. Help would be appreciated.

import java.util.*;

public class ArrayListClass {
    protected Object[] list;
    protected int maxSize;
    protected int length;

    public ArrayListClass() {
      maxSize = 100;
      length = 0;
      list = new Object[maxSize];
    }

    public ArrayListClass(int size) {
      maxSize = size;
      list = new Object[maxSize];
      length = 0;
    }

    public boolean isEmpty() {
      return length == 0;
    }

    public boolean isFull() {
      if (length == maxSize)
         return true;
      else 
         return false;
    }

    public int listSize() {
      return length;
    }

    public int maxListSize(){
      return maxSize;
    }

    public void print() {
      System.out.print("The list contains:");
      for(int i = 0; i < length; i++)
         System.out.print(list[i] + " ");
      System.out.println();
    }

    public boolean isItemAtEqual(int location, Object item) {
      return (list[location].equals(item));
    }

    public void insertEnd(Object item) {
      if(!isFull())
         list[length++] = item;
    }

    public static void main(String [] args) {
        ArrayListClass dac = new ArrayListClass(5);
        dac.insertEnd(4);
        dac.insertEnd(5);
        dac.insertEnd(6);
        dac.print();
        System.out.println("dac.isItemAtEqual(0,9)"+dac.isItemAtEqual(0,9));
        System.out.println("dac.isItemAtEqual(1,9)"+dac.isItemAtEqual(1,9));
    }
}  
8
  • That's the principle of an abstract class. Read docs.oracle.com/javase/tutorial/java/IandI/abstract.html. Also, please, learn to indent your code. Commented Nov 27, 2016 at 16:57
  • You want to convert the ArrayListClass to abstract or you wanted to see the error ? Both are contradictory Commented Nov 27, 2016 at 16:58
  • 2
    ArrayListClass is not abstract and your code runs perfectly, giving the output: The list contains:4 5 6, dac.isItemAtEqual(0,9)false, dac.isItemAtEqual(1,9)false Commented Nov 27, 2016 at 16:59
  • Your class will only be abstract if you mark as 'abstract class' with at least one abstract method. It's not your case. Commented Nov 27, 2016 at 17:07
  • 1
    I mean, that could be a simple as calling your class public abstract class ArrayListClass. Are those the only instructions your professor has provided? Commented Nov 27, 2016 at 17:10

3 Answers 3

1

You can not instantiate any abstract class in any programming language. Basic construct of abstract is, it is merely blueprint, not a real object. It provides the template of a class and will provide the form or outline of the class to the concrete classes that implement the class ('extend' the class...)

So you can not instantiate ArrayListClass, as this gives a blueprint. If you extend this class say DerievedArrayListClass extends ArrayListClass, then you will be able to instantiate DerievedArrayListClass .

package com;

abstract  class ArrayListClass{
    protected Object [] list;
    protected int maxSize;
    protected int length;

    public ArrayListClass(){
        maxSize = 100;
        length = 0;
        list = new Object [maxSize];
    }
    public ArrayListClass(int size){
        maxSize=size;
        list=new Object [maxSize];
        length=0;
    }


    public boolean isEmpty(){
        return length==0;
    }
    public boolean isFull(){
        if(length==maxSize)
            return true;
        else 
            return false;
    }


    public int listSize(){
        return length;
    }

    public int maxListSize(){
        return maxSize;
    }

    abstract void  print();

    public boolean isItemAtEqual(int location, Object item)
    {
        return (list[location].equals(item));
    }
    public void insertEnd(Object item){
        if(!isFull())
            list[length++] = item;
    }

}  

public class ArrayListClassImpl extends ArrayListClass{

    public ArrayListClassImpl(int i) {
        super(i);
    }

    public void print(){
        System.out.print("The list contains:");
        for(int i = 0; i < length; i++)
            System.out.print(list[i] + " ");
        System.out.println();
    }

    public static void main(String [] args){
        ArrayListClass dac = new ArrayListClassImpl(5);
        dac.insertEnd(4);
        dac.insertEnd(5);
        dac.insertEnd(6);
        dac.print();
        System.out.println("dac.isItemAtEqual(0,9)"+dac.isItemAtEqual(0,9));
        System.out.println("dac.isItemAtEqual(1,9)"+dac.isItemAtEqual(1,9));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your code is fine . it is getting compiled and executed without any error on my eclipse and output is : The list contains:4 5 6

dac.isItemAtEqual(0,9) false

dac.isItemAtEqual(1,9) false

Comments

0

If you convert the class to abstract, you must create a separate class that implements your abstract class.

The implementing class must override the unimplemented methods in the abstract class and can optionally override any or all of implemented methods in the abstract class

An abstract class can have a *mix of implemented and unimplemented methods. An interface class can only contain unimplemented methods.

You instantiate the class that implemenents the abstract class, but you can't instantiate the abstract class itself, because abstract classes, and interface classes are considered templates or blueprints that describe the form that the implementation must follow. It's like a recipe. You can't bake the recipe itself, you must bake the ingredients.

Working example of creating, implementing and instantiating an abstract Java class...

Shape.java: abstract class

public abstract class Shape { // Indicates this is an abstract class

   protected static String shapeType = "generic shape";

   abstract void draw(); // Implementing class *MUST* provide (due to 'abstract' keyword)

   void logGreeting() {  // Children can *optionally* override this implementation
        System.out.println("I want to say hello");
   }
   void logDescription() {  // Children can *optionally* override this implementation
        System.out.println("This shape is a " + shapeType);
   } 

}

Circle.java: implementing class

public class Circle extends Shape { // Extends (e.g. implements) abstract class

   public Circle() {
       shapeType = "Circle";
   }

   public void logGreeting() { // Overrides implementation already in abstract class
       System.out.println("This is my overridden greeting message");
   }

   public void draw() { // Provides implementation for *unimplemented* abstract method
       // This is a NOP for example only (normally you'd put code here)
   }

}

TestAbstract.java: instantiating class

public class TestAbstract extends Circle {

     public static void main(String args[]) {
          Circle circle = new Circle(); // instantiates implementing class
          circle.logGreeting();
          circle.logDescription();
          circle.draw(); 
}

Compile the code:

javac Shape.java
javac Circle.java
javac TestAbstract.java

Execute the code:

java TestAbstract.java

Output:

This is my overridden greeting message
This shape is a Circle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.