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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:33:37+00:00 2026-06-10T08:33:37+00:00

I am asking a quite simple question, but I am bit confused in this.

  • 0

I am asking a quite simple question, but I am bit confused in this.

Suppose I have a class Parent:

public class Parent {

    int name;
}

And have another class Child:

public class Child extends Parent{

    int salary;
}

And finally my Main.java class

public class Main {

    public static void main(String[] args)
    {
        Parent parent = new Child();
        parent.name= "abcd";
    }
}

If I make a child object like

Child child = new Child():

Then child object can access both name and salary variables.

My question is:

Parent parent = new Child();

gives the access of only name variable of Parent class.
So what is the exact use of this line??

 Parent parent = new Child();

And also when it is using dynamic polymorphism then why the variable of child class is not accessible after doing this

Parent parent = new Child();
  • 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-10T08:33:38+00:00Added an answer on June 10, 2026 at 8:33 am

    First, a clarification of terminology: we are assigning a Child object to a variable of type Parent. Parent is a reference to an object that happens to be a subtype of Parent, a Child.

    It is only useful in a more complicated example. Imagine you add getEmployeeDetails to the class Parent:

    public String getEmployeeDetails() {
        return "Name: " + name;
    }
    

    We could override that method in Child to provide more details:

    @Override
    public String getEmployeeDetails() {
        return "Name: " + name + " Salary: " + salary;
    }
    

    Now you can write one line of code that gets whatever details are available, whether the object is a Parent or Child:

    parent.getEmployeeDetails();
    

    The following code:

    Parent parent = new Parent();
    parent.name = 1;
    Child child = new Child();
    child.name = 2;
    child.salary = 2000;
    Parent[] employees = new Parent[] { parent, child };
    for (Parent employee : employees) {
        employee.getEmployeeDetails();
    }
    

    Will result in the output:

    Name: 1
    Name: 2 Salary: 2000
    

    We used a Child as a Parent. It had specialized behavior unique to the Child class, but when we called getEmployeeDetails() we could ignore the difference and focus on how Parent and Child are similar. This is called subtype polymorphism.

    Your updated question asks why Child.salary is not accessible when the Childobject is stored in a Parent reference. The answer is the intersection of “polymorphism” and “static typing”. Because Java is statically typed at compile time you get certain guarantees from the compiler but you are forced to follow rules in exchange or the code won’t compile. Here, the relevant guarantee is that every instance of a subtype (e.g. Child) can be used as an instance of its supertype (e.g. Parent). For instance, you are guaranteed that when you access employee.getEmployeeDetails or employee.name the method or field is defined on any non-null object that could be assigned to a variable employee of type Parent. To make this guarantee, the compiler considers only that static type (basically, the type of the variable reference, Parent) when deciding what you can access. So you cannot access any members that are defined on the runtime type of the object, Child.

    When you truly want to use a Child as a Parent this is an easy restriction to live with and your code will be usable for Parent and all its subtypes. When that is not acceptable, make the type of the reference Child.

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

Sidebar

Related Questions

query = Files .Where(file => file.Fileinfo.Name.ToUpper().Contains(textBox1.Text.ToUpper())) .Take(7).ToList(); I hate asking this question, but I
So this is quite the simple question I did not manage to find an
Before asking this question I have Googled for this problem and I have looked
Probably what I'm asking is quite simple but I don't seem to be getting
Question has been asking before, but I am still a bit at a loss
Before asking my question, let me explain the context. CONTEXT: I have a web
I've got a bit of an issue and I've been asking regarding it quite
I've searched quite a bit on both google and stackoverflow, but a lack of
my apologies for asking what must be very simple to solve, but I just
I am quite new to spring coding.. I asked a question earlier but no

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.