Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Polymorphorism with array objects of different classes?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Polymorphorism with array objects of different classes?

    Hey guys, I'm stuck on this problem and I'm getting absolutely nowhere with it.

    Our Person class has several subclasses, namely CollegeKid and Oldie. Suppose there is yet another subclass: Programmer. Notice that, like the other subclasses, a Programmer also has an additional data member: caffeinatedBeveragesConsumed. Also notice that we provide Programmer with a print method. All of these subclasses may be referred to as members of the Person class. This is an example of polymorphism.

    Suppose we have an array containing a number of Person objects. We want to print information for all of the objects in the array. We can do this by looping through the array and calling each object's print method. The objects in the array can be instances of any of the above classes.

    For this exercise, enter code in the box below that will allow the printPeople method to produce a printout to the console of all of the "people" in the array.

    public void printPeople (Person[] people) {
    //code goes here
    }

    I'm not sure how to even begin writing this code. I know I need a for loop and at every increment call the print method of the subclass.. so I have

    public void printPeople (Person[] people){
    for (int i=0; i<people.length; i++){
    print();
    }
    }


    Here are all the subclasses:
    /**
     * This class contains data and methods pertaining
     * to a person. The data contained in this class are:
     *  name and age.
     */
     
     public class Person
     {
      private String name;
      private int age;
     
      public Person(String n, int a)
      {
        this.name = n;
        this.age = a;
      }
     
      public String getName()
      {
        return name;
      }
     
      public int getAge()
      {
        return age;
      }
     
      public void setName(String n)
      {
        name = n;
      }
     
     public void setAge(int a)
     {
       age = a;
     }
     
      public void print( )
      {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
      }
    }
     
     
     
    /**
    * This class contains data and methods pertaining
    * to a college kid. The datum contained in this class is:
    * grade point average- gpa. This class derives from Person.
    */
     
    public class CollegeKid extends Person
    {
      private double gpa;
     
     
      public double getGPA()
      {
        return gpa;
      }
     
      public void setGPA(double g)
      {
        gpa = g;
      }
     
      public void print( )
      {
          /* print code here */
      }
    }
     
     
     
    /**
    * This class contains data and methods pertaining
    * to an oldie- i.e. someone over the age of, oh say 30.
    * The datum contained in this class is:
    * whether or not this person is a Perry Como fan.
    * This class derives from Person.
    */
    public class Oldie extends Person
    {
      private boolean perryComoFan;
     
     
      public boolean isPerryComoFan()
      {
        return perryComoFan;
      }
     
      public void setPerryComoFan(boolean f)
      {
        perryComoFan = f;
      }
     
      public void print( )
      {
          /* print code here */
      }
    }
     
     
     
     /**
     * This class contains data and methods pertaining
     * to a programmer. The datum contained in this class is:
     * caffeinatedBeveragesConsumed. This class derives from Person.
     */
     
     public class Programmer extends Person{
     
      private int caffeinatedBeveragesConsumed;
     
     
      public int getCaffeinatedBeveragesConsumed()
      {
        return caffeinatedBeveragesConsumed;
      }
     
      public void setCaffeinatedBeveragesConsumed(int cbc)
     {
       caffeinatedBeveragesConsumed = cbc;
     }
     
      public void print( )
      {
          /* print code is provided */
      }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Polymorphorism with array objects of different classes?

    Does the code compile and execute? If you are getting errors, copy the full text and paste it here.

    Do you have any specific questions about the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Polymorphorism with array objects of different classes?

    All it says is that it cannot find the symbol method print() so no it doesnt execute.

    My only problem with exercises like this is that I don't know how to call a method of a specific subclass. I have another problem like this that uses the same subclasses above but goes through the array and figures which positions contain CollegeKids and calculates the average GPA.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Polymorphorism with array objects of different classes?

    know how to call a method of a specific subclass.
    You need to have a reference to an instance of the class with the method you want to call
    refToClassWithMethod.theMethodToCall(<ARGS GO HERE>);

     System.out.println("Age: " + age);
    Here the method: println() is called using the object reference: System.out

    cannot find the symbol method print()
    Where is the method: print() defined?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Polymorphorism with array objects of different classes?

    print() is defined in Person and all three subclasses. So when I am going through this loop, the object may be from subclass Oldie, and the next may be from subclass CollegeKid. So if I need to call Oldie.print() or CollegeKid.print() respectively, how do I get the loop to determine what subclass print() method I am calling?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Polymorphorism with array objects of different classes?

    If a sub class overrides the base class's method, you'll be calling the sub class's method.

    If you have a reference to a class and use it to call a method, you will be calling a method in that class.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Polymorphorism with array objects of different classes?

    Right I knew that but my question is say if I have an array of people of length 3. I want to call the print() method for each position of the array, but each position is from a different sub class, and I don't know which. If I need to use a reference to a class to call each different print method, how do I write the code so that it determines what class the object in the position is from, and uses it to call it's respective print() method?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Polymorphorism with array objects of different classes?

    Sounds simple enough to test. Write some small classes with methods, compile and execute it and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Classes and Array of objects
    By dynasty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 27th, 2013, 01:53 AM
  2. Two problems (Dealing with Classes and Objects)
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2012, 08:17 PM
  3. Understanding Classes and Objects
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2012, 11:35 AM
  4. Creating classes with generic objects
    By rbt in forum Collections and Generics
    Replies: 2
    Last Post: April 23rd, 2012, 08:08 PM

Tags for this Thread