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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:18:56+00:00 2026-06-04T00:18:56+00:00

Why does the execution of TestClass.main() outputs 202 202 101?? class BaseClass { int

  • 0

Why does the execution of TestClass.main() outputs 202 202 101??

class BaseClass
{
    int data = 101;
    public void print()
    {
        System.out.print(data + " ");
    }
    public void fun()
    {
        print();
    }
}
class SubClass extends BaseClass
{
    int data = 202;
    public void print()
    {
        System.out.print(data + " ");
    }
}
class TestClass
{
    public static void main(String[] args)
    {
        BaseClass obj = new SubClass();
        obj.print();
        obj.fun();
        System.out.print(obj.data);
    }
}

With my poor OOP knowledge I think the execution must be this way:

1- obj.print(); prints 202 from SubClass

2- Since there is no obj.fun(); method in Subclass it calls parent method so the output should be 101

3- System.out.print(obj.data); should print 202 since the value is overridden in subclass.

So I think the output would be 202 101 202 but it isn’t, can you explain me why?

  • 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-04T00:18:57+00:00Added an answer on June 4, 2026 at 12:18 am

    Since there is no obj.fun(); method in Subclass it calls parent method so the output should be 101

    Indeed, it calls the super class for fun, but the super class calls print, and as print is overridden, the overriding version (in the subclass) gets called.

    System.out.print(obj.data); should print 202 since the value is overwritten in subclass.

    Variables are not overriden, but hidden by subclass, as obj declared as BaseClass, it accesses it data property directly. unlike method call, this is decided at compile time.

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

Sidebar

Related Questions

Why does the following test fail? [TestClass] public class DynamicTests { public class ListOfIntsTotaller
[TestClass] public class FooTests { [TestMethod] public void TestFoo() { var fooMock = MockRepository.GenerateMock<IFoo>();
Given the following code, [DataContract] public class TestClass { [DataMember] public object _TestVariable; public
I have a windows service that generates logs as it does some execution. I
Does the Grand Central Dispatch API allow an execution context (thread) to query any
Does java has library exception class which means actually not an error, but good
Why does the execution continue after redirection using header() ? $flag=1; if($flag==1) header("Location:page1.php"); header("Location:page2.php");
import java.util.*; import java.io.*; import java.util.regex.*; class ZiggyTest2 extends Thread{ String sa; public ZiggyTest2(String
In GMP library.... how does internal execution of operations on integers ll be done??
I have this script import unittest,itertools,random ##testclass class Testcomb(unittest.TestCase): def test_input(self): self.assertRaises(TypeError,calculate_combinations,dict(comb1), 5) def

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.