简体   繁体   中英

Hiding methods with a subclass parameter

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM