Using getClass inside super class [duplicate] - java

This question already has answers here:
Return Type incompatible with Object.getClass()
(4 answers)
Closed 1 year ago.
I want to return a string value from public String getClass() but I'm getting an error and telling me to change it to
public Class<?> getClass()
This is the UML
getClass(): String // return “Animal” as class name
I put it as
public String getClass() {
return "Animal";
}

Pick a different name for the method.
If you use getClass() as the method signature, you are overriding Object::getClass ... which returns a Class object.
You don't want to override that method.
Even if you wanted to, you can't override it with an incompatible return type.
You have to ignore / moderate what your UML says about the method name here. (Or modify the UML, if that is an option.) That UML cannot be implemented literally in Java. It is just not possible.
In fact, the location of this declaration in your superclass is not significant. You'd get the same problem no matter where you attempted this in your inheritance hierarchy. (With an instance method.)

Related

Two objects with same state not retrieving the same value from HashMap [duplicate]

This question already has answers here:
java why should equals method input parameter be Object
(6 answers)
Overloading in Java and multiple dispatch
(6 answers)
Closed 4 years ago.
I was making some tests with the java equals method and I figured out that if my parameter is not of the generic type Object the test won’t pass, even if the two objects I create are of that type. Let’s say I want to check if an object is an animal, what I tried to do is writing down:
public boolean equals(Animal other) {
*some code*
}
And then I create a test for that method to compare the animals. But if I do that the test will fail, on the other side, if I write down:
public boolean equals(Object other) {
*some code*
}
and then test it, the test will pass. I understand that’s useless declaring the object of the desired type and try to test it but I don’t get why it doesn’t work in a good weather test case.
It is simple, Object class equals method signature is this
public boolean equals(Object obj)
But if you write equals method with Animal parameter then it will not be the Overridden equals method from object class. and when you try to compare objects by using .equals() Object class equals will be invoked
For this reason and to make it clear it is always recommended to use #Override annotation
The equals method is part of the base Object class in Java and the only way to make benefit of it is to override it. To override it you need to stick to the same signature which will tell any libraries using equals to invoke your method instead of the base one.
Your above code is doing an overloading which is a totally different method to the Java compiler.

NullPointerException when calling a method reference to an arbitrary object with null argument [duplicate]

This question already has answers here:
Java 8 method reference to class instance method NPE [duplicate]
(2 answers)
Closed 3 years ago.
I was trying to create a method reference to an arbitrary object, so I defined the following types:
interface I {
boolean get(Impl impl);
}
static class Impl {
public boolean get() {
return true;
}
}
Then I declared the method reference, like below:
I i = Impl::get;
When I call:
i.get(null);
I get a NullPointerException:
Exception in thread "main" java.lang.NullPointerException
Can someone explain why this happens even though the Impl reference is not used anywhere?
I think you misunderstood the meaning of this line:
I i = Impl::get;
I is a functional interface that represents a method that takes an Impl and returns a boolean, whereas get is a method that takes no parameters and returns a boolean. How does this conversion work? Well, the compiler realises that get is an instance method, and to call it you must need a Impl object. Isn't that just like a function having a parameter before it is called?
So the compiler can happily infer that you meant:
I i = impl -> impl.get();
Now the cause of the NPE should be clear.
In general, all instance methods can be thought of as static methods that take one extra parameter, of type T where T is the declaring type of that instance method.

Why do we need an Object parameter for equals method? [duplicate]

This question already has answers here:
java why should equals method input parameter be Object
(6 answers)
Overloading in Java and multiple dispatch
(6 answers)
Closed 4 years ago.
I was making some tests with the java equals method and I figured out that if my parameter is not of the generic type Object the test won’t pass, even if the two objects I create are of that type. Let’s say I want to check if an object is an animal, what I tried to do is writing down:
public boolean equals(Animal other) {
*some code*
}
And then I create a test for that method to compare the animals. But if I do that the test will fail, on the other side, if I write down:
public boolean equals(Object other) {
*some code*
}
and then test it, the test will pass. I understand that’s useless declaring the object of the desired type and try to test it but I don’t get why it doesn’t work in a good weather test case.
It is simple, Object class equals method signature is this
public boolean equals(Object obj)
But if you write equals method with Animal parameter then it will not be the Overridden equals method from object class. and when you try to compare objects by using .equals() Object class equals will be invoked
For this reason and to make it clear it is always recommended to use #Override annotation
The equals method is part of the base Object class in Java and the only way to make benefit of it is to override it. To override it you need to stick to the same signature which will tell any libraries using equals to invoke your method instead of the base one.
Your above code is doing an overloading which is a totally different method to the Java compiler.

Checking for valid overloading [duplicate]

This question already has answers here:
Which overload will get selected for null in Java?
(3 answers)
Closed 8 years ago.
I want to know whether this is a valid overloading :
public class OverLoadingTest{
private void callFunction(Object object){
System.out.println("Printing Object");
}
private void callFunction(String string){
System.out.println("Printing String");
}
}
Further more, since someone asked me this question.
If I do like this,
OverLoadingTest test = new OverLoadingTest();
test.callFunction(null);
what will be printed ?
Of course my opinion is that it isn't valid overloading at all.
So no question of the second part.
Please tell me about this with some explanation.
The method with the least generic argument is called. So, in your case, it will be method accepting String
Note : If 2 classes are at the same level, then you will get an ambiguous call exception. For example if one method took String and another took Exception.
If more than one member method is both accessible and applicable to a method
invocation, it is necessary to choose one to provide the descriptor for
the run-time method dispatch.
The Java programming language uses the rule that the most specific method is chosen.
See more details in JSL 15.12.2.5
In your case, String method will be invoked, if argument is String or null and for other argument's types Object method will be invoked.
In your example, if you define one more method with argument type that is not String (e.g Integer), can't compile the source as it is ambiguous to be invoked between the methods with String and Integer as they are same level.

Creating an object using a string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use string in place of variable name
Is there any way in Java that allows an object to be created using a string?
Let's say, i have a class called "Toyata", is there anyway i can create an object of Toyata
using the string variable s in class Car?
public class Car{
String s = "Toyota";
}
public class Toyota{
int speed;
public Toyota(int speed){
this.speed=speed;
}
}
You can use reflection, but you need a fully qualified classname:
https://stackoverflow.com/a/4030634/1217408
EDIT:
Hovercraft Full Of Eels's comment about using a map lookup is probably far more appropriate in this situation.
You need to query for class (assuming it's in the default package this should work) via the value of s. Then you have to lookup the proper constructor via your class' getConstructor() method. Since Toyota does not contain a default constructor, you need to query for one that matches your expected parameters. In this case, it's an int. int is represented in Class form by Integer.TYPE. Finally after that is all said and done, you can call newInstance() on the constructor with the desired speed passed in and you should have a new instance ready to use. Of course there will be a few checked exceptions to handle.
Class<?> clazz = Class.forName(s);
Constructor constructor = clazz.getConstructor(new Class[] {Integer.TYPE});
Toyota toyota = (Toyota) constructor.newInstance(new Object[] {90});

Categories