Linked Questions

96 votes
6 answers
239k views

Is there an easy way to verify that an object belongs to a given class? For example, I could do if(a.getClass() = (new MyClass()).getClass()) { //do something } but this requires instantiating a ...
chimeracoder's user avatar
  • 21.8k
55 votes
4 answers
213k views

Possible Duplicate: How to determine an object's class (in Java)? Java determine which class an object is I have following sample incomplete method to compare the object type of a given ...
Harsha's user avatar
  • 3,608
1 vote
2 answers
10k views

I need to write a new method that checks if certain values are of the type String or not. I have two objects and I want to be able to check if these two objects are strings, if they are to then ...
Joe's user avatar
  • 4,917
-1 votes
3 answers
172 views

The question might look trivial but I couldn't find the solution anywhere! I have an abstract superclass Vehicle, and there are 4 subclasses that extend it, viz Car, Bus, Lorry and Bicycle. I also ...
Prasannjeet Singh's user avatar
-1 votes
1 answer
190 views

if my method is accepting Queues from any type, how to determine if the method arg is ArrayBlockedQueue() or LinkedList() queue? I used .equals as the following its always returning true Queue q1 =...
user6336's user avatar
1 vote
1 answer
87 views

I created a class called "Object" and two classes "Item" and "Scenery" that implement the class Object ( every item is an object and every scenery is an object) During my ...
Wassim Jaoui's user avatar
0 votes
1 answer
62 views

Let's assume I have the following classes: public class AnSuperClass { ... } and public class AnBetterClass extends AnSuperClass { ... } Also I have the following ArrayList: private final ArrayList&...
Doralitze's user avatar
  • 192
-2 votes
3 answers
58 views

I am currently working on a project, in which I have a maze. The maze is a two-dimensional array of Objects. I have several different objects (player, wall, path, minotaur) which will be in the array. ...
SlowChroma's user avatar
-1 votes
1 answer
45 views

Following super-class/sub-class relationship: public abstract class A { public Class<? extends A> getSubClass() { Class<? extends A> clazz = ??? } } public class B extends A {...
Hannes's user avatar
  • 5,342
0 votes
0 answers
44 views

I am working on a school project that involves a parking lot. Our 'parking lot' contains a 10x10 grid and each space can either contain a vehicle, or be empty. I have a superclass of vehicles and ...
dawsUTV's user avatar
  • 19
0 votes
0 answers
41 views

for example in the code below I want to get "Dog" or "Cat". How do I do that? public class Animal { // insert code here } public class Dog extends Animal { // insert code here } public ...
thelili018's user avatar
0 votes
0 answers
20 views

For example, I have class A and its subclasses B and C. I have an Arraylist contains class A's objects. The Arraylist can contain objects from class B and C? I have a foreach loop looping thought the ...
Tran Dai Duong FX17629's user avatar
47 votes
13 answers
29k views

Is there any way in Java to create a method, which is expecting two different varargs? I know, with the same object kind it isn't possible because the compiler doesn't know where to start or to end. ...
T_01's user avatar
  • 1,413
4 votes
5 answers
946 views

I am currently implementing a function, using the superclass as a parameter. For example: private void foo(Parent parent) { if(parent.getClass() == Child1.class) { Child1 c1 = (Child1) ...
am5a03's user avatar
  • 587
0 votes
3 answers
13k views

I was wondering if there is a way to check with the if statement of which class an object is an instance of. I would need something like this: if(object.getClass().equals("class Circle")) ...
Luca Giorgi's user avatar
  • 1,040

15 30 50 per page