0

I am having trouble adding my subclasses: Martian and Saturner to my ArrayList within the driver.

I am getting a "cannot find symbol error", any reason as to why this may be?

import java.util.*;
import java.io.*;
abstract class Alien
{
    private String planet;
    private String venus;
    private String name;
    private String skinColor;
    private String alienClass;

public Alien(String planet, String venus, String name, String skinColor, String alienClass)
{
    this.planet=planet;
    this.venus=venus;
    this.name=name;
    this.skinColor=skinColor;
    this.alienClass=alienClass;
}


abstract void invade();

public String toString()
{
    return name + "has" + skinColor + "skin, and is apart of the " + alienClass + "class" ;
}

public abstract class Martian extends Alien
{
    public Martian(String p, String v, String n, String s, String a)
    {
        super(p,v,n,s,a);

    }
}

public abstract class Saturner extends Alien
{
    public Saturner(String p, String v, String n, String s, String a)
    {
        super(p,v,n,s,a);
    }

        abstract void invade();
        {
            System.out.println("The aliens are invading: ");
        }
}

}
 public class AlienDriver
 {
    public static void main(String[]args)
    {


    ArrayList<Alien> alienList = new ArrayList<>();

    alienList.add(new Martian("Hookrah"));

}

THIS IS IRRELEVANT I am adding more details to this post because it is mostly code but this is reall irrelevant and I am just typing this because I have to. My question is clear and concise and it is not much code, I don't see the problem to be quite honest.

2
  • Give us the actual compiler error. Commented Apr 13, 2017 at 18:00
  • The error says "AlienDriver.java.60: error: cannot find symbol alienList.add(new Martian("Hookrah")); symbol: class Martian location: class AlienDriver 1 error " Commented Apr 13, 2017 at 18:10

2 Answers 2

1

All your classes are abstract classes, so they can't be instantiated. In your case: override invade() method in Alien subclasses and remove 'abstract' keyword.

public class AlienDriver { // only one class have 'public' keyword in a file
    public static void main(String[] args){
        // do your stuff
    }
}

abstract class Alien {
    // constructor
    ...
    abstract void invade(); // abstract method
}

class Martian extends Alien {
    // constructor
    ...
    @Override
    void invade() {
        // do something such as print line
    }
}

// Same for Saturner

Hope that help.

Sign up to request clarification or add additional context in comments.

3 Comments

remove abstract keyword from superclass and subclasses? and by implement invade() do you mean like this "public abstract class Saturner extends Alien implements invade()" ?
@KelOkekpe: No. Look in to your code, there're several errors. First, you have to move all classes (Alien and its subclasses) out of AlienDriver. Because in java, there's only one public class in a file, so remove 'public' keyword from other classes (except AlienDriver). Second, remove 'abstract' keyword from Martian and Saturner, because you want to extend them from Alien. Finally, override abstract method invade() for each Alien subclasses.
Wow thanks, removing public from the subclasses did the job, thank you Karen
0

Maybe i'm missing it, but it appears you are trying to use a constructor that takes one argument

new Martian("Hookrah")

But you're defined constructor takes 5 arguments

public Martian(String p, String v, String n, String s, String a)
{
    super(p,v,n,s,a);

}

Edit:

Following up on our discussion in the comments, here's the example code that should work

Alien.java

abstract class Alien
{
private String planet;
private String venus;
private String name;
private String skinColor;
private String alienClass;

public Alien(String planet, String venus, String name, String skinColor, String alienClass)
{
    this.planet=planet;
    this.venus=venus;
    this.name=name;
    this.skinColor=skinColor;
    this.alienClass=alienClass;
}


abstract void invade();
public String toString()
{
    return name + " has " + skinColor + " skin, and is apart of the " + alienClass + " class" ;
}
}

Martian.java

public class Martian extends Alien
{
public Martian(String p, String v, String n, String s, String a)
{
    super(p,v,n,s,a);

}

@Override
void invade() {
    // TODO Auto-generated method stub

}
}

Saturner.java

public class Saturner extends Alien
{
public Saturner(String p, String v, String n, String s, String a)
{
    super(p,v,n,s,a);
}

    void invade()
    {
        System.out.println("The aliens are invading: ");
    }
}

AlienDriver.java

import java.util.ArrayList;

public class AlienDriver
{
public static void main(String[]args)
{


    ArrayList<Alien> alienList = new ArrayList<>();

    alienList.add(new Martian("MarPlanet", "MarVenus", "MarName", "MarSkinColor", "MarAlienClass"));
    alienList.add(new Saturner("SatPlanet", "SatVenus", "SatName", "SatSkinColor", "SatAlienClass"));
    for (Alien al : alienList) {
        System.out.println("Class of Object: " + al.getClass().getName() + " || description = " + al.toString());
    }
}
}

The output is:

Class of Object: Martian || description = MarName has MarSkinColor skin, and is a part of the MarAlienClass class
Class of Object: Saturner || description = SatName has SatSkinColor skin, and is a part of the SatAlienClass class

I'm not sure if you're using an IDE (like Eclipse), but I would highly recommend it for helping with debugging. In this example all these classes are in the same default package of the src folder.

6 Comments

I fixed this issue, but I do not believe that is the reason I am getting the error.
I can't comment on the other answer as I don't have enough reputation. But related to removing the abstract, it would look something like this public class Martian extends Alien { public Martian(String p, String v, String n, String s, String a) { super(p,v,n,s,a); } @Override void invade() { // invade implementation since it's not implemented in the Alien base class it must be implemented in any class that extends it } }
I did this but I'm still getting the same error... I removed the word abstract from everything except for the Superclass but I am still not able to add the subclass to the ArrayList within the driver.
@KelOkekpe I've added example code showing each different class and the output Let me know if you have any questions about it.
I used the code you posted, now the error I'm getting is telling me that "class Martian is public and should be declared in a file named Martian.java" it says the same for the Saturner class... I saved a Martian.java and a Saturner.java version of this code and tried to run it under AlienDriver.java to no avail.
|

Your Answer

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