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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:42:55+00:00 2026-06-15T18:42:55+00:00

I have a super class Sam and a sub-class SubSam public class Sam {

  • 0

I have a super class “Sam” and a sub-class “SubSam”

 public class Sam {

String msg;
String msg1;

Sam(String mm, String mm1) {
    msg = mm;
    msg1 = mm1;
}

@Override
public String toString() {
    return this.msg + "  " + this.msg1;
    }
}

class SubSam extends Sam {

String msg1="C";

public static void main(String[] args) throws Exception {
    SubSam obj = new SubSam();
    System.out.println(obj);
}

SubSam() {
    super("A", "B");
}
}

The output is:

A  B

Why “toString()” is referring the instance fields of “Sam” instead of “SubSam”. The output should be: A C

I am thinking over it for a long time now, but not getting?

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

    Because instance variables in Java aren’t overridden, quite simply. A subclass can define a variable with the same name as one defined in one of its superclasses, but it counts as a separate variable for all intents and purposes.

    For example, consider the following code:

    public class A {
        public String var;
    }
    
    public class B extends A {
        public int var;
    }
    

    Given those definitions, instances of B will have two variables, one being of type String, the other of type int, but both will be named var. They are still separate variables which can be independently assigned to and read from, and Java does not consider there being anything wrong with this.

    If you want to override behavior as you indicate you want, you need to use methods instead. For instance, you could do it like this:

    public class A {
        public String msg;
        private String msg1;
    
        public A(String mm, String mm1) {
            this.msg  = mm;
            this.msg1 = mm1;
        }
    
        public String msg1() {
            return(this.msg1);
        }
    
        @Override
        public String toString() {
            return(this.msg + " " + msg1());
        }
    }
    
    public class B extends A {
        public B() {
            super("A", "B");
        }
    
        @Override
        public String msg1() {
            return("C");
        }
    }
    

    Now, if you call System.out.println(new B());, it will print A C.

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

Sidebar

Related Questions

I have a class, Super : public class Super { public static String foo
I have a super class like this: class Parent { public: virtual void Function(int
I have this super class which extends from another class public abstract class AbstractDOEMessageFinderAction
I have this code: #include <iostream> class Super{ public: virtual void showName(); }; class
If subclass in objective-c wants to override a super class's method, does it have
Lets say I have the code: class NoneDict(dict): def __getitem__(self, name): try: return super(NoneDict,
I have a super class 'Node' and several sub classes e.g. 'SubNode1', 'SubNode2' etc.
I have a super class: public class SuperClass { public void dosomething() { firstMethod();
I have a super-class named ClassA and two sub-classes Class1 and Class2. I have
I have a super abstract class: @XmlSeeAlso({AndQuery.class, OrQuery.class, NotQuery.class, PropertyQuery.class, MultiQuery.class}) @XmlRootElement public abstract

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.