logo
down
shadow

Why does this program involving a generic interface method compile?


Why does this program involving a generic interface method compile?

By : Karen Ho
Date : November 22 2020, 10:31 AM
To fix the issue you can do This is a surprisingly deep question. The Java Language Specification writes:
An instance method mC declared in or inherited by class C, overrides from C another method mA declared in class A, iff all of the following are true:
code :


Share : facebook icon twitter icon
Can't compile class calling a method in an interface with a generic list argument

Can't compile class calling a method in an interface with a generic list argument


By : Akshay
Date : March 29 2020, 07:55 AM
Does that help This has got to do with the subtyping rules for parametrized types. I'll explain it in three steps:
Non-nested case
code :
_ModelDto <: BaseObject
List<_ModelDto> <: List<BaseObject>
List<_ModelDto> <: List<? extends _ModelDto> <: List<? extends BaseObject>
List<_ModelDto> <: List<? extends BaseObject>
MyAsync<List<_ModelDto>> <: MyAsync<List<? extends BaseObject>>
MyAsync<List<_ModelDto>>
  <: MyAsync<? extends List<_ModelDto>>
  <: MyAsync<? extends List<? extends BaseObject>>
public void getList(MyAsync<? extends List<? extends BaseObject>> callback);
public interface MyAsync<T> {
    T get();
    void set(T t);
}
Tricky Java Generics : generic class implementing non generic interface with generic Method

Tricky Java Generics : generic class implementing non generic interface with generic Method


By : Galal Elshery
Date : March 29 2020, 07:55 AM
I hope this helps you . I have the following code as seen in ideone.com:
code :
class MyVisitor<T> implements Visitor{
    List<T> list = new  ArrayList<T>();

    public <T> void visit(T value){
       list.add(value);
    }
}
class MyVisitor<T> implements Visitor{
    List<T> list = new  ArrayList<T>();

    public <V> void visit(V value){
       list.add(value);
    }
}
interface Visitor<V>{
    public void visit(V Value);
}
interface Visitor{
    public void visit(Object Value);
}
using interface in another generic interface with constraints won't compile

using interface in another generic interface with constraints won't compile


By : Rodolfo Hill
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Sorry, my stupid mistake. I don't know what I was smoking yesterday but after a good nights sleep I am clearer now.
@rhughes pointed out (see note below) it was the new() constraint that was stopping the Generic definition from taking interfaces, which was a good suggestion. I did try it but Entity Framework (EF) won't work with interfaces (known design point) so I stayed with real classes in my Generic definition, i.e.
code :
private IMyService<MyData, MyDto>
public class MyService<TData, TDto> : IMyService<TData, TDto>
    where TData : class, new()
    where TDto : GenericDto<TData, TDto>, new()
internal virtual IQueryable<TDto> BuildListQueryUntracked(TemplateWebAppDb context)
{
    Mapper.CreateMap<TData, TDto>();
    return context.Set<TData>().AsNoTracking().Project().To<TDto>();
}
Create an interface with a method that's 'generic', but it's generic type is the implementer of the interface

Create an interface with a method that's 'generic', but it's generic type is the implementer of the interface


By : Jur
Date : March 29 2020, 07:55 AM
this one helps.
Is there any way in C# to have a generic type that is always the implementing type in an interface?
code :
interface IFoo<T>
{
    T GetOtherThis();
}

public class NotAString : Foo<string>
{
    string GetOtherThis() { ... }
}
public class Good : IFoo<Good> { ... }

public class Evil : IFoo<Good> { /* Mwahahahaha */ }
interface IFoo<T>
{
    T GetOtherThis();
}

public class WellBehaved : IFoo<WellBehaved>
{
    WellBehaved GetOtherThis() { ... }
}

public class BadlyBehaved : WellBehaved
{
    // Ha! Now x.GetOtherThis().GetType() != x.GetType()
}
C# call generic interface method from non generic parent interface without dynamic

C# call generic interface method from non generic parent interface without dynamic


By : bhaskar arora
Date : March 29 2020, 07:55 AM
like below fixes the issue You expect or want type safety, but think about it this way:
In order to be able to call Do, Get needs to return a type which defines that method. IFoo does not have it, but IBar does. Get however returns an IFoo object which is not guaranteed to be a IBar. Even if the implementation of Get would make sure that only a IBar is returned, there is no way the type system would know this without actually returning that type. Assuming you could return a type that allowed you to call the Do method, the type would be unclear: You need to pass it an object of type T. But the returned IFoo or IBar does not necessarily use the same type T that you wanted to pass to Do. Even if the implementation of Get would provide this (like “Get me an IBar that accepts the type T”) and the type system would have a way to reflect this, then this still wouldn’t say anything about K. For a known T, it could still be an IBar, or an IBar. And there is actually no way to know that without actually having the concrete type. And still, assuming that this would work with the type system: What purpose would this actually serve? You could call the method of your generic type with your correct typed argument: But the return value still has no concrete type. You couldn’t say anything about the returned type from Do.
code :
interface IBar
{
    object Do(object t);
}
interface IBar<T, K> : IBar
{
    K Do(T t);
}

public class BarImpl : IBar<Type, AnotherType>
{
    public AnotherType Do(Type type)
    {
        return new AnotherType(type);
    }

    public object Do(object t)
    {
        return Do((Type) t);
    }
}
Related Posts Related Posts :
  • Repeating for loop in menu
  • Can Java garbage collector randomly delete objects in the On-Heap tier?
  • Apache CXF Exception: SSL connection unexpectedly closed
  • Add properties to ArrayList<Object> in Java
  • Multiple Adapters or One Adapter for different lists and objects - Code Performance
  • Is it good practice in Java for a class's method to redundantly return a modified global field of the class?
  • How to change the color of the background in libgdx labels?
  • direct file path not working
  • ClassFileTransformer + Javassist: no such field
  • How do I change and instance variable for an object which the user selects from a combobox?
  • reading and updating a large xml file in java
  • ActionListener and Thread.sleep
  • What is the maximum of number of arguments for varargs in java?
  • When I compile I get an error as "ask" cannot be resolved or is not a field"
  • What is the best way to check if 4 integer variables are equal to 0?
  • Storing constant webdriver elements in an enum
  • Java: Why main class does not extend Thread class
  • Java - how to find out if a directory is being used by another process?
  • How can I get a method to print?
  • Java unknown exceptions
  • BufferedReader,StreamWriter crash
  • found raw type: JComboBox
  • Java Generics -> Function return type
  • how to refresh contents of jtable on event of action listener?
  • Why does List interface extend Collection interface?
  • Pros and Cons of Clojure http client libraries
  • use final inside a for each loop
  • Convert java data object to service object
  • Unsupported major.minor version 51.0 while executing JSP
  • How do I display a word diagonally in Java?
  • making sure one task completes before another starts in java
  • My program gives an index out of bounds error
  • Removing duplicate characters in a String (user inputted keyword)
  • Jersey 2.0 Content-Length not set
  • AWS.SimpleQueueService.NonExistentQueue Exception thrown when Accessing Existing SQS queue
  • Where can I find the source code for the com.sun.jdi package?
  • How can I get path of resource under WEB-INF/class folder (java ee web dynamic project)
  • convert a hexadecimal into a decimal via string convert in java
  • error populating a table using jstl
  • If Singletons are bad then how do you store global state for a framework
  • input system where the user inputs the array position of the object followed by a # to indiacte quantity but it gives me
  • Java: when would I ever want to use static methods when I can use a singleton instead?
  • Extracting RSA public key modulus and exponent stored in unsigned char arrays
  • What does "cannot be resolved to a type" mean, and how can I fix it? Java Android 4.0
  • How can I have a callback on stdout/stderr readiness instead of busy polling?
  • How to report AWT/Swing event queue length programmatically?
  • What is the difference between different for loops in Java?
  • Java arrays effective use/alternative
  • EasyMock chained method call producing null pointer exception
  • Installing Java on Windows 8
  • Java: Is it bad practice to write methods that start with "get" when they aren't getters?
  • Can Iterable.filter() skip processing with "constant" (including short circuited) Predicates?
  • How to save edited JTable data to database?
  • Java Packet Sniffer
  • I have a getText error
  • Reading bytestream and writing to file
  • dat file is not get created by buffered writer
  • Converting a pdf to word document using java
  • Measuring memory use of a piece of code at runtime in Java as an on-going functionality of a service
  • What is Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); in OpenGL ES 20?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org