Questions tagged [instanceof]

instanceof is an operator available in some object-oriented languages, including Java, php and JavaScript. Generally speaking, it allows the programmer to check whether an object passed as its left operand is an instance of a class specified by the right operand.

instanceof is an operator available in some object-oriented languages, including Java, php and JavaScript. It allows the programmer to check whether an object passed as its left operand is an instance of a class specified by the right operand.

instanceof in Java

instanceof in php

instanceof in JavaScript

958 questions
-1
votes
3 answers

In Java can we write "SuperClassObject instanceof SubClass"?

Can I write a code like : class Shape { } class Circle extends Shape { } public class Main { public static void main(String args[]) { Shape shape = new Circle(); if (shape instanceof Circle) { Circle C =…
Jorvis
  • 386
  • 5
  • 13
-1
votes
2 answers

I am confused about Polymorphism and object of class

class A {} class B extends A{} A objectX = new B(); What does the last line mean? Is it a object of class A or Is it a object of class B. Does instance and object have same meaning ? Is objectX instance of A or is it an instance of B? When we run…
Mahesh Gupta
  • 55
  • 1
  • 10
-1
votes
1 answer

Invoking annotated methods without creating instances for all the annotated methods

I have two annotations in a class say @annotation_1 and @annotation_2 which means that I have created two instances. ---Please correct this statement if there is any error! Now, I find all the methods that are annotated with @annotation_1 or…
nt fury
  • 11
  • 5
-1
votes
2 answers

How can I access non-static methods for instances of objects stored inside of an array?

I'm creating a simulation for one of my classes and I'm running into some trouble. Looking for some direction here + any tips. I have two classes so far and I'm looking to implement some other things. At the moment, I want to be able to access the…
mmuso
  • 11
  • 3
-1
votes
1 answer

False result when using instance of ElementFinder

I just do very very basic thing but the result is also false: console.log("Check element "+ (element(txt_LoginUsername) instanceof ElementFinder) ); Check element false But it gives the right result if I use the debug tool of webstorm. I just update…
Mike
  • 83
  • 8
-1
votes
1 answer

How to examine objects of an ArrayList (java)

I have an abstract class Rental...I have two more subclasses,SuiteRental and SimpleRoomRental that inherits Rental class. I have an ArraList in a third class Hotel (not connected with the others) that contains both objects SimpleRoomRental and…
Manolis
  • 9
  • 1
-1
votes
3 answers

Instanceof syntax error?

I have an exercise which relates to "instanceof" and I am not quite sure how to use it. This is what I came up with: for(int i = 4; i < 6; i++){ int availSupply = part[i].stockLevel+part[i].getAvailForAssembly(); if(availSupply instanceof…
Tam Lam
  • 135
  • 1
  • 3
  • 13
-1
votes
1 answer

how to simplify this statement using indexOf?

How can I simplfy the following text inside the if statement in Javascript using "indexof"? if (a === 1 || a === 2 || a === 3) { return "correct"; }; I am guessing an array needs to be made for 1,2, and 3, but am unsure of how to us instanceof…
-1
votes
1 answer

javascript inheritance and instanceof operator discrepancy

Consider the Constructor functions: function ConstructorOne(){/*.....*/} function ConstructorTwo(){/*.....*/} Consider the following js code: var myInstance = new ConstructorOne(); ConstructorOne.prototype=new ConstructorTwo(); myInstance…
sumeet batheja
  • 329
  • 1
  • 2
  • 13
-1
votes
2 answers

Object Type resolution: instanceof

When I try to compile and run this section of code, I get this erro: Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: listener cannot be resolved to a variable at…
Stats4224
  • 778
  • 4
  • 13
-1
votes
3 answers

instanceof on enum variables

EDIT: Please skip reading details of this question, it's a simple oversight. ORIGINAL MESSAGE: So apparently instanceof on enum variables is a weird thing, an example: Let this be enum definition: public enum CardType { CLERIC, UNDEAD, …
Mladen Adamovic
  • 3,071
  • 2
  • 29
  • 44
-1
votes
1 answer

How to create a PHP equivalent of a checked cast

What is the most simple way to mimic a Java/C# style checked cast in PHP (e.g., (X)y)? I.e., the semantics should be that nothing is done if y is of X or a subclass, but if it is not, an exception should be thrown. The quickest way I can think of is…
gexicide
  • 38,535
  • 21
  • 92
  • 152
-1
votes
2 answers

Check instanceof a type from name

I want to do this: Get a type from a name, and the fail a test if the message. I suppose it doesn't work because clazz is not a type. But how do I go about this? Class clazz=Class.forName("com.android.test."+myEnum.toString()); if(myObj…
JohnyTex
  • 3,323
  • 5
  • 29
  • 52
-1
votes
2 answers

Switch method based on type of class using instanceof or reflection?

I have a data model with some classes that are derived from a single interface, like this: public interface Foo extends Visitable {} public class BarA implements Foo { void accept(Visitor visitor) { visitor.visit(this); } …
klingt.net
  • 2,019
  • 3
  • 18
  • 19
-1
votes
4 answers

Weird behaviour of if(x instanceof X)

I have a class PartA and a class PartB which is a derived class of PartA. I have a list of type PartA which holds both parts a and b. And I need to determine which kind of class it actually is when I loop through it. But if I do this: for (PartA i :…
NewProger
  • 2,945
  • 9
  • 40
  • 58