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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:13:46+00:00 2026-05-24T08:13:46+00:00

I’ve a code: class Parent{ int x=10; void show(){ System.out.println(x); } } class Child

  • 0

I’ve a code:

class Parent{
    int x=10;
    void show(){
        System.out.println(x);
    }
}

class Child extends Parent{
    int x=20;
    public static void main(String[] args){
        Child c=new Child();
        c.show();
    }
}

Here when I run this program the output comes is 10. Which means at runtime Parent member-function not using the Child data-member with same name(data hiding). The thing I know is that whenever we are extending a class its parent class member-function and data-members are available to Child class, then when I say c.show() why it takes the data-member of Parent class not Child class. Also, I want to know that when we create an object of Child class its Parent class data-members are put in the Parent section of Child class object in Heap, but what happens to member-functions?

  • 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-24T08:13:48+00:00Added an answer on May 24, 2026 at 8:13 am

    The child can’t hide a field from a method in the parent class by simply declaring its own version of the field. You are applying the idea of “data hiding” in reverse. The child can never hide fields from the parent, but the parent can hide fields from the child (by declaring x as private). If you wanted to, you could use encapsulation to achieve your desired effect, like:

    class Parent{
        private int x=10;
    
        public int getX() {
            return x;
        }
    
        void show(){
            System.out.println(this.getX());
        }
    }
    
    class Child extends Parent{
        int x=20;
    
        @Override
        public int getX() {
            return x;
        }
    
        public static void main(String[] args){
            Child c=new Child();
            c.show();
        }
    }
    

    By overriding the getX() accessor method, the child can hide the parent’s value of x from the rest of the world.

    Some other points worth discussing:

    The thing I know is that whenever we are extending a class its parent
    class member-function and data-members are available to Child class

    This is not entirely correct. Private methods and data members will not be (directly) available to the child class. Only public, protected, and “package-level” methods and fields will be directly accessible to the child class.

    Also, I want to know that when we create an object of Child class its
    Parent class data-members are put in the Parent section of Child class
    object in Heap, but what happens to member-functions?

    Member functions aren’t created per-instance. It would be terribly inefficient if the JVM worked like that. Instead, the method implementations for a given class are defined only once, as part of the Permanent Generation which holds things like class definitions and other internal JVM state. All instances of a given class will share the same method definitions.

    In the case of a child class, it will share the method definitions of its parent, and it will also have its own definitions added for any methods that exist just in the child (and for any methods that it overrides).

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

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a jquery bug and I've been looking for hours now, I can't

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.