This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 1 year ago.
I am recreating basic data structures as an assignment. I have an interface called Queue and I have to implement the same data structure with the same name (both are in different packages) I tried doing something like this but it gives a warning
Queue is a raw type. References to generic type Queue<E> should be parameterized
This is my code
public class Queue implements uo.mp2021.util.collections.Queue
What's it a Queue of?
Anything?
public class Queue<E> implements uo.mp2021.util.collections.Queue<E>
Something?
public class Queue implements uo.mp2021.util.collections.Queue<Something>
The point is that the interface is generic, so you need to decide whether you're providing a generic implementation, or a specific implementation.
Related
This question already has answers here:
When do I have to use interfaces instead of abstract classes? [duplicate]
(18 answers)
When to use an interface instead of an abstract class and vice versa?
(26 answers)
Closed 4 years ago.
Apologies for the title, I'm not quite sure what it should be.
I'm new to Java and have inherited a codebase with the following pattern:
public abstract class BaseThing{...}
public class ThingA extends BaseThing{
public List<BaseThing> elements;
}
public class ThingB extends BaseThing{
public List<BaseThing> elements;
}
public class ThingC extends BaseThing{...}
public class ThingD extends BaseThing{...}
I want to write a method that works on BaseThing(s) that have an elements property e.g. ThingA and ThingB.
A Java method that accepts multiple types as a single parameter is similar to what I want to achieve, however the interface is for a method instead of a property. I don't think it's possible with properties on an interface.
I also looked at method overloading, however it doesn't appear to scale well, I'd need to add a new method every time a new ThingX is created.
What is the Java way of achieving this?
This question already has answers here:
What are functional interfaces used for in Java 8?
(11 answers)
Closed 4 years ago.
While going through Functional Interfaces, I am not able to understand how are they different from other interfaces with a single method, like Runnable.
We can use Runnable as we try to use other Functional interfaces.
Prior to Java 8, we already could create interfaces and anonymous objects for a single piece of functionality.
For example:
#FunctionalInterface
public interface ITrade {
public boolean check(Trade t);
}
How is this different from:
public interface ITrade {
public boolean check(Trade t);
}
There is no difference, the docs for FunctionalInterface state:
An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface [emphasis added]
and
However, the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.
So the annotation is only there to indicate that the developer intended the interface to be used as a functional interface.
This question already has answers here:
What is the purpose of a marker interface?
(10 answers)
Closed 7 years ago.
I have a question regarding "Interfaces" in Java and the question sounds like this:
What is the use of implementing a blank (empty) interface in my class?
In order to get a better understanding of the question, I will give you a concrete example:
If you go and see the implementation of "ArrayList" class, you will find out that it implements two interfaces (RandomAccess and Cloneable), which are actually totally empty!
Why is that happening?
What do I win by implementing a totally blank interface for my class?
In case you have any ideas, please leave a comment.
Thank you in advance.
Those interfaces are called as Marker interfaces (used to mark the class of that type) and at run time they are used to check the type.
For ex
While running the programm, internal logic may goes like
if (yourList instanceof Cloneable) {
// Hey this object is of type Clonable, please proceed
} else {
// Not that type. Reject
}
Those interfaces serve only for differing and identifying instances, consider this:
interface MyInterface1 {}
interface MyInterface2 {}
Consuming code:
if (foo is MyInterface1) ...
This question already has answers here:
How Marker Interface is handled by JVM
(7 answers)
Closed 7 years ago.
I have gone through few question in stack overflow but could not find a suitable answer. So raising it for more clarity.
I know a marker interface is an interface with no methods. When we implement a marker interface for example Serializable it declares that the class implementing it becomes eligible for serialization.
My question is how JVM understands that the objects of class implementing Serializable interface should be serialized. If i write an interface with no methods and hope that objects of class that implements it will be serialized i'll not work that way.
Is it possible for us to create a custom marker class.?
They aren't 'identified by the JVM' at all. They're identified by the Java code that is interested in them, for example ObjectOutputStream, via the instanceof operator.
This question already has answers here:
Extend a generic type in Java
(2 answers)
Closed 8 years ago.
I wanted to create a class that can extend an unknown class that is provided at runtime. I thought that I could do something like this:
public class Foo<T extends Bar> extends T {}
but that doesn't work. Is there a way to do this?
Not without some crazy runtime code generation. Java's generics are not the same sort of thing as C++'s templates: Internally, T is simply treated as an Object, and up- and down-casted as necessary for the benefit of your code. So while a C++ template is instantiated for a particular T, and can decide at compile-time whether that works, in Java the machinery underlying inheritance requires that the actual base class be decided upon in the class definition itself.