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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:42:45+00:00 2026-06-04T01:42:45+00:00

class A{ int a=10; public void show(){ System.out.println(Show A: +a); } } class B

  • 0
class A{
int a=10;   
public void show(){ 
    System.out.println("Show A: "+a);
    }
}

class B extends A{
public int b=20;    
public void show(){
    System.out.println("Show B: "+b);
    }
}



public class DynamicMethodDispatch {

    public static void main(String[] args) {

    A aObj = new A();       
    aObj.show();    //output - 10

    B bObj = new B();
    bObj.show();   //output - 20

    aObj = bObj;   //assigning the B obj to A..         
    aObj.show();  //output - 20 

    aObj = new B();
    aObj.show();  //output - 20
             System.out.println(bObj.b);  //output - 20 
    //System.out.println(aObj.b); //It is giving error



     }
}

In the above program i’m getting Error wen i try invoking aObj.b.
1.why i’m not able to acess that variable through the aObj though it is refering to class B??
2. why i’m able to acess the method show()?

  • 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-04T01:42:47+00:00Added an answer on June 4, 2026 at 1:42 am

    You have to distinguish between the static type of aObj and the runtime type of aObj.

    Code such as

    A aObj = new B(); 
    

    results in an aObj variable with static type A and runtime type B.

    The compiler will only bother too look at the static type when deciding what to allow or not.

    To your questions:

    1.why i’m not able to acess that variable through the aObj though it is refering to class B??

    Because there is (in general) no way for the compiler to know that aObj will refer to a B object at runtime, only that it will refer to some form of A object. Since .b is not available on all A objects, so the compiler will think “better safe than sorry” and disallow it.

    2.why i’m able to acess the method show()?

    Because this method is available in all A objects (if it’s not declared in the subclass, it is still inherited from A).

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

Sidebar

Related Questions

class B { int x,y; int z; z=x*y; void show() { System.out.println(z); } }
I've a code: class Parent{ int x=10; void show(){ System.out.println(x); } } class Child
class A{ A(int x){} } class B extends A{ B(int x){} public static void
class MyClass { public: void method2() { static int i; ... } }; Will
class Foo { public: void bar(); }; void Foo::bar() { static int n =
class classe (){ public: int key; static void *funct(void *context){ printf(Output: %d, , key);
public class TowThreads { public static class FirstThread extends Thread { public void run()
class CRectangle { int x, y; public: void set_values (int,int); int area (void); }
public class SendImage { public delegate int DWatch(int bytesLeftToSend, IntPtr Response); ret=0xffff; public void
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(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.