interface

Create generic Interface restricted to own class

回眸只為那壹抹淺笑 提交于 2019-12-23 08:58:17
问题 I would like to create a generic interface for those two classes but I'm not sure how to specify the generics the right way. public class ThingA implements Thing { public ThingA createCopy(ThingA original); } public class ThingB implements Thing { public ThingB createCopy(ThingB original); } I tried it this. public interface Thing<V extends Thing<V>> { public V createCopy(V original); } But I'm still able to do things like this, which shouldn't be allowed. public class ThingB implements Thing

Using a Func<> over an interface?

非 Y 不嫁゛ 提交于 2019-12-23 08:53:09
问题 I have an already existing generic class public class Foo<T> { private T _item; public Foo(T item){ _item = item;} } I have to create a method which will return a certain property of T. I see two solutions here. Creating an interface : public const string TestVar = "bar"; public interface INamable { string Name { get; } } public class Bar : INamable { public string Name { get { return TestVar; } } } public class Foo<T> where T : INamable { private T _item; public Foo(T item) { _item = item; }

Is Interface as labels a bad practice in java OO?

做~自己de王妃 提交于 2019-12-23 07:46:11
问题 During the parsing of certain xml files, I come across a situation that I have to use interface as labels to identify that certain tags belong to certain category, for example, I created a Tag interface to identify that these classes are used to represent xml tags, and ContainableTag to point out that certain tags can be one of the children tags of some tags. Then I stumble into this page: http://xahlee.org/java-a-day/interface.html (Please look for the " Interface as Labels " session.). It

how can I declare java interface field that implement class should refine that field

自古美人都是妖i 提交于 2019-12-23 07:41:36
问题 How can I declare java interface field that implement class should refine that field ? for example public interface IWorkflow{ public static final String EXAMPLE;// interface field public void reject(); } // and implement class public class AbstWorkflow implements IWorkflow { public static final String EXAMPLE = "ABCD"; /*MUST HAVE*/ public void reject(){} ... } Thank you. 回答1: See section 9.3 of the specification. There is no overriding of fields in interfaces - they are just hidden in some

Java: general function X->Y interface

二次信任 提交于 2019-12-23 07:23:09
问题 I need an interface like: interface Function<X,Y> { Y eval(X obj); } Is there something like this in Java already or do I need to define my own? 回答1: There is a built-in interface like this, although it's only compatible with Java 8 onwards. You can find it here. From the Javadocs: public interface Function<T,R> Represents a function that accepts one argument and produces a result. This is a functional interface whose functional method is apply(Object) . Type Parameters: T - the type of the

Why interface really needed when normal class can do the same work [duplicate]

陌路散爱 提交于 2019-12-23 07:13:08
问题 This question already has answers here : Why would I want to use Interfaces? [closed] (19 answers) What does it mean to “program to an interface”? (31 answers) Interface vs Base class (39 answers) Closed 5 years ago . I have been wondering the real use of interface. please check the below code. interface Animal { public void eat(); public void travel(); } public class MammalInt implements Animal{ public void eat(){ System.out.println("Mammal eats"); } public void travel(){ System.out.println(

What is the meaning of “this [int index]”?

落花浮王杯 提交于 2019-12-23 07:09:13
问题 In C# we have the following interface: public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable { T this [int index] { get; set; } int IndexOf (T item); void Insert (int index, T item); void RemoveAt (int index); } I don't understand the line T this [int index] { get; set; } What does it mean? 回答1: That is an indexer defined on the interface. It means you can get and set the value of list[index] for any IList<T> list and int index . Documentation: Indexers in Interfaces (C#

Constrained interface implementation

Deadly 提交于 2019-12-23 06:58:47
问题 In Haskell (and Rust, and others) I can have instances that are constrained by other instances: data Pair a b = Pair a b instance (Eq a, Eq b) => Eq (Pair a b) where Pair a b == Pair a' b' = a == a' && b == b' With Java interfaces I can't. I must require the type parameters of Pair to always implement Eq , or I can't implement Eq<Pair<A, B>> at all: interface Eq<A> { public boolean eq(A other); } class Pair<A extends Eq<A>, B extends Eq<B>> implements Eq<Pair<A, B>> { A a; B b; public boolean

Can an implemented class have methods NOT declared in its parent interface?

梦想的初衷 提交于 2019-12-23 06:08:00
问题 I tried this in Eclipse and it shows a compile error. However trying the same in the online IDE Compilr showed no errors. That's why the confusion. interface Iclass{ void print(); void hey(); } class sdlfkajl implements Iclass { public void print(){ System.out.println("Impl class"); } public void hey(){ System.out.println("Hey!"); } public void extra(){ System.out.println("Should I be here?"); } } The error shown is that this class cannot have methods not declared in the interface. Suggestion

Abstraction Vs. Interface Confusion

笑着哭i 提交于 2019-12-23 05:27:32
问题 Wait, before you start thinking, I would like to clear that I am NOT going to ask the routine differences between Interface and Abstract. I had gone through the difference between Abstract and Interface in MSDN. It is said : By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface. See this : - Can anyone prove