-1

I'm trying hard to get this point. In Java I can use the Parent class type using the new operator with the Child class instance. Let's say that I have Parent Human class and Male and Female Child classes. I know that they are in a IS-A relationship, because a Male is-a Human and a Female is-a human. I know that only the methods that are defined in Human and overridden in Male/Female can be used with the Human type AND the Male/Female instance. But the question is: why would I want to use Human as the type of a Male/Female (child) instance?

I've tried a sample "Types" class that creates a Human type Male object. In order for the Male->humanType (or Female->humanType) method to work, I had to declare a Human->humanType method that does nothing. The only thing I could think of, is that by doing this I am able to pass a Human parameter to a method (method signature i.e. Human h1) that is both Male or Female, but in order fot this code to work, I have to declare in Human class ALL the methods that I declare in both Male/Female classes, that in Human class do nothing (similar to abstract methods).

class Types {

  public static void main(String[] args) {

    Types t1 = new Types();
    Human p1 = new Male("Ugo");

    t1.getHumanType(p1);

  }


  public void getHumanType(Human human) {
    human.humanType();
  }

}

class Human {
  String name;

  Human(String name) {
    this.name = name;
  }

  public void humanType() { }
}

class Male extends Human {

  Male(String name) {
    super(name);
  }

  public void humanType() {
    System.out.println("Im a Male");
  }

}

class Female extends Human {

  Female(String name) {
    super(name);
  }

  public void humanType() {
    System.out.println("Im a Female");
  }

}

Is this a good way to run? I just want to understand better this kind of possibility that Java offers (I think it's called Late/Early binding), because I do not fully grasp it. Wasn't abstract class Human more correct?

saga
  • 93
  • 1
  • 7
  • `getHumanType` won't compile, since `Object` does not define `humanType()`. I think you mean to accept `Human` as a parameter. Now, imagine you write a class for a `Car` and wanted it to accept people. You wouldn't want to specify every type of person allowed in the car when designing the car - build the car around the `Human` type, and cars will be able to accept all humans, female or male. – Vince Jul 11 '19 at 15:47
  • @VinceEmigh: you're right: the method signature is `getHumanType(Human human)`. I just pasted a wrong signature. The question remains the same. – saga Jul 11 '19 at 15:52
  • Take a look at this: https://stackoverflow.com/q/2697783/1288408 You should always program to an interface, not an implementation. Your interface (im Java: interface or abstract class) defines the contract you are programming against. The implementation should be hidden, so you can change it to another implementation of your interface. – Modus Tollens Jul 11 '19 at 19:26

1 Answers1

2

The word you are looking for is "polymorphism".

Here is a link to the Oracle Polymorphism Tutorial Page.

DwB
  • 37,124
  • 11
  • 56
  • 82
  • This tutorial adds nothing to the theory I already know and I posted myself on my previous post. I did understand what polymorphism is, but I don't grasp why should I wrote Animal a1 = new Dog or Human h1 = new Male instead of using the subtype. The question is why, not how. – saga Jul 11 '19 at 19:19
  • @saga Statements such as `Human h = new Male()` don't have any use, as far as I know. This feature is useful when the actual variable is defined elsewhere, such as a method parameter or an field member. If we had a field or method parameter `List`, we can change which value is used on-the-fly. If you were told to write a class for a `Car`, but weren't told what types of entities will be in the car, you would define a type `CarEntity` and build your class around that. Others would then derive from that type. You can write `Car` without having to be aware of what exactly may go in the car. – Vince Jul 12 '19 at 18:30