0

I'm currently learning polymorphism in Java, and part of the assignment was to create a program that will print out various subclasses using inheritance and polymorphism. I have tried looking for solutions but I couldn't seem to find anyone else experiencing this issue.

Below is a piece of code that is supposed to print Alex and Alexa respectively. However, the output is, instead, Alexa Alexa.

I have tried debugging by stepping through using Eclipse, but I can't pinpoint what is the mistake. I am truly stumped at this point, I've been trying this question for the past week but to no avail. Please forgive me if this is a simple question but I can't figure out what went wrong. I would truly appreciate any assistance!

import java.util.ArrayList;
public class Human {
   protected static String name;

   public Human(String name) {
       System.out.println("In human constructor");
       this.name = name;
   }
   void greetings() {}

   static void print(Human human) {
       System.out.println(name);
   }

   public static void main(String[] args) {
       ArrayList<Human> human = new ArrayList<Human>();
       human.add(new Man("Alex"));
       human.add(new Woman("Alexa"));
       for (int i = 0; i < human.size(); i++) {
           print(human.get(i));
       }
   }
}

class Man extends Human {

   public Man(String name) {
       super(name);
       // TODO Auto-generated constructor stub
   }

}

class Woman extends Human {

   public Woman(String name) {
       super(name);
       // TODO Auto-generated constructor stub
   }

}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97

4 Answers4

2

There are a couple things wrong here:

  1. name should not be static. You want an instance of name for each instance (object) created.

  2. You are printing the wrong name. You want to print the name of the instance passed into print(). So you would want something like this (you would create a getter method for name):

static void print(Human human) {
    System.out.println(human.getName());
}
brso05
  • 13,142
  • 2
  • 21
  • 40
  • Why would you pass a `Human` to itself to print itself? Why wouldn't you just do `void print() { System.out.println(this.name) }` and call it with `yourHuman.print()`? The way you have it now it is not `static` so you would be requiring an instance of `Human` to print a _different_ `Human` which makes no sense. Either have it `static` with a `Human` parameter, or make it not `static` with no parameter – Nexevis Jan 28 '20 at 14:29
  • 1
    @Nexevis you are correct – brso05 Jan 28 '20 at 14:41
1

Do some research on the static context. Here is part of your problem:

protected static String name;

Making name static means that it belongs to the Human class. That means every object of class Human will have the same name.

If you want Human objects to have different names, remove that static modifier.

Next, Human should probably be a POJO, instead of your main class. Something like this:

public class Test {
    public static void main(String[] args) {
        Human human = new Human("Steve");

        System.out.println(human.getName());
    }
}

class Human {
    private String name;

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

    public String getName() {
        return this.name;
    }
}
sleepToken
  • 1,866
  • 1
  • 14
  • 23
1

every Human has his own name, no matter if Man or Woman.

so remove the static keyword from

protected static String name;
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

This is because you've defined name as a static field. so in every object it will have the same value. just remove the static from name.

protected String name;

when a variable, method, ... is defined static, you can have access to it without creating objects. meanwhile it will have the same value in each object. so if you are modifying a static variable, it will keep the last modification.

take a look at this link for more information.

Mohsen_Fatemi
  • 2,183
  • 2
  • 16
  • 25