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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:36:51+00:00 2026-06-11T11:36:51+00:00

Java is object oriented programming language.Inheritance is one of most important features. We use

  • 0

Java is object oriented programming language.Inheritance is one of most important features.
We use encapsulation to hide object states.In the below program i should have used private access modifier for age and counter but to test this inheritance i have used public.

  1. Why inheritance does not work in case of direct object state access
    (primitive types or object reference). SOP 3 output is different from expected.
  2. Since compiler does not give any error in line SOP 13 and SOP 14. Why Father class details are printed not child class.

.

public class Father {

    public int age = 50;
    /*
     * Counter keeps track of total no of instances created so far.
     */
    public static int counter = 0;

    public Father(){
        super();
        synchronized (Father.class) {
            ++Father.counter;   
        }
    }

    public int getAge(){
        return this.age;
    }

    public static int getStaticCount(){
        return Father.counter;
    }

}

public class Child extends Father {

    public int age = 25;
    public static int counter = 0;

    public Child(){
        super();
        synchronized (Child.class) {
            ++Child.counter;    
        }
    }

    public int getAge(){
        return this.age;
    }

    public static int getStaticCount(){
        return Child.counter;
    }

    public static void main(String args[]){

        Father father = new Father();
        Father child = new Child();
        Child realChild = new Child();

        System.out.println("Expecting Father Class details to be printed");
        System.out.println("SOP 1 : Father Age : "+father.age);     //prints 50 as expected.
        System.out.println("SOP 2 : Father Age : "+father.getAge());//prints 50 as expected.

        System.out.println("Expecting Child Class details to be printed");
        /*
         * Why inheritance does not work in case of direct integer access.
         */
        System.out.println("SOP 3 : Child Age : "+child.age); //prints 50 ?? , Father Age . Why ?
        System.out.println("SOP 4 : Child Age : "+child.getAge());//prints 25 as expected.

        System.out.println("Expecting Child Class details to be printed");
        System.out.println("SOP 5 : Child Age : "+realChild.age); //prints 25 as expected.
        System.out.println("SOP 6 : Child Age : "+realChild.getAge());//prints 25 as expected.

        /*
         *Total No of static Count : proper way of accessing static field using Class Name. 
         */
        System.out.println("SOP 7 : Father Instance Count : Using Class Reference :"+Father.counter);
        System.out.println("SOP 8 : Father Instance Count : Using Class Reference :"+Father.getStaticCount());

        /*
         * Incorrect Way to use static. Since Compiler allows this lets see output.
         */

        System.out.println("SOP 9 : Father Instance Count : Using Object Reference :"+father.counter); //prints 3 as expected.
        System.out.println("SOP 10 : Father Instance Count : Using Object Reference :"+father.getStaticCount());//prints 3 as expected.

        /*
         *Total No of static Count : proper way of accessing static field using Class Name.  
         */
        System.out.println("SOP 11 : Child Instance Count : Using Class Reference :"+Child.counter); // output is 2 as expected
        System.out.println("SOP 12 : Child Instance Count : Using Class Reference :"+Child.getStaticCount()); // output is 2 as expected

        /*
         * Incorrect Way to use static.Since Compiler allows this lets see output.
         * This invokes function of parent class. Why ? Inheritance does not work for static fields.
         */
        System.out.println("SOP 13 : child Instance Count : Using Object Reference :"+child.counter); // output is 3 but expected is 2 .          why ? 
        System.out.println("SOP 14 : child Instance Count : Using Object Reference :"+child.getStaticCount()); // output is 3 but expected is 2 .  why ?

        /*
         * Incorrect Way to use static.Since Compiler allows this lets see output.
         * This invokes function of parent class. Why ?
         */
        System.out.println("SOP 15 : child Instance Count : Using Object Reference :"+realChild.counter); // output is 2 as expected
        System.out.println("SOP 16 : child Instance Count : Using Object Reference :"+realChild.getStaticCount()); // output is 2 as expected
    }

}

My question is why inheritance only works for instance methods. Why output is different for SOP 3, SOP 13 and SOP 14.

  • 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-11T11:36:53+00:00Added an answer on June 11, 2026 at 11:36 am

    The reason is that polymorphism does not apply to instance variables. What you are doing is variable shadowing.

    In SOP 3 and 4 the variable with the same name in the child class shadows the variable in the base class. And since this is resolved at compile time the value of the static type is chosen.

    In SOP 13 and SOP 14 this happens for the same reason. At the scope of the method the child’s class hiding variable is unknown.

    From JSL:

    The scope of a declaration of a member m declared in or inherited by a
    class type C (§8.1.6) is the entire body of C, including any nested
    type declarations.

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

Sidebar

Related Questions

I've been doing object-oriented programming for most of the past 7 years, using Java
My first serious language was Java, so I have comprehended object-oriented programming in sense
I'm planning on learning Java, I have experience programming in other Object Oriented languages,
I know HTML is not a programming language and so applying Object Oriented principles
I just recently started with object oriented programming, using java. Before I was programming
Is there any low Level Programming language available based on java syntax and features?
I'm new at object oriented programming and we are studying on a Java project.
I've learned programming from Java, then tried to learn one programming language per year,
This past semester I took intro to object oriented programming in java and next
I am trying to learn object oriented programming in the context of Java. I

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.