Advanced Java Generics question: why do we need to specify redundant information
I've got some generic class for my JPA model POJO that goes like this:
public interface Identifiable<PK extends Serializable> {
PK getUniqueId();
}
public interface GenericDao<T extends Identifiable<PK>> {
public T findById(PK id);
}
This code won't compile. For this to work, I need to specify
public interface GenericDao<T extends Identifiable<PK>, PK extends Serializable>
But that's redundant information !! The fact that T extends Identifiable imply that a PK type will be specified for the Identifiable instance and that this is the type to use for the DAO's PK.
How can I make this work without redundant information ?
Thanks, Fred
Edit: Simplified example
Asked by: Abigail168 | Posted: 28-01-2022
Answer 1
Have you tried:
public interface WatchableDao<T extends Watchable<?>>
(i.e. it's a Watchable<Something>
but I don't care what Something
is)
I haven't tried it, but it's worth a go.
EDIT: Post question edit, it seems that you really do need PK as a type parameter to the interface. In that case, I believe you have to effectively repeat the constraint as you are doing. Yes, it's redundant, but I think it's simpler than the language having to specify what effective constraints would apply to PK based on its use as a type argument elsewhere. If it's any consolation, the same is true in C#.
It also makes the constraints on PK clear from just the interface itself, rather than having to look at another interface to see what's feasible.
Answered by: First Name803 | Posted: 01-03-2022Similar questions
jvm - General Question: Java has the heap and local stack. Can you access any object from the heap?
I was really looking at the differences between pass by value and how Java allocates objects and what java does to put objects on the stack.
Is there anyway to access objects allocated on the heap? What mechanisms does java enforce to guarantee that the right method can access the right data off the heap?
It seems like if you were crafty and maybe even manipulate the java bytecode during runtime, that you ...
java - Quick Swing question: Need to shut down my database on close
I've been building a test application that works with a database that up until recently has been without a UI. I'm adding one now. Problem is, the JFrame is launched in another thread and I need my database connection to close when that thread closes (when the UI closes, I should say). How do I do this?
Also, what happens to the application's database connection (in this case an embedded database) if the applica...
Java Generic Question: Any better way?
[Update]: my initial example doesn't reflect my problem. Updated the sample, hopfully it is better now.
Java Generic and overloading don't play well together.
public interface Request<E> {
E getValue();
}
I have a generic parameterized Request interface as above, and I would like write a DataStore class to save the payload request carries. The save logic is different for di...
java - gc question: array of 10 ints a single object or 10 objects?
If i have an array say -
int[] a = new int[10];
does the Java GC when doing its collection see that as 10 objects or a single object?
Update:
so according to the given answers, looking at it from a GC perspective isnt it more efficient that instead of
List l;
for(i =0;i<1000;i++)
l.add(new MyObj(343,"asdfasfd"));
we should ...
Java Question: Is it possible to have a switch statement within another one?
I have a yes or no question & answer. I would like to ask another yes or no question if yes was the answer. My instructor wants us to use charAt(0) as input for the answer.
Is it possible to have a switch statement within another one (like a nested if statement)?
EDIT: Here is a sample of my pseudo code =
display "Would you like to add a link (y = yes or n = no)? "
input addLink
switc...
Basic Java question: String equality
public class A {
static String s1 = "I am A";
public static void main(String[] args) {
String s2 = "I am A";
System.out.println(s1 == s2);
}
}
Above program outputs "true". Both are two different identifiers/objects how the output is "true" ?
My understanding is that the JVM will create different reference for each object, if so how the output is true?
java - SCJP question: Method ambiguous
Take a look at this code:
public class Test {
public static void main(String... args) {
flipFlop("hello", new Integer(4), 2004);
// flipFlop("hello", 10, 2004); // this works!
}
private static void flipFlop(String str, int i, Integer iRef) {
System.out.println(str + " (String, int, Integer)");
}
private static void flipFlop(String str, int i, int j) {
System.out.println(str + " (String, in...
Newb Question: passing objects in java?
I am new at java. I am doing the following:
Read from file, then put data into a variable.
I have declared the checkToken and lineToken as public strings under the class.
public static void readFile(String fromFile) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(fromFile));
String line = null;
while ((line=reader.readLine()) != nu...
java - RMI question: what happens to an object reference after server goes down?
Could someone help on this , please?
Q: An application server registers an object in RMI Registry by calling Naming.rebind(). After a while, the server app goes down. Explain what will happen to the object reference registered in the Registry.
A: I think the reference is kept in the Registry for a while, but after that period ("lease period" ?) the local garbage collector can remove the reference.
I...
basic Java question: throwing an exception to a later catch clause?
If you have:
catch (FooException ex) {
throw new BarException (ex);
}
catch (BarException ex) {
System.out.println("hi");
}
...and the first catch clause is triggered (i.e. FooExcepetion has occurred),
does the new BarException get immediately caught by the subsequent "catch" clause?
Or is the new BarException thrown one level up the continuation stack?
I realize th...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)