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!