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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:05:18+00:00 2026-06-11T15:05:18+00:00

class A2 { int numt=111; void hello() { System.out.println(Hello from A2); } } class

  • 0
class A2 {
    int numt=111; 
  void hello() {

    System.out.println("Hello from A2");
  }
}

class B2 extends A2 {
  void hello() {
    System.out.println("Hello from B2");
  }
}

class C2 extends B2 {
  int numt=666;  
  void hello() {
    System.out.println("Hello from C2");
  }


}

class MethodOverriding2 {
  public static void main(String args[]) {
    A2 obj = new C2();
    obj.hello();
    System.out.println("Num is : " + obj.numt);
  }
}

So basically the output here is

Hello from C2
Num is : 111

Why does it run hello() from C2 but numt from A2?

As I understand, I’m inheriting A2 into B2, and C2 into B2. Then in my main I’m creating an object of class A2 which refers to C2 (which it can do since A2 is a superclass of C2).
Then in compile time there is no error as the code is satisfied.
At runtime, the program looks for ‘hello()’ where ‘obj’ is referring to and not where it is defined.
But it does the opposite for ‘numt’. Why is this so? And is my above understanding correct? Please correct if it isn’t.

Thanks a lot! I know it’s a novice question, I’m very new to OOP.

  • 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-11T15:05:19+00:00Added an answer on June 11, 2026 at 3:05 pm

    When you call method, the method in the instance will be called, not on reference type due to polymorphism. Here your object is of C2 even though reference is of type A2, so method in C2 will be invoked.

    When you access variables it is on reference type than object type because polymorphism doesn’t applicable for fields. That is why it is always good design to make your variables private so that other classes can’t directly access these variables.

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

Sidebar

Related Questions

class X { int i; public: X() { i = 0; } void set(int
class CRectangle { int x, y; public: void set_values (int,int); int area (void); }
class test{ int x; public: //public functions getx() and putx() }; void main() {
i have a class: class GetColumnsNames { public static int Occurrence_Date = Convert.ToInt16(ConfigurationSettings.AppSettings[Occurrence_Date].ToString()); public
class temp { int id; public int getId() { return id; } temp(int id)
class a { int variable;//4 bytes } class a { static int variable;//? bytes
#include <iostream> #include <vector> int main() { class Int { public: Int(int _i) :
I have a program following: class INT { public: INT(int ii = 0) :
Given the example code: class Integer { int i_; public: Integer(int i) : i_(i)
There is code: #include <iostream> class Int { public: Int() : x(0) {} Int(int

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.