I am dealing with some past exam papers and here I am not sure of the output. I think I am not clear about extends and super.
public class Superclass{
public boolean aVariable;
public void aMethod(){
aVariable = true;
}
}
class Subclass extends Superclass {
public boolean aVariable;
public void aMethod() {
aVariable = false;
super.aMethod();
System.out.println(aVariable);
System.out.println(super.aVariable);
}
}
I think that the second output would be true since it would refer to the super class and it is an object. However, I am not sure of the first output. Would it be just a value and print false or it is also an object?
The output will be:
false
true
Because in your Subclass aVariable is false by default (so assignation aVariable = false; is needless). Read more about Primitive Data Types default values.
And in Superclass you initialize aVariable as true by invoking the superclass' method using the keyword super: super.aMethod();. Read more about Accessing Superclass Members.
Take a look on demo.
Since they're both scoped to their own class block, having them with the same name doesn't matter. As it looks like now, you set aVariable to false, the call to the super doesn't change that, except for creating another variable (new reference) with the same name and sets it to true. So the expected output would be
false
true
Output will be:
false
true
super.aMethod() will execute making aVariable = true of SuperClass
aVariable of SubClass will remain false.
I conducted scientific experiment (copy-pasted and ran) and it prints
false
true
More here:
If you overwrite a field in a subclass of a class, the subclass has two fields with the same name(and different type)?
Related
I don't think we can instantiate an Enum. So do we need to have toString(), equals() and hashCode() method for an enum? Is this statement true?
Also as a follow up question
What does "this" keyword refers to in the following enum class?
public enum DocuType {
text, print, html, pdf; //Why aren't these in caps?
public boolean isPrint() {return this == print; } //What does this refers to?
public boolean isText() {return this == text; }
public boolean isTextOrPrint() { return isText() || isPrint(); }
}
How do I call any of the boolean methods of this enum as they are not static?
Also when I call, what does the keyword "this" refers to ?
I don't think we can instantiate an Enum.
Enum is one of type as we have class type. The way we can create instance (i.e. create variable) of a class, similarly we can create variables of Enum but that's only during during its definition which eventually makes them a Constant.
So do we need to have toString(), equals() and hashCode() method for an enum?
equals() & hashcode() are used to compare two instances. In case of class instances, the instances are created dynamically. So this means we don't have knowledge about instances beforehand and hence we need to compare them to know if they are equal. However, in case of Enum we know the instances when we define the Enum. So, we know beforehand whether they are equal or not. If the enum instances mean to be equal why on the earth we need two separate equal enum instances. So, in case of enum we generally don't override these methods.
What does "this" keyword refers to in the following enum class?
&
How do I call any of the boolean methods of this enum as they are not static?
&
Also when I call, what does the keyword "this" refers to ?
this means an instance currently in access. In your example, you have DocuType instances as text, pdf, print, html. When you invoke a method on any of the instance, ex: DocuType.text.isPrint(), this keyword inside the isPrint method will point to text. So, for instance pdf, all the methods will return false except isPdf().
So do we need to have toString(), equals() and hashCode() method for an enum? Is this statement true?
You don't need any of these methods in the enum.
Also as a follow up question What does "this" keyword refers to in the following enum class?
Imagine you have this code:
DocuType myType = DocuType.print;
If you want to check if the document is a PRINT document, you can do this:
boolean isPrint = myType.isPrint();
In this case, this is the myType, which means that it's the print enum. In the end, the result is true, because print == print;
I would also suggest to read the Tutorial to Enums
It seems you lack the knowledge of the concept of an enum.
And to your last question, an enum is static, but each "type" of it is an object, meaning you can do
DocuType.html.isText(); // false
DocuType.print.isPrint(); // true
To initalize your own Enum you have to
DocuType TextDocu = DocuType.text;
And you can write them in CAPS, but you don't have to, but it's recommened.
To explain it more in detail:
enum DocuType //this is an enum and not instantiatable
text,print,html,pdf // these are Objects with the functions of the enum and are an instance
As preparation for an explanation, a quick glance back in history.
Before enums were available in Java, a possible replacement could have been constructed like that:
public class DocuType {
public final static DocuType text=new DocuType();
public final static DocuType print=new DocuType();
public final static DocuType html=new DocuType();
public final static DocuType pdf=new DocuType();
private DocuType(){}; // hide constructor
public boolean isPrint() {return this == print; }
public boolean isText() {return this == text; }
public boolean isTextOrPrint() { return isText() || isPrint(); }
}
As you can see, while you cannot create new instances of DocuType, 4 static instances of them are already prepared for you in the Class.
Javas enum work very similar to that example (less writing involved, obviously); therefore, this refers to the instance you execute the method on. Regarding toString, it's up to you; the default implementation can be good enough or not. There is no need to implement neither equals nor hashCode.
In my GameObject class I have the following method to check if the GameObject would be colliding with another object if it moved to the specified position:
public boolean collisionAt(Vector2d position, Class<? extends GameObject>... exclusions) {
if (getBounds() == null)
return false;
Rectangle newBounds = getBounds().clone();
newBounds.setPosition(position);
// Check collisions
for (GameObject object : new ArrayList<>(gameObjects)) {
if (object.getBounds() != null && newBounds.intersects(object.getBounds()) && object != this) {
boolean b = true;
for (Class<? extends GameObject> exclusion : exclusions) {
if (object.getClass().isInstance(exclusion))
b = false;
}
if (b)
return true;
}
}
return false;
}
I want to allow the program to define exclusions, for example if I don't want this method to return true if it collides with a Spell. But for some reason the Class.isInstance() line always returns false. I even tried this:
System.out.println(Spell.class.isInstance(Spell.class));
and the console outputs false! What's going on here?
The isInstance tests if the given object is an instance of the Class, not if the given Class is a subclass of the Class.
You have your invocation backwards. You need to test if the gameObject is an instance of one of the exclusion classes.
if (exclusion.isInstance(gameObject))
From official Javadocs
public boolean isInstance(Object obj)
Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.
You need to pass in the object of class rather than the class itself.
Example
SomeClass object = new SomeClass();
System.out.println(SomeClass.class.isInstance(object));
You need to pass in an instance of the class in question rather than a class literal. E.g.
Spell spell = new Spell();
System.out.println(Spell.class.isInstance(spell));
isInstance determines if the specified Object is assignment-compatible with the object represented by this Class. You're passing it a class when it expects an object.
The opposite should work:
Spell.class.isInstance(spell)
Why ReflectionChild.class.isInstance(Class.class) is not true?
As we know in reflection Class.class.isInstance(Class.class) is true. Now see on below code snip.
ReflectionChild ch = new ReflectionChild(); //Take random class
if(ch.getClass()==ReflectionChild.class){
System.out.println("ch.getClass()==ReflectionChild.class");
}
System.out.println(ReflectionChild.class.getClass());
System.out.println(Class.class);
if(ReflectionChild.class.getClass()==Class.class){
System.out.println("ReflectionChild.class.getClass()==Class.class");
//System.exit(0);
}
if(ReflectionChild.class.isInstance(Class.class)){
System.out.println("true");
//System.exit(0);
}else{
System.out.println("false");
}
The output is :-
ch.getClass()==ReflectionChild.class // 1st SYSOUT
class java.lang.Class // 2nd SYSOUT
class java.lang.Class // 3rd SYSOUT
ReflectionChild.class.getClass()==Class.class //4th SYSOUT
false // 5th SYSOUT
According to above 1st four SYSOUT's the line ReflectionChild.class.isInstance(Class.class) should be true. But for me it is false as output .
Can any one explain?
Class.class is an instance of Class, not ReflectionChild.
Therefore, isInstance() returns false.
Because a Class instance is not an instance of of type ReflectionChild.
From the javadoc
Specifically, if this Class object represents a declared class, this
method returns true if the specified Object argument is an instance of
the represented class (or of any of its subclasses); it returns false
otherwise.
The argument you've provided is of type Class which is not an instance of the type ReflectionChild represented by the Class instance returned by the class literal expression ReflectionChild.class.
From the Javadocs of Class#isInstance(Object):
Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.
So the object passed as an argument to the method must be an instance of the class represented by the object on which the method is called, not the other way around: the expression that should return true is the following:
if(Class.class.isInstance(ReflectionChild.class)){
As we know, Class#isAssignable does not consider, that a value can be auto boxed/unboxed. E.g. the lower of the four following cases return false:
// obvious
System.out.println(boolean.class.isAssignableFrom(boolean.class)); // true
System.out.println(Boolean.class.isAssignableFrom(Boolean.class)); // true
// boxing/unboxing
System.out.println(boolean.class.isAssignableFrom(Boolean.class)); // false
System.out.println(Boolean.class.isAssignableFrom(boolean.class)); // false
Is there a pre-existing variant of this method which would consider this case? (i.e. returning true in all four given cases above.) If not, what would be the best way of implementing this for all primitive/wrapped combinations?
This method is about subtyping, not whether one value can be assigned to a variable, which is much more complex, see http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2
I have a WRAPPER_MAP field like this.
WRAPPER_MAP.put(boolean.class, Boolean.class);
// add others
then I look this up.
public static Class wrap(Class clazz) {
Class clazz2 = WRAPPER_MAP.get(clazz);
return clazz2 == null ? clazz : clazz2;
}
Then the test is
wrap(clazz1).isAssignableFrom(wrap(clazz2));
I'm going through a book on data structures. Currently I'm on graphs, and the below code is for the vertex part of the graph.
class Vertex<E>{
//bunch of methods
public boolean equals(Object o){
//some code
}
}
When I try to implement this equals method my compiler complains about not checking the type of the parameter and just allowing any object to be sent it. It also does seem a bit strange to me why that parameter shouldn't be a Vertex instead of an Object. Is there a reason why the author does this or is this some mistake or antiquated example?
#Override
public boolean equals(Object obj)
{
if (!(obj instanceof Vertex)) return false;
else return // blah blah
}
equals(Object) is the method defined in the root - Object. If you don't match the signature exactly, Object's version will be called when someone checks if two objects are equal. Not what you want.
You've probably seen other methods (like Comparator) where you can use the exact time. That's because those APIs were generic-ified with Java 5. Equals can't be because it is valid to call equals with two separate types. It should return false, but it is valid.
equals is a method inherited from Object, is defined to be flexible enough so that you can take any object and test if it is equal to any other object (as it rightfully should be able to do), so how could it be any other way?
Edit 1
Comment from jhlu87:
so is it not good form to write an equals method that has an input parameter of vertex?
You are welcome to create your own overload to any method, including equals, but doing so without changing the name could risk confusing many who would assume that your equals is the one that inherits from Object. If it were my code and I wanted a more specific equals method, I'd name it slightly different from just "equals" just to avoid confusion.
If your method doesn't take an argument of type Object, it isn't overriding the default version of equals but rather overloading it. When this happens, both versions exist and Java decides which one to use based on the variable type (not the actual object type) of the argument. Thus, this program:
public class Thing {
private int x;
public Thing(int x) {
this.x = x;
}
public boolean equals(Thing that) {
return this.x == that.x;
}
public static void main(String[] args) {
Thing a = new Thing(1);
Thing b = new Thing(1);
Object c = new Thing(1);
System.out.println(a.equals(b));
System.out.println(a.equals(c));
}
}
confusingly prints true for the first comparison (because b is of type Thing) and false for the second (because c is of type Object, even though it happens to contain a Thing).
It's because this method existed before generics, so for backward compatabitity it has to stay this way.
The standard workaround to impose type is:
return obj instanceof MyClass && <some condition>;
It is because the author is overriding equals. Equals is specified in java.lang.Object and is something that all classes inherrits from.
See the javadoc for java.lang.Object