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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:41:26+00:00 2026-05-22T19:41:26+00:00

Please explain the following behaviour. class Base { public int num = 3; public

  • 0

Please explain the following behaviour.

class Base
{
    public int num = 3;

    public int getNum()
    {
        return num;
    }

    public void setNum(int num)
    {
        this.num = num;
    }
}


class child
    extends Base
{
    public int num = 4;

    public child()
    {

    }

    public child(int i)
    {
        this.num = i;
    }

    public int getNum()
    {
        return num;
    }

    public void setNum(int num)
    {
        this.num = num;
    }
}

public class Main
{

   public static void main(String[] args)
   {

        Base obj2 = new child();
        System.out.println(obj2.num);
        System.out.println(obj2.getNum());

        Base obj3 = new child(10);
        System.out.println(obj3.num);
        System.out.println(obj3.getNum());
    }
}

Output :
3
4
3
10

Here how obj2.num and obj3.num still point to the value 3 which is assigned in the Base class instance variable. Wont it get overidded like the obj2.getNum() and obj3.getNum().

Thanks in advance.

  • 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-22T19:41:27+00:00Added an answer on May 22, 2026 at 7:41 pm

    Because the objects are declared with super class type, you get the super member variable value.

    If the object was declared with sub class type, the value would be overridden value.

    If the Base obj3 = new child(10); is modified to child obj3 = new child(10); the output would be 3 4 10 10

    This is well explained here

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

Sidebar

Related Questions

Please see the following code and explain the output behavior. public class MyFinalTest {
Please explain why the following code snippet fails to compile: public class ScjpTest{ static
This is an easy way to earn some points. Please explain the following: class
Could anyone please explain the following line of code, found on http://docs.openttd.org/ai__cargo_8cpp_source.html return (AICargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
Can someone please explain what the & does in the following: class TEST {
Please explain what this task is about? Create a generic linked list class that
Could you please explain the following behavior of C# Class. I expect the classResult
Can anyone please explain why the following will compile int a = aAssignments[i]->Count; int
Can someone please explain the following How was the first JDK release unit Tested?
Can anyone please explain to me why the following two queries yield different results?

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.