4

I'm having a major disconnect when I'm attempting to use methods of classes (of which both classes implement interfaces) that also contain generics. I have a SetInterface interface which contains the methods for my Set class. The same is true for my ProfileInterface interface and the Profile class. Within my Profile class, I have the following type-cast referring to the Set class:

private Set<ProfileInterface> followBag = new Set<ProfileInterface>();

Basically, we are learning Array Bags, Linked Bags, etc in my Data Structures class. My problem is that I wish to use methods of the Set Data Structure class within my Profile class thru Composition. The one problem I'm having is that I need to create a method in my Profile class that cross references items within a given array to see if that Profile object is "following" another Profile and, if not, recommends that object to be followed (assignment is to accomplish something similar to that of Twitter or Facebook with Data Structures). Here is the method I've created so far and the error that I can't get passed (third line from the top):

    public ProfileInterface recommend(){
    ProfileInterface recommended;
    ProfileInterface thisProfile = new Profile(); //  <--Here is the question
    for(int index = 0; index < followBag.getCurrentSize(); index++){
        ProfileInterface follows = followBag[index];
        for(int followedFollowers = 0; followedFollowers < follows.getCurrentSize(); followedFollowers++) { // <--Question here also
            //if Profile's match, do nothing
            //if Profile's do not match, set recommended == the Profile
        }
    }
    return recommended;
}

Please excuse my pseudocode that remains as I'm still working on it. But I cannot continue with this method until I fully understand what I need to do to get that third cast correct and understand if my other concern (second note) is possible.

I'm in my second class of Java and I can't seem to get passed these issues. I'm ideally hoping that a member, or multiple members, can dumb this down to a 5-year old level so I can completely grasp it. I understand that the Profile class will have an "has a" relationship (Composition) with my Set class. I also know that since I've casted my followBag in the way I did, that it will have both ProfileInterface and Set methods available to it. But I would like an explanation, with examples if possible, as to how I can properly type cast within a method to take an item of an array, or in my Set in this example, and cast that to a given object? I wish to fill this object, (thisProfile in this case), to a given index for comparison purposes. But there are two pieces I need clarification on:

A) What happens to an object that is type-casted as follows: ProfileInterface thisProfile = new Profile();

Since I'm referring to both the interface and the class that implements it? Meaning, if I have more methods within my Profile() class, which methods will this thisProfile only have access to; interface or class?

B) Should I instead simply call ProfileInterface thisProfile and then assign the reference to thisProfile to a particular index within my internal for loop?

C) My second note: I'm getting an error in IntelliJ stating that it, "cannot resolve method getCurrentSize()". I do not understand this since I created follows a line above that. I thought that ProfileInterface follows = followBag[index] would set the given Profile index of an array equal to that for use later in my code. This error stems from my confusion above. So I'm positive once I'm provided some clarity on what I'm doing wrong, I'm sure I'll fix it appropriately.

Thank you all and as always I look forward to hearing your responses!

Pwrcdr87
  • 935
  • 3
  • 16
  • 36

1 Answers1

0

A) What happens to an object that is type-casted as follows: ProfileInterface thisProfile = new Profile();

First, that's an assignment, not a type-cast.

Since I'm referring to both the interface and the class that implements it? Meaning, if I have more methods within my Profile() class, which methods will this thisProfile only have access to; interface or class?

You'll only have access to the methods in ProfileInterface. If that's a problem, I suggest you simply change ProfileInterface to Profile. The types of private variables should be no problem to refactor.

C) My second note: I'm getting an error in IntelliJ stating that it, "cannot resolve method getCurrentSize()". I do not understand this since I created follows a line above that.

It means that the followBag is a Set and does not implement a method named getCurrentSize().

I thought that ProfileInterface follows = followBag[index] would set the given Profile index of an array equal to that for use later in my code.

This is unrelated to the first part of question C. followBag is not an array. I'm not sure what you're trying to do here, but if you're a C# programmer, Java doesn't use the same syntax. [] can't be used to access members of a Collection. You have to use get() or a related method.

I'm really unsure what you're trying to say in question B. I'd get rid of the syntax errors, then ask again, something is a bit off here that I can't figure out exactly where you're going.

markspace
  • 10,621
  • 3
  • 25
  • 39
  • Thank you for your clarification. What I am attempting to accomplish is my profile has other profiles that I "follow". The meaning behind this method is I need to check each index of my array of `Profiles`, and then check that profiles array of `Profiles`. If any of those are not in my `Set` of `Profiles`, then the method returns one, or "Recommends" it. In my `for` loops, I want to look at those indexes, and I do not know the best way to assign a variable to represent those. I hope that makes further sense? Again, much appreciated for your input! – Pwrcdr87 Feb 06 '16 at 15:42