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 8458847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:11:21+00:00 2026-06-10T13:11:21+00:00

Say, I have a simple class Fruit defined as follows: public class Fruit {

  • 0

Say, I have a simple class Fruit defined as follows:

public class Fruit {
    int price;

    public boolean isEqualPrice(Fruit object) {
        return (object.price == price);         
    }
}

Now, there are several subclasses of Fruit such as Apple, Orange, Mango etc. I want the method isEqualPrice to be valid only when these subclasses are the same. For example, Apple.isEqualPrice(Apple) would be valid, but Apple.isEqualPrice(Orange) would not be. How should I define the parameter of isEqualPrice to acheive this? I don’t want to override isEqualPrice in every subclasses of Fruit.

One possible solution might be:

public boolean isEqualPrice(Fruit object) {
        if(this.getClass() != object.getClass())
            throw new IllegalArgumentException();
        return (object.price == price);         
    }

But it will report classes are same or not only at runtime, and an user has no way to realize the correct parameter type watching the method signature. Is there any way to detect this at compile time?

NOTE: I think compareTo method of enum is such kind of method, as every enum has this method and a type of enum can’t be compared to other type. But I couldn’t understand how it is implemented.

  • 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-10T13:11:23+00:00Added an answer on June 10, 2026 at 1:11 pm

    Make Fruit generic, or add a generic subclass of fruit and derive Apple, Orange etc. from that.

    class Fruit {
       ...
    }
    
    class FruitGeneric<T extends Fruit> extends Fruit {
        public boolean isEqualPrice (T otherFruit) {
            ...
        }
    }
    
    class Apple extends FruitGeneric<Apple> {
       ...
    }
    
    class Orange extends FruitGeneric<Orange> {
       ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a simple object such as class Something { public int SomeInt
Let's say I have a simple POJO class: public void Foo { public int
Say we have this hyper simple class hierchy: public class SomeMath { public int
Let's say we have the following simple model: public class Car { public int
Let us say we have a simple business object: class SimpleBO { public string
Say I have a simple address class like below: public class Address { public
Let's say I have a regular simple Java class, such as: public class Foo
Simple question on best practice. Say I have: public class Product { public string
Lets say we have a simple class public class Foo { public string FooName;
let's say we have simple data class: public class Ex { public string Prop1

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.