Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7992941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:42:47+00:00 2026-06-04T13:42:47+00:00

I have two classes Class1 and Class2, Class1 obj; Class2 obj1; How can I

  • 0

I have two classes Class1 and Class2,
Class1 obj;
Class2 obj1;
How can I compare obj is instance of Class2 ?
I am not able to use instanceOf operator for these classes, It is giving compilation error “cant’t compare incompatiable types”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:42:48+00:00Added an answer on June 4, 2026 at 1:42 pm

    If it’s failing at compile time, you can be sure that it would always fail at run time. The compiler won’t allow you to use instanceof on two types where the first can’t be an instance of the second. e.g.:

    Integer i = 5;
    if (i instanceof String) {
        System.out.println("Never happen");              
    }
    

    The compiler knows definitively that an Integer can never be a String because they’re not in the same inheritance hierarchy. Therefore it won’t even let you write it. If you’re tempted to write it anyway, then there’s something wrong with your design and/or your logic.

    That being said, Class.isAssignableFrom() is another way to check inheritance hierarchies that can only fail at runtime:

    Integer i = 5;
    if (String.class.isAssignableFrom(i.getClass())) {
        System.out.println("Never happen");
    }
    

    It bears repeating that, while the above will compile successfully, the condition still can never be true. It only makes sense to compare types when they’re in the same inheritance hierarchy. For instance, this is a sensible comparison to make:

    Number n = 1;
    if (n instanceof Integer) {
        System.out.println("I'm an int!");
    } else if (n instanceof Long) {
        System.out.println("I'm a long!");
    }
    

    If you’re tempted to write code like this, though, it almost always means that you’ve broken polymorphism in your code, since this is exactly the sort of thing it’s supposed to handle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, a class1 which is an NSObject and a class2 which
Imagine I have two classes, class1 and class2. Class1 inherits from baseClass, which looks
I've got a basic question about obj-c coding, say I have two classes represented
Inside a DLL, we've defined two classes (Class1 and Class2) which inherit from an
I have the following two classes: public class Class1 { public Class1 randomvariable; //
my code is like this: i have two classes first class: public class Box<E>
I have two classes class A { public string something { get; set; }
I have these two classes public class Person { } public class Company {
I have two classes, ClassA and ClassB , as well as a many to
I have two classes that I am testing (let's call them ClassA and ClassB).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.