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

The Archive Base Latest Questions

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

I want to understand how hiding works in Java. So lets assume you have

  • 0

I want to understand how hiding works in Java.
So lets assume you have following code

public class A{
    protected SomeClass member;
    public A(SomeClass member){
        this.member = member;
    }
}  

public class B extends A{
    protected SomeClass member;
    public B(SomeClass member){
        super(member);
    }
    public static void main(String[] args){
        SomeClass sc = new SomeClass();
        B b = new B(sc);
        System.out.println(b.member.toString());
    }
}

If I compile I get honored with a NullPointerException. I thought it would be the output of sc.toString();

I change this code to

public class A{
    protected SomeClass member;
    public A(SomeClass member){
        setMember(member);
    }
    public void setMember(SomeClass sc){
       this.member = sc;
    }

}  

public class B extends A{
    protected SomeClass member;
    public B(SomeClass member){
        super(member);
    }
    public void setMember(SomeClass sc){
       this.member = sc;
    }
    //...main
}

and it get the output as expected…
ok setMember of B Overrides the one from A so I can explain this in this way.
I played around a little bit and removed setMember from B to get Back my NullPointerException. But it compiles again and gives me output if I change A’s Code to

public class A{
    protected SomeClass member;
    public A(SomeClass member){
        setMember(member);
    }
    public void setMember(SomeClass sc){
       member = sc;
    }

} 

it seems to me that there are in fact two instances of SomeClass member… but what means shadowing if there are two instances? Is hiding only usefull for the second case?

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

    I’m going to assume you meant the last line of the first code sample to be b.member.toString().

    There are two member variables named “member” and in both of your examples only one of them is being set, becuase only one assignment to this.member is being called. To fix the first example you would usually say

    public B(SomeClass member) {         
        super(member);
        this.member = member;
    } 
    

    But I think you already understand that and are really asking why the language is designed this way. It has to do with the encapsulation of the superclass implementation. The author of the superclass should be able to rewrite it without breaking subclasses and vice versa. Imagine if B.member came first because the author of B though “member” would be a good thing to have around, and later the author of A had the same thought.

    This system isn’t perfect, however, and your second example shows why. If B.setMember() came first and then a later version of A introduces A.setMember(), then the author of A might fail to anticipate an overriding B.setMember() method, and write the constructor the way you show, with the result that A.member never gets initialized. C# introduced the “overrides” keyword to catch this sort of thing, and Java borrowed it as “@overrides”, but that annotation isn’t madatory in Java so it isn’t as effective.

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

Sidebar

Related Questions

I want to understand median of medians algorithm on the following example: We have
I want to understand if code snippets are what I am looking for here.
I want to understand the use of xargs man in Rampion's code : screen
I want to understand how a C++ program that was given to me works,
I just want to understand why I cannot create a protected enum on C#?
I have a .exe app which I want to understand better - I can
I want to understand how Configure::write('Config.language','fre'), do I have to call this function in
I want to understand the following pattern, step by step. /\p{L}/u /u is a
I want to understand if the following coding practice is good or bad practice.
I want to understand how locking is done on static methods in Java. let's

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.