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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:31:11+00:00 2026-05-21T11:31:11+00:00

I am trying to really understand how inheritance and the super keyword work. I

  • 0

I am trying to really understand how inheritance and the super keyword work.

I have the following 4 classes:

People class:

public class People {

    public static void main(String[] args) {
        Person people[] = new Person[4];
        people[0] = new Teacher("Anna-Belle", 37, 6);
        people[1] = new Teacher("John McGil", 43, 12);
        people[2] = new Student("Jason Blue", 25, 17);
        people[3] = new Student("Alice Grad", 22, 34);

        // let's print student's credits
        // and teacher's courses taught

        for (int i=0; i < people.length; i++) {
            Person p = people[i];
            if (p instanceof Teacher) {
                Teacher t = (Teacher) p;
                System.out.println("Teacher #" + t);
            } else if (p instanceof Student) {
                Student s = (Student) p;
                System.out.println("Student #" + s);
            } else { };
        }

    }
}

Person class:

class Person {
    String name;
    int age;

    Person(String name, int age)
        {
            this.name = name;
            this.age = age;
        }

    public String toString() {
        return " "  +   super.toString() + " (Person): " + name + ", " + age;
    }

Teacher class:

class Teacher extends Person {

    Teacher(String n, int a, int c)
        {
            super(n,a);
            coursesTaught = c;
        }

    public String toString() {
        return super.toString()
            + ", (Teacher): " + coursesTaught;
    }

    int coursesTaught;
}

Student class:

class Student extends Person {

    Student(String n, int a, int c)
        { 
            super(n,a);
            creditsCompleted = c;
        }

    public String toString() {
        return super.toString() 
            + ", (Student): " + creditsCompleted;
    }

    int creditsCompleted;
}

What I don’t understand is how the super keyword works I guess. for instance how come the super.toString() in the teacher class knows what to take and convert to a string??

Actually thinking about it, I understand what super does. It says to go to the up a level to the superclass. I guess I am confused as to what happens when these 2 are combined? Any help is appreciated

  • 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-21T11:31:12+00:00Added an answer on May 21, 2026 at 11:31 am

    You are right: super goes up one level and invokes the toString() method on the parent class.

    What might not be obvious, if you are new to Java, is that every class inherits from Object. Every class that does not explicitly extend something, implicitly extends Object. So, in effect, your Person class looks like this (BTW, this still compiles):

    public class Person extends Object {
      ...
    }
    

    For a more visual representation go to your Teacher class in Eclipse and press Ctrl + T

    Hierarchy as presented by Eclipse

    Therefore, when you call toString() on a Teacher instance, toString() is invoked on Person. There another super.toString() call is made. The direct parent of Person is Object thus the default toString() on the Object class is invoked, which looks like this:

    public String toString() {
      return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    

    That is where the first part of your output comes from. The part before the @, is the runtime type of the instance. The hex string after the @ is a hash representing the object instance, which is usually derived from the memory location:

    Teacher #Teacher@5d0385c1 (Person): Anna-Belle, 37, (Teacher): 6

    It is the runtime type that determines which method will be called. E.g.

    Person person = new Teacher("ABC", 37, 6);
    System.out.println(person);
    

    will print this:

    Teacher@4fe5e2c3 (Person): ABC, 37, (Teacher): 6
    

    Even though the compile time type is Person at runtime the toString() on Teacher is invoked.

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

Sidebar

Related Questions

I have the following assembly code snippet I am trying to understand. It is
I'm really trying to understand CakePHP's naming conventions and the following isn't entirely intuitive
I'm really trying to understand the difference between OpenID and OAuth? Maybe they're two
I'm trying to include the Sparkle framework in my application. I don't really understand
I'm trying to understand amazon php sdk for AWS but I really can't use
Hey guys, i'm really trying to understand regular expressions while scraping a site, i've
I am having problems and I don't really understand why. I have a JFrame
I have been trying to understand a bit more about the wider picture of
I have been trying to understand the use of primitives in Java and C#
I'll preface this by saying: I don't really understand how membership providers work in

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.