Can interfaces have constructors




















And even if you could, they would be erased anyway due to type erasure. Passing this to the binding library would be like saying " a list of maybe nullable java. Object please " but worse.

The error message could be clearer to say " only classes without generic parameters are allowed on the left side of :: ". And your use of javaClass can only be used on an instance, so if you happen to have had a list handy By creating an object with a super type ParameterizedTypeReference the code works around this problem because any class, even if type erased, can see the generics in its superclass and interfaces via the lense of Type.

For example:. This instance of an anonymous class has the superclass MySuperClass with generic parameters of SomeGenerics. So if MySuperClass contained this code:. You could then add. And now you would have some implementation of Type describing our SomeGenerics class. And by understanding and working with these, a library that does data binding knows enough to do its job. In Kotlin, it is easy to write extension functions so that you never have to do this type of code more than once.

Just create a helper inline function that uses reified generics to pass through the type and create the ParameterizedTypeReference :. If any of these functions is explicitly defined in the class body or inherited from the base types, it will not be generated. Collectives on Stack Overflow. Learn more. Constructor in an Interface? Ask Question. Asked 11 years, 6 months ago. Active 3 years, 9 months ago. Viewed k times. Improve this question. You say "In the constructor I could ensure [every implementation of this class really has an receiver set].

Provided it was possible to define such a constructor, the parameter would only be a strong hint to your implementors — but they could chose to simply ignore it if they wanted to.

Add a comment. Active Oldest Votes. Taking some of the things you have described: "So you could be sure that some fields in a class are defined for every implementation of this interface. Improve this answer. Matt, that's clearly true, but abstract classes suffer from the single-inheritance limitation, which leads people to look at other ways of specifying hierarchies.

This is true and may solve Sebi's immediate problem. But one reason for using interfaces in Java is because you cannot have multiple inheritance. In a case where I cannot make my "thing" an abstract class because I need to inherit from something else, the problem remains.

Not that I claim to have a solution. CPerkins while this is true, I am not suggesting that simply using an abstract class will solve Sebi's use case. If anything, it's best to declare a Message interface which defines the send method, and if Sebi wishes to provide a "base" class for implementations of the Message interface, then provide an AbstractMessage as well.

Abstract classes shouldn't take the place of interfaces, was never attempting to suggest so. Understood, Matt.

I wasn't arguing with you, more pointing out that it's not a complete replacement for what the op wants. Show 1 more comment. For a class to implement multiple interfaces would not pose any problem, since new InterfaceName would simply construct a class designated by the interface. Counter-argument: your A String,List constructor could be the designated constructor, and A String and A List could be secondary ones that call it. Your code isn't a counter-example, just a poor one.

Why would you call all the constructors in an implementation?! Yes, if it implements more Interfaces with ctor, one with a String and one with an int, it needs that two ctors - but either or can be used. If thats not applicable, the class simply doesn't implement both Interface. So what!?

In other words: In my example, the instance has both a name and a list, so each instance needs to instantiate both the name and the list. To summarize: is it asking for trouble when you call overloaded methods from parent constructors, to quote mindprod : In general you must avoid calling any non-final methods in a constructor.

Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. Why interfaces can not have the constructor?

An Interface is a complete abstraction of class. All data members present in the interface are by default public, static, and final. The methods inside the interface are by default public abstract which means the method implementation cannot be provided by the interface itself, it has to be provided by the implementing class. Therefore, no need of having a constructor inside the interface. A constructor is used to initializing non-static data members and as there are no non-static data members in the interface, there is no need of constructor Methods present in the interface are only declared not defined, As there is no implementation of methods, there is no need of making objects for calling methods in the interface and thus no point of having constructor in it.

If we try to create a constructor inside the interface, the compiler will give a compile-time error. In the above program, we have created an interface Subtraction which defines a method subtract , whose implementation is provided by the class GFG. Therefore, there is no need for calling the method in the interface.

Since we cannot call the methods in the interface, there is no need for creating the object for interface and there is no need of having a constructor in it. Why abstract classes have a constructor?



0コメント

  • 1000 / 1000