This question already has answers here:
Determine if a Class implements a interface in Java
(5 answers)
Closed 3 years ago.
How to check if some class implements interface?
When having:
Character.Gorgon gor = new Character.Gorgon();
how to check if gor implements Monster interface?
public interface Monster {
public int getLevel();
public int level = 1;
}
public class Character {
public static class Gorgon extends Character implements Monster {
public int level;
#Override
public int getLevel() { return level; }
public Gorgon() {
type = "Gorgon";
}
}
}
Is the method getLevel() overridden in Gorgon correctly, so it can return level of new gor created?
For an instance
Character.Gorgon gor = new Character.Gorgon();
Then do
gor instanceof Monster
For a Class instance do
Class<?> clazz = Character.Gorgon.class;
Monster.class.isAssignableFrom(clazz);
Use
if (gor instanceof Monster) {
//...
}
In general for AnInterface and anInstance of any class:
AnInterface.class.isAssignableFrom(anInstance.getClass());
If you want a method like public void doSomething([Object implements Serializable]) you can just type it like this public void doSomething(Serializable serializableObject). You can now pass it any object that implements Serializable but using the serializableObject you only have access to the methods implemented in the object from the Serializable interface.
Related
This question already has answers here:
Call a method of subclass in Java
(7 answers)
Closed 2 years ago.
Have a below code snippet.
public class Character {
private static String type;
public void doMainSomething() {
System.out.println("Doing Main Something");
}
public static class Gorgon extends Character implements Monster {
public int level;
#Override
public int getLevel() { return level; }
public Gorgon() {
Character.type = "Gorgon";
}
public void doSomething() {
System.out.println("Doing Something");
}
}
public static void main(String[] args) {
Character.Gorgon gor = new Character.Gorgon();
Monster mon = new Character.Gorgon();
mon.doSomething(); -> Error
}
}
How can I access inner class's Gorgon method doSomething using mon ? Is there any specific way, so that we could access class's method using Interface's ref type ?
Proper way is to declare the doSomething() method on Monster interface. If a method needs to be called on interface type, then that method needs to be on the interface or it's parent.
If that is not doable, then you can safely cast the mon to Character.Gorgon
if (mon instanceof Character.Gorgon) {
((Character.Gorgon) mon).doSomething();
}
This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 4 years ago.
I have this code:
public interface Interface1{
void interfaceMethod1();
}
public class Class1<T extends Class0&Interface1>{
private T field;
public T getField(){
return field;
}
}
When I invoke class1.getField().interfaceMethod1(), where class1 is Class1 instance, I see error "Cannot resolve method".
I want to define class with generic field which will include methods from Class0 and Interface1.
Assume we have next definitions:
public interface Interface1 {
void interfaceMethod1();
}
public class Class0 {
}
public class Class2 extends Class0 implements Interface1 {
#Override
public void interfaceMethod1() {
}
}
public class Class1<T extends Class0 & Interface1> {
private T field;
public T getField() {
return field;
}
}
When you have
Class1 class1 = new Class1();
class1.getField().interfaceMethod1();
it is called type erasure. It means all generic arguments assumed to be Object, so class1.getField() return Object which lack of interfaceMethod1 method.
To fix that you should do this:
Class1<Class2> class1 = new Class1<>();
class1.getField().interfaceMethod1();
Now everything compiles fine.
I have just been creating a Parcelable and I had to create a CREATOR field inside the Parcelable and implement 2 methods. The problem is that CREATOR actually implements an interface of Parcelable.Creator. What I don't understand is how it is possible to create an instance of a interface.
Normally I thought it we would use the word "implements" on my custom class.
I am coming from .NET so that's probably why it seems a little strange. Here is an example.:
public static final Parcelable.Creator<Account> CREATOR = new Parcelable.Creator<Account>() {
public Account createFromParcel(Parcel in) {
return new Account(in);
}
public Account[] newArray(int size) {
return new Account[size];
}
};
Creating an instance of an interface like this is called declaring an anonymous class.
public static final Parcelable.Creator<Account> CREATOR = new Parcelable.Creator<Account>() {
public Account createFromParcel(Parcel in) {
return new Account(in);
}
public Account[] newArray(int size) {
return new Account[size];
}
};
It is equivalent to implementing the interface normally like this:
private static class ExampleCreator implements Parcelable.Creator<Account> {
public Account createFromParcel(Parcel in) {
return new Account(in);
}
public Account[] newArray(int size) {
return new Account[size];
}
}
And then creating a public static field like this:
public static final Parcelable.Creator<Account> CREATOR = new ExampleCreator();
But instead of explicitly implementing the interface in a named class you are implementing the interface implicitly without giving the resulting class a name which is why it's called an anonymous class!
Doing something like this is pretty common in Java and it happens all the time wether people know it or not. For example you can do the same thing with an abstract class:
public static abstract class SomeAbstractClass {
public abstract void fooTheBar();
public void doSomething() {
System.out.println("Something happened!");
}
}
public SomeAbstractClass someInstance = new SomeAbstractClass() {
#Override
public void fooTheBar() {
System.out.println("The bar is fooed!");
}
};
Here the type of the field someInstance is not actually SomeAbstractClass but another anonymous class which extends SomeAbstractClass but additionally implements fooTheBar().
The reason why you have to specifically create this anonymous class of Parcelable.Creator as a public static field is simply convention. Every Parcelable has to define a constant field which implements Parcelable.Creator. The framework looks for this field at runtime and uses it to create instances of that particular Parcelable.
This question already has answers here:
Instantiating a generic class in Java [duplicate]
(10 answers)
Closed 8 years ago.
Here's the code:
package Fabrika.Trake;
import Vozila.*;
public class Traka<T extends Vozilo> extends Thread
{
protected int vrijemeRada;
...
#Override
public void run()
{
while(true)
{
//I want to create the object here
}
}
}
Now, let's say I have a class called Auto which extends Vozilo.
I'd like to know if there is a way of constructing an object of type T without using reflection. Something like:
T object = new T();
This, of course, is completely incorrect, but it just serves as an illustration of what I'd like to do. :)
Use a type token.
public class Traka<T extends Vozilo> extends Thread
{
private Class<T> type;
protected int vrijemeRada;
...
public Traka(Class<T> type) {
this.type = type;
}
#Override
public void run()
{
while(true)
{
//I want to create the object here
T object = type.newInstance();
}
}
}
This works - of course - only with constructors that have a known number of arguments. My example uses a no-arg constructor.
And: Do not extend Thread! Implement Runnable instead. And then uses those runnables, wherever you need them.
You can't do this with just the generic type T.
To create an instance you need a Class instance.
E.g.:
public class Traka<T extends Vozilo> extends Thread
{
protected int vrijemeRada;
...
private Class<T> type;
public Traka(Class<T> type) {
this.type = type;
}
#Override
public void run()
{
while(true)
{
T newInstance = type.newInstace(); // this uses the default constructor
}
}
}
Usage:
Traka<Auto> autoTraka = new Traka(Auto.class);
The problem is that the generic type information T is erased at runtime. Therefore you need a Class instance to represent the type at runtime.
There is a special pattern for this: pass Class instance to the constructor of Traka, store it in a private variable, and the use that class to instantiate new objects, like this:
public class Traka<T extends Vozilo> extends Thread {
private Class<T> theClass;
public Traka(Class<T> theClass) {
this.theClass = theClass;
}
...
#Override
public void run() {
while(true) {
T inst = theClass.newInstance();
}
}
}
Note: In case you were wondering why Class became parameterized in Java 5, the pattern above is the answer.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Create instance of generic type in Java?
I got these classes
public abstract class Base {
public String Id = "originalId";
}
public class InstanceOfBase extends Base {
public void setString(String test) {
this.Id = test;
}
}
public class UseIt {
public Test<InstanceOfBase> test = new Test<InstanceOfBase>();
public void run() {
InstanceOfBase instanceOfBase = test.createMe();
System.out.println(instanceOfBase.Id);
}
}
public abstract class Test<E extends Base> {
public E createMe() {
// How do I do this?
return new E();
}
}
The code above does not compile because it does not know how to create E. how can I achieve this?
When I invoke the run method, I expect it should print "originalId".
Unfortunately you cannot create classes from generic types in java. But you can do as Justin Rudd suggested in this thread and write:
public E createMe(Class<E> clazz)
{
return clazz.newInstance();
}
So you use the class archetype to create a new instance. Invoking it could be:
InstanceOfBase instanceOfBase = test.createMe(test.getClass)