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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:17:17+00:00 2026-06-18T18:17:17+00:00

I have following sample code explaining sample Polymorphism concept – Overriding class Super {

  • 0

I have following sample code explaining sample Polymorphism concept – Overriding


    class Super
    {
        public int number = 1;
        public char superText='a';
        public String getColor()
        {
            return "red";
        }
    }

    class Sub extends Super
    {
        public int number = 2;
        public char subText='b';

        public String getColor()
        {
            return "blue";
        }
    }


    public class Sample2 
    {
        public static void main(String[] args)
        {
            Super supersub = new Sub();
            System.out.println( supersub.getColor() + supersub.number + supersub.superText );
        }



    }

The Output is blue1.

Question 1:

The Method in derived class getColor() is overridden and Field of the Super class is displayed.

Can some one explain why number field in derived class is not called ? i.e., output is blue2


Question 2: *REGARDING MEMORY ALLOCATION*

for below object instantiation,

   Sub subobj = new Sub(); 

Memory for field ‘number’ is allocated in heap and the address of the Number variable
is assigned to object reference, subobj.

consider below case,

Super supersub = new Sub(); 

(a) Here memory for variables, ‘number and subText’ in derived class ‘Sub’ is created and the address of the variables is placed in supersub Object

when i access, supersub.subText i got error that subText cannot be resolved.

SO, PLEASE EXPLAIN POINT (a) described above i.e., Memory Allocation of Derive Class variables

Thanks,
Cyborgz

  • 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-18T18:17:18+00:00Added an answer on June 18, 2026 at 6:17 pm

    Question 1:

    The Method is derived class is displayed and Field of the Super class
    is displayed.

    Can some one explain why number field in derived class is not called ?
    i.e., output is blue2

    Fields cannot be overridden. Even if two classes share a parent-child relationship, the fields belong to the class they were defined in, even if they share names with an inherited field. In other words, number in Sub is totally different field than number in Super.

    Question 2:
    Here memory for variables, ‘number and subText’ in derived class ‘Sub’ is created and the address of the variables is placed in
    supersub Object
    when i access, supersub.subText i got error that subText cannot be resolved.

    The object stored in supersub is of type Sub, but the compiler doesn’t know that.

    Because Java is a statically typed language, the compiler goes by the declared type (that is, the variable type) of the reference because, in most real-world cases, the runtime type (the one that’s apparent in the new expression) isn’t necessarily known at compile time. For example, you may have gotten this object from another method, or from two or three candidate methods, therefore the runtime type is unpredictable.

    Storing the reference in a superclass variable means that you intend to use that object as Super for a while. The compiler, then, works on this perceived intention of yours. Super isn’t guaranteed to only have instances of a runtime type of Sub, therefore it can’t make the assumptions you expect.

    That being said, storing the reference in one kind of variable or another does not modify the object. If you were to cast the object back to a variable of a type that actually knows about those members you’re trying to access (in your case, the Sub type), you’ll find that they’re still there (and they retain their values).

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

Sidebar

Related Questions

I have the following code sample: class A { public: A(int a):AA(a) {}; int
I have the following sample piece of code, public class test implements Runnable{ public
Lets say we have following sample code in C#: class BaseClass { public virtual
I have the following simple code abstract class A { public abstract void Test(Int32
I have the following sample code let x = [return 1::IO(Int), return 2::IO(Int)] So
I have a following sample code from my lecture note. int[] a = new
I have the following sample code (a stripped down version of my programme) Class
Consider the following sample code: class MyClass { public long x; public void DoWork()
I have the following code sample in VB .net 2008 Public Function CheckPathFunction(ByVal path
I have been confused about ORM since I see the following sample code: public

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.