-1

I am completing an assignment and I was provided an interface called Place. It has several defined methods and no variables. I created a class that implements Place and this contains a string variable name and all the methods from the Place interface.

public class PlaceImpl implements Place
{
    public String name;
    ...
}

I then create an object

Place p = new PlaceImpl(placeName);

However, when I try to access p.name I get the error "cannot find symbol". Can I access instance variables in this way or do I just need to find somewhere else to store my variables? Thanks

Update: I cannot change the Place interface and I have to use a variable of type Place to integrate with the rest of the code I was provided.

  • 3
    if you need access to `name` then you cannot use a variable of type `Place` because a `Place` has no `name`. – luk2302 Sep 24 '18 at 08:09
  • Unfortunately I have to use a variable of type Place to integrate with the rest of the code I was provided. Looks like I will have to find another way. Thanks – tomhumphrey Sep 24 '18 at 08:13
  • Can you change the `Place` interface? – daniu Sep 24 '18 at 08:15
  • Unfortunately not – tomhumphrey Sep 24 '18 at 08:17
  • 2
    Add a `getName()` method to your interface, implement it in your implementation, and use that. If you need to directly access variables in your object, you can't access it via an interface. – khelwood Sep 24 '18 at 08:17
  • 4
    and please stop coming up with new restrictions and constraints on the fly. That makes answering pretty much impossible. – luk2302 Sep 24 '18 at 08:18
  • Close related to https://stackoverflow.com/questions/10068263/why-wont-my-interface-typed-objects-preform-methods-that-are-not-declared-in-th – Raedwald Sep 24 '18 at 08:20
  • 1
    So what do you need the name for exactly? – daniu Sep 24 '18 at 08:21

3 Answers3

0

Since you can't change the Place interface to add a getName() method, maintain a Map<Place,Name>.

enum PlaceNameMap implements Function<Place,Name>, Supplier<Place> {
    INSTANCE;

    private Map<Place,String> map = new HashMap<>();

    public Place createPlace(String name) {
        PlaceImpl result = new PlaceImpl(name);
        add(result, name);
        return result;
    }
    public void add(Place p, String name) {
        map.put(p, name);
    }
    public String apply(Place p) {
        return map.get(p);
    }
}
daniu
  • 14,137
  • 4
  • 32
  • 53
0

You can access name by first casting type PlaceImpl to p. For example, assigning name to String variable myName:

String myName = ((PlaceImpl) p).name;

Sorry for the really late reply but I just came across the same problem and figured it out.

AmeerAK
  • 1
  • 1
-1

You are on the right track - if you change your code to:

PlaceImp p = new PlaceImp(placeName);

you will find that your code will work because your static type is now PlaceImp and PlaceImp contains the instance variable you are looking for as a public element.

On the other hand, if your static type is Place (as in your example code), it does not contain an element called 'name' and thus you cannot access it.

Ralfino
  • 39
  • 5