0

I am having some issues understand something about abstract classes. I have an abstract class created with a method for setting/getting a name:

public abstract class Person {

    private String firstName;

    public void setFirstname(String name)
    {
        this.firstName = name;
    }
    public String getFirstName()
    {
         return firstname;
    }

I have 2 other classes that extend this class and they are Worker and Employer. I have the different workers and employers being created and stored into arraylists in a forth class called WorkPlace. My question is if I wanted to get every first name of every Person is there a way to do it without going through each arraylist and doing something like workerList.get.getFirstName?

7
  • if you store Personinstances in ArrayList, no. Commented Mar 17, 2013 at 20:53
  • Your class isn't really abstract so far, it doesn't contain abstract methods. Commented Mar 17, 2013 at 20:54
  • 2
    The headline differs fram what your question is about. Your problem is not that abstract class method. Your problem is, that you do not know a way to loop over a collection without actually looping over the collection. ;) And in addition: in case of a collection, there is no way to loop without looping. Commented Mar 17, 2013 at 20:54
  • There are abstract methods I just didn't include them for the question and @freakazoid_em thanks for you response. Commented Mar 17, 2013 at 20:57
  • @Sentry: Of course, his class is abstract. Watch, there's written abstract in the class signature on the first line. An abstract class may work without additional functionality, but can be declared abstract to intend that it requires some extras making it meaningful. It doesn't need to have any extra methods declared abstract. Commented Mar 17, 2013 at 20:59

4 Answers 4

3

Not unless your abstract class statically keeps a record of all first names, and you provide a static method to get all of those names.

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

1 Comment

Okay, thank you I understand. I probably shouldn't record the names because they can be removed and changed throughout my program. Thank you for your answer.
1

Perhaps you could create a class like Workplace, and whenever you want a new Employer or Worker you would call something to the effect of workplaceName.newWorker(), which would return a new Worker and at the same time keep track of all Workers, so that you can later call workplaceName.getAllWorkerNames().

Comments

1

What you could do is override the toString() method in your Person class and return the first name.

When you then get out of ArrayList.toString() is a something like [firstname1, firstname2, firstname3]. You can remove the leading and trailing brackets and split the string with with ", ". This is a bit complicated and may decrease performance because of unnecessary string operations, but you would not required to loop over all elements in the collection.

Here is an example:

// in Person class do this
@Override
public String toString() {
    return this.firstName;
}


// in your WorkPlace class do this
public String[] getAllFirstNames() {
    String allNames = workerList.toString();
    allNames = allNames.substring(1, allNames.length - 2);
    return allNames.split(", ");
}

This should work and there is no loop (better: no loop you've written by your own). But as mentioned before, it may cost to much. In addition it may be not very save, in case the behavior of the collection's toString() method changes.

Comments

0

so if you have:

ArrayList<Person> list = new ArrayList<Person>();

Then:

Employer e1 = new Employer();
Worder w1 = new Worker();

list.add(e1);
list.add(w1);

Then you have to do:

list.get(0).getFirstName();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.