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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:12:18+00:00 2026-05-24T22:12:18+00:00

Not sure if this is called method hiding or method overriding, or neither, and

  • 0

Not sure if this is called “method hiding” or “method overriding”, or neither, and would like to be directed to some good articles about the subject. Especially, whether or not it is good practice, when and when not to use it, and the advantages/disadvantages of using it.

public class Animal {

  /* constructor */
  Animal () { }

  /* instance method */
  void add(Number n) {
    System.out.println("An animal added a number!");
  }

  /* Main method */
  public static void main(String[] args) {
    Integer i = 2;   // Integer i = new Integer(2);
    Double d = 3.14; // Double d = new Double(3.14);

    Animal mammal = new Animal();
    Cat tom = new Cat();
    Mouse jerry = new Mouse();

    mammal.add(i); // produces "An animal added a number!"
    mammal.add(d); // produces "An animal added a number!"

    tom.add(i);    // produces "Tom added an integer!"
    tom.add(d);    // produces "An animal added a number!"

    jerry.add(i);  // produces "An animal added a number!"
    jerry.add(d);  // produces "Jerry added a double!"
  }
}

class Cat extends Animal {

  /* constructor */
  Cat () { }

  /* instance method */
  void add(Integer i) {
    // param of type Integer extends Number
    System.out.println("Tom added an integer!");
  }
}

class Mouse extends Animal {

  /* constructor */
  Mouse () { }

  /* instance method */
  void add(Double d) {
    // param of type Double extends Number
    System.out.println("Jerry added a double!");
  }
}

EDIT:

Thanks to @MByD, found out this is called “method overloading”.

New question related to above:
In the Animal class, I want to create a method that takes a Number object and uses one of the overloaded add() methods in the subclasses Cat and Mouse. Is there a better way to do this than what’s shown below?

public class Animal {
...
  void subtract(Number n) {
    if      (n instanceof Integer) this.add(-(Integer) n); // from the Cat class
    else if (n instanceof Double)  this.add(-(Double) n);  // from the Mouse class
    ...
  }
...
}

Yes, I realize I could just write this.add(-n), but I’m wondering if there’s a way to choose an implementation dependent on the subclass of a parameter. Since the parameter is an abstract type and can’t be instantiated, I must pass a subclass as an argument.

  • 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-05-24T22:12:19+00:00Added an answer on May 24, 2026 at 10:12 pm

    This is called method overloading, since the signature of the methods are not the same.

    See the Java Tutorials of methods:

    The Java programming language supports overloading methods, and Java
    can distinguish between methods with different method signatures. This
    means that methods within a class can have the same name if they have
    different parameter lists (there are some qualifications to this that
    will be discussed in the lesson titled “Interfaces and Inheritance”).

    The argument whether and when to use all kinds of overloading / overriding / shadowing etc. is a big one. A very good resource is the book Effective Java, By Joshua Bloch. Which I found very useful and interesting.

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

Sidebar

Related Questions

I'm not sure if this is possible or not, but what I would like
My apologies for an inaccurate title, but I'm not sure what this is called
I'm not even sure what this is called? But I'm trying to learn what
I'm not sure what this practice is actually called, so perhaps someone can edit
I'm not even sure what this principle is called or how to search for
Not sure this is possible, but looking to write a script that would return
I'm still not sure this is the correct way to go about this, maybe
I'm not sure what this is called, which is why I'm having trouble searching
Sorry for the vague title, but I'm not sure what this is called. Say
Ok, I am not sure what the naming for this is actually called, but

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.