Downcast parameter in Child class Java [duplicate] - java

This question already has answers here:
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
Closed 5 years ago.
public class Contract extends StandardEntity {
/***/
#Composition
#OnDeleteInverse(DeletePolicy.UNLINK)
#OnDelete(DeletePolicy.CASCADE)
#OneToMany(mappedBy = "contract")
protected List<Objective> objectives;
/***/
Is it possible to downgrade argument objectives in child class (ContractWthDeadLines extends Contract) from List< Objective> to List< ObjectiveWitchDeadLine>:
public class ObjectiveWitchDeadLine extends Objective

Make your Contract class generic
public class Contract<T extends Objective> extends StandardEntity {
protected List<T> objectives;
}
and if you extend it you can change the type
public class ContractDeadline extends Contract<ObjectiveWitchDeadLine> {
}

Related

Generic variable declaration with wildChar vs without wildChar [duplicate]

This question already has answers here:
What does the question mark in Java generics' type parameter mean? [duplicate]
(6 answers)
When to use generic methods and when to use wild-card?
(9 answers)
Closed 3 months ago.
interface Shape { }
class Circle implements Shape { }
class ShapeContainer<T extends Shape> {
T sh;
public ShapeContainer(T newInstance) {
sh = newInstance;
}
...
}
class Main {
public static void main(String[] a) {
ShapeContainer<Shape> A = new ShapeContainer(new Circle());
ShapeContainer<? extends Shape> B = new ShapeContainer(new Circle());
}
}
What are the pros and cons of declaring the variable as ShapeContainer<Shape> A vs ShapeContainer<? extends Shape> B
What condition should each one be preferred?

Java/Generics: Return a list of extending a type? [duplicate]

This question already has answers here:
What is PECS (Producer Extends Consumer Super)?
(16 answers)
Closed 3 years ago.
If I have the following:
class A {}
class B extends A {}
public <T extends A> List<T> getList() {
List<T> list = new ArrayList<>();
list.add(new A());
return list;
}
But I get the error: add(T) in List cannot be applied to A
What I would like to do is return a list of Class A or any class extending Class A. Why doesn't it work here?
You can't add an instance of A to a list of type that extends A, such as List<B>, since A isn't a B (but visa-versa).

Difference between List <? extends interface> and List <interface> [duplicate]

This question already has answers here:
When do Java generics require <? extends T> instead of <T> and is there any downside of switching?
(7 answers)
Generics : List<? extends Animal> is same as List<Animal>?
(3 answers)
Closed 5 years ago.
I have this interface:
public interface IModel extends Serializable{
boolean isOnDb();
}
I have an Object that implements the interface IModel :
public class Media implements Serializable, IModel {
#Override
public boolean isOnDb() {
return isOnDb;
}
}
I want to create a List where I can put objects that implement the interface IModel. Something like this:
List<? extends IModel> list= new ArrayList<>();
Media media = new Media();
list.add(m);
the code above doesn't compile. But I have no error if I do :
List<IModel> list= new ArrayList<>();
Media media = new Media();
list.add(m);
What's the difference between List<? extends IModel> and List<IModel>?
Thank you in advance

Java class generics [duplicate]

This question already has answers here:
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
Closed 5 years ago.
What are my options, if I what the following code to work?
class Garage<X>{}
class Vehicle {}
class Car extends Vehicle {}
class Bike extends Vehicle {}
class A {
public static void main(String[] args) {
Garage<Car> car = new Garage<>();
Garage<Bike> bike = new Garage<>();
Garage<Vehicle> vehicle = new Garage<>();
//what are the options to do this?
vehicle=car;
vehicle=bike;
}
}
What I'm after is to try upcast(?) car and bike to vehicle. Is this even possible with this kind generic class type?
In order to do what you desire, you need to change
Garage<Vehicle> vehicle = new Garage<>();
to
Garage<? extends Vehicle> vehicle = new Garage<>();

Apparent type violation, but compiles [duplicate]

This question already has answers here:
String gets assigned to a List without a compilation error [duplicate]
(1 answer)
Why can this generic method with a bound return any type?
(1 answer)
Generic return type upper bound - interface vs. class - surprisingly valid code
(2 answers)
Closed 5 years ago.
Why does the below snippet compile ? OtherInterface does not extends Concrete so I would have bet a kidney that this wouldn't compile. But it does.
public class Test {
public static interface SomeInterface {}
public static interface OtherInterface{}
public static class Concrete implements SomeInterface {
public <T extends Concrete> T getConcrete() {
return null;
}
}
public static void doStuff() {
Concrete c = new Concrete();
OtherInterface iCompile = c.getConcrete();
}
}
On the other hand, the next snippet does not compile, which is what I expect.
public class Test {
public static interface SomeInterface {}
public static class UnrelatedClass{}
public static class Concrete implements SomeInterface {
public <T extends Concrete> T getConcrete() {
return null;
}
}
public static void doStuff() {
Concrete c = new Concrete();
UnrelatedClass iCompile = c.getConcrete();
}
}
The difference is here:
public static interface OtherInterface{} ...
OtherInterface iCompile = c.getConcrete();
vs.
public static class UnrelatedClass{} ...
UnrelatedClass iCompile = c.getConcrete();
Meaning: in the first case, you call the method to return an instance of some interface. Interfaces can be any class.
In the second example, you instruct that returned type is of be a specific class! A class which is known, and that does not implement that other interface!
And the error message:
reason: no unique maximal instance exists for type variable T with upper bounds UnrelatedClass,Concrete
is pretty specific here.
In other words: the compiler factors in the left hand side of that assignment - to determine the valid types. And UnrelatedClass can never be a Concrete - because the class UnrelatedClass does not extend Concrete!
Whereas something that is SomeInterface can as well implement OtherInterface.

Categories