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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:46:55+00:00 2026-06-14T19:46:55+00:00

Possible Duplicate: force base class to use its own method and not overrided method

  • 0

Possible Duplicate:
force base class to use its own method and not overrided method

Suppose I have these classes — sorry, it’s kind of hard to think of a simple example here; I don’t want any why-would-you-want-to-do-that answers! –:

class Squarer
{
    public void setValue(int v)
    {
        mV = v;
    }
    public int getValue()
    {
        return mV;
    }
    private int mV;
    public void square()
    {
        setValue(getValue() * getValue());
    }
}

class OnlyOddInputsSquarer extends Squarer
{
    @Override
    public void setValue(int v)
    {
        if (v % 2 == 0)
        {
            print("Sorry, this class only lets you square odd numbers!")
            return;
        }
        super.setValue(v);
    }
}

// auto s = new OnlyOddInputsSquarer();
OnlyOddInputsSquarer s = new OnlyOddInputsSquarer();
s.setValue(3);
s.square();

This won’t work. When Squarer.square() calls setValue(), it will go to OnlyOddInputsSquarer.setValue() which will reject all its values (since all squares are even). Is there any way I can override setValue() so that all the functions in Squarer still use the method defined there?

PS: Sorry, Java doesn’t have an auto keyword you haven’t heard about! Wishful thinking on my part.

Edit: I can’t modify Squarer!

  • 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-14T19:46:56+00:00Added an answer on June 14, 2026 at 7:46 pm

    It seems to me that the class Squarer is not well designed. If you really need a dirty trick to make this work, you could also override the method square():

    class OnlyOddInputsSquarer extends Squarer
    {
        @Override
        public void setValue(int v)
        {
            if (v % 2 == 0)
            {
                print("Sorry, this class only lets you square odd numbers!")
                return;
            }
            super.setValue(v);
        }
        @Override
        public void square()
        {
            super.setValue(getValue() * getValue());
        }
    }
    

    But… a square of a odd number is not even, so this should not really be a problem. I suppose this is just an example and your real problem is different.

    Edit: OK, if this doesn’t work, there is even a dirtier method: in setValue check the stack and if it was called from square call super.setValue instead. I don’t recommend this, but If you really really need to get this done, check here to see how to do it.

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

Sidebar

Related Questions

Possible Duplicate: How can I force inheriting classes to implement a static method in
Possible Duplicate: Mobile site - force landscape only / no auto-rotate I have seen
Possible Duplicate: Force Download an Image Using Javascript Basically I want to have the
Possible duplicate: why does the css min-width attribute not force a div to hae
Possible Duplicate: How to force using zxing lib with only my application? I use
Possible Duplicate: jquery: how to force a pdf download automatically? Afternoon folks, I have
Possible Duplicate: Passing a Int value to another class I have been trying to
Possible Duplicate: How to force child same virtual function call its parent virtual function
Possible Duplicate: How to force child same virtual function call its parent virtual function
Possible Duplicate: Is there a way to force a C# class to implement certain

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.