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 8916793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:20:00+00:00 2026-06-15T05:20:00+00:00

I must have missed something basic in inheritance since it doesn’t make sense to

  • 0

I must have missed something basic in inheritance since it doesn’t make sense to me.

Given these two classes:

class Person
{
   private String name;
}

class Student extends Person
{
   private String degree;
}

Why is it possible to create a new Student object, store it in a Person type variable and still be able to keep the variable String degree and have access to it?

Person p = new Student("John", "Master's");

I thought that this is possible to use the above initialization because p will not have any variable other than name, since a Person variable can’t contain a degree variable, and so it’s legal – but when you do that, degree doesn’t exist in p.

That’s what I thought the reason was behind the fact that the opposite is illegal
(Student s = new Person("John");) – because then s will not have its other variable (degree) initialized.

  • 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-06-15T05:20:02+00:00Added an answer on June 15, 2026 at 5:20 am

    A variable with a type of reference to a supertype can hold a reference to an instance of a subtype. Assigning the subtype instance to the variable does not change the instance in any way; it just broadens the type of reference. All the instance data is still there, and can be retrieved by a down-cast. That is,

    Student s = new Student("John", "Master's");
    Person p = s; // no cast needed for a widening conversion
    

    is exactly the same as:

    Person p = new Student("John", "Master's");
    Student s = (Student) p; // cast required for a narrowing conversion
    

    EDIT Perhaps this will help clarify things.
    Suppose there was a second subclass of Person:

    class Teacher extends Person
    {
        private String department;
    }
    

    Now you can do this:

    Person p = new Teacher("Fred", "Physics");
    Teacher t = (Teacher) p;
    

    But if you later tried to do this:

    p = new Teacher("Fred", "Physics");
    Student s = (Student) p; // Error!
    

    you’ll get a ClassCastException because at that point in the program, p is now a reference to an object (an instance of Teacher) that is not a Student.

    Here’s an analogy. In English, you might say, “I have a pet animal”. This says nothing about the type of animal. (You cannot know whether the pet barks, for instance.) On the other hand, if you say, “I have a pet dog”, then it makes sense to ask whether it barks at strangers. The key point is: calling your pet an animal doesn’t change the fact that it is (or is not) a dog. Exactly the same thing is going on with Java references—using a more general (e.g., base class) reference type to store the object reference does not change the nature of the object itself. You lose knowledge of the details of the object, but those details are still there.

    To repeat the point I made in my comment: you never assign objects to variables in Java; you only assign references to objects. Or, to put it another way, none of your variables are objects.

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

Sidebar

Related Questions

I must have missed something regarding generics in Java, but why does this not
Here is example: http://jsfiddle.net/pcNzb/9/ I just study, so i must have missed something or
I must have missed something in my LINQ training. In Linq to SQL using
I must have missed something obvious, but how do you create a scala-tuple in
I must have missed something here. I am trying to create a table but
I must have missed something. I'm doing an exercise to learn c++ and it
I must have overlooked something completely obvious?? but my button displays its image and
I must have done something wrong. I have a C# project in Visual Studio
I'd like to get with LINQ an employee list, these employees must have in
I have seen many examples, with many 'no, you missed something' comments. What is

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.