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

  • Home
  • SEARCH
  • 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 6641411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:48:28+00:00 2026-05-25T23:48:28+00:00

I am studying for the AP CS Exam, and came across this practice problem

  • 0

I am studying for the AP CS Exam, and came across this practice problem in the OOP section of my book.
The following two classes are given.

package chap4q9;

public class Person 
{
    private int age;
    public Person(int a)
    {
        age = a;
    }
    public String toString()
    {
        return "Age: " + age + "\n";
    }
}

package chap4q9;

public class Student extends Person
{
    private double gpa;
    public Student(int a, double g)
    {
        super(a);
        gpa = g;
    }
    public String toString()
    {
        return super.toString() + "GPA: " + gpa; //This was where the missing code was
    }
}

And the following is the client program that calls these two classes.

package chap4q9;

public class Chap4Q9 
{
    public static void main(String[] args) 
    {
        Student kathy = new Student(17, 3.85);
        System.out.println(kathy);
    }
}

Finally, the output is:

Age: 17

GPA: 3.85

Just in case you were wondering, there is not actually supposed to be a line between the Age and GPA in the output, that was a weird formatting thing when I posted this.

The goal was to replace missing code in the second toString method (in the code above, the correct answer was inserted for the missing code, but I marked the location). I thought the book was wrong, but ran the code and got the same output. I thought that it would simply print the memory location that kathy was located at, and if you wanted to get that output, you would have to print kathy.toString(). However, just printing kathy seems to be running the toString method in the Student class. My question is, why is printing the Student object, kathy, getting that output, and not simply a memory location.
Thanks in advance for all responses.

  • 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-25T23:48:29+00:00Added an answer on May 25, 2026 at 11:48 pm

    Well that’s just because System.out.println by default calls the toString()-method of an object. In your case you have implemented your own toString in the child class, so this is used.

    Only if you wouldn’t have a toString in your Person and in your Student class, the toString from the Object-class would be called, which prints an object identification string, which consists of the class-name and the hexadecimal representation of the object’s hashCode.

    See the javadoc for the Object-class for more details: http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#toString()

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

Sidebar

Related Questions

I am studying for an exam and came across this question that seems a
I am stuck on this problem which I am studying for an exam tomorrow.
NOTE: THIS IS NOT HOMEWORK IT IS FROM A PRACTICE EXAM GIVEN TO US
I'm studying for an exam, and this is a problem from an old test:
I'm studying for an exam, and this was on an old test: Given: public
I'm studying for an exam and I'm having difficulty with a concept. This is
I am just studying a few classes given to me by my lecturer and
I'm studying this book (Addison Wesley Windows System Programming 4th Edition) and I think
I'm studying for an exam, and a previous exam had this question: 1. void
While studying for the SCJP 6 exam, I ran into this question in a

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.