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

  • SEARCH
  • Home
  • 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 8108877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:14:47+00:00 2026-06-06T01:14:47+00:00

Suppose I have a class hierarchy as follow: class Vehicle; class Car extends Vehicle;

  • 0

Suppose I have a class hierarchy as follow:

class Vehicle;
class Car extends Vehicle;
class Plane extends Vehicle;

I have a function which compares the two object

public <T extends Vehicle> generateDiff(T original, T copy)

At compile time, the method above guarantees the two objects is Vehicle, but it cannot make sure the types of the two object are the same.

generateDiff(new Car(), new Car()); //OK
generateDiff(new Plane(), new Plane()); //OK
generateDiff(new Car(), new Plane()); //WRONG

Can I achive this at compile time using Generics?

P.s: currently, I’ve implemented it will throw exception if the Class of two objects are not the same. But I’m not satisfied with this.

Thanks in advance.

  • 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-06T01:14:49+00:00Added an answer on June 6, 2026 at 1:14 am

    Yes, you can (kind of)!

    The type T is being inferred from the arguments, but you can specify the type:

    MyClass.<Car>generateDiff(new Car(), new Plane()); // generates a compile error
    

    Without the typing the method, the type T is inferred to be the narrowest class that satisfies the bounds as used, so for parameters Car and Plane, the narrowest type that will work is Vehicle, so these two lines are equivalent:

    generateDiff(new Car(), new Plane()); // type is inferred as Vehicle
    MyClass.<Vehicle>generateDiff(new Car(), new Plane());
    

    The above code assumes the generateDiff() is a static method. If it’s an instance method, you could type your class and have that type used in your method.

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

Sidebar

Related Questions

Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}
Suppose I have such exceptions hierarchy: public class A extends RuntimeException { ... }
Suppose I have a composite class PharmaProduct (which represents the product hierarchy of a
Suppose I have: class A{ public $one; public $two; } and an array with
Suppose I have the following class hierarchy: public abstract class Organization { /* properties
Suppose I have class MyObject { Object object1 = new Object(); Object object2; public
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose I have the following hierarchy: class A { public: A() private: int aa;
Suppose you have a Java class hierarchy of about 30 classes, with a base
Suppose I have: class Foo { ... }; class Bar : public Foo {

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.