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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:43:15+00:00 2026-06-05T17:43:15+00:00

I am new to java and I have some problem on Casting. I have

  • 0

I am new to java and I have some problem on Casting.
I have a class called Parent, and a class called Children, the Children class is the subclass of Parent.

public class Parent
{
    int age;
    String occupation;

    public void print()
    {
        System.out.println("My age is:"+ age + "and i am a:" + occupation);
    }
}



public class Children extends Parent
{
    int height;

    public static void main(String args[])
    {
        Children c = new Children();
        Parent p = new Parent();

        p=c;
        c=(Children) p;

        /**Here***/



    }

}

My problem is, when I add p.XXX after the casting, I only see the age and occupation accessible for instance p. and when I do c.xxx, I see all age, occupation and height accessible.
I thought when I do p=c, p now are considered to be an instance of a children class isn’t it? If yes, then why i didnt see the height integer accessible?
And when I do c=(Children)p, an instance of Parent class is assigned to an instance of Children class, and since the Parent instance doesn’t have a option, that why we do a casting from parent to children, correct?

  • 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-05T17:43:17+00:00Added an answer on June 5, 2026 at 5:43 pm

    I thought when I do p=c, p now are considered to be an instance of a children class isn’t it?

    No. The type of the variable p is still Parent. The value will be a reference to an instance of Children, but the compiler doesn’t take that into account. It only uses the declared type of the variable.

    And when I do c=(Children)p, an instance of Parent class is assigned to an instance of Children class, and since the Parent instance doesn’t have a option, that why we do a casting from parent to children, correct?

    Well you’re not changing the instance at all. You’re casting an expression of type Parent – the value will be a reference. At execution time, the JVM will ensure that the value is actually a reference to an instance of Children or some subtype (or null) – if it isn’t, the JVM will throw an exception.

    It’s very important to distinguish between variables, references and objects – they’re three quite different concepts, and making sure you understand the difference between them will make a lot of other things clearer.

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

Sidebar

Related Questions

I am used to Java and new to QT/C++, and I have some problems
I'm new to Java, but have some OOP experience with ActionScript 3, so I'm
I am a bit new with android but I have some java experience. So
I'm new to Java EE and have been struggling with some basic middleware concepts
I am kind of new to C (I have prior Java, C#, and some
I am new to Java. I just read that class variables in Java have
I'm new to android. I have some java knowledge (not extensive), and I've done
I'm new to Java and I have come to having the following problem: I
I have some problem with the nested try statements in java and I am
I have some problem with generics and collection. Java support generics only at compilation

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.