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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:37:48+00:00 2026-06-11T07:37:48+00:00

     I have created a basic inheritance program using a polymorphic array. From the parent-class,

  • 0

     I have created a basic inheritance program using a polymorphic array. From the parent-class, this array is looped through and each object (created from the child-class) at each index executes the parent-class’ instance method.

     As an experiment, I created an object within the child-class’ constructor of its’ parent-class type, and executed the parent-class’ instance method from there.

     For reasons unknown to me, this is causing the instance method (executed from the child-class’ constructor) to execute the number of times as the length of the parent-class’ polymorphic array (if the polymorphic array has 5 elements, the child-class’ method call will be executed 5 times).


Here is the parent-class:

public class MyClass
{
    // instance variables
    protected String name;
    protected String numStrings;

    // constructor
    public MyClass(String name)
    {
        this.name = name;
    }

    // instance method
    public void getDescription()
    {
        System.out.println("The " + name + " has " + numStrings + " strings.");
    }

    // main method
    public static void main(String[] args)
    {
        MyClass[] instruments = new MyClass[2];

        instruments[0] = new Child("Ibanez bass guitar");
        instruments[1] = new Child("Warwick fretless bass guitar");

        for(int i = 0, len = instruments.length; i < len; i++)
        {
            instruments[i].getDescription();
        }
    } // end of main method
} // end of class MyClass

…here is the child-class:

public class Child extends MyClass
{
    // constructor
    public Child(String name)
    {
        super(name); // calling the parent-class' constructor
        super.numStrings = "four";

        MyClass obj = new MyClass("asdf");
        obj.getDescription();
    }
} // end of class Child

…and here is the output:

The asdf has null strings.
The asdf has null strings.
The Ibanez bass guitar has four strings.
The Warwick fretless bass guitar has four strings.

  • 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-11T07:37:50+00:00Added an answer on June 11, 2026 at 7:37 am

    The problematic line is this:

    MyClass obj = new MyClass("asdf");
    

    if you just simply call getDescription() instead of obj.getDescription(), it should be OK. Since ‘Child’ extends ‘MyClass’ the super constructor call is for initializing everything in the super class (let’s just say you can imagine it for now as an implicit new MyClass("...")) you don’t have to instantiate ‘MyClass’ explicitly.

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

Sidebar

Related Questions

I have my XML feed being created from an associative array. Using new DOMDocument('1.0','UTF-8');
I have the following code (inherited from someone): <div class=feedback>&nbsp;</div> <form onsubmit=return false autocomplete=off>
I have six columns of output. The content is grabbed from an array (created
I have created a django application. Using this app users can be created or
I have a Visual Studio 2005/ C# ClickOnce application that gets all its data from a
I have a Qt application in Visual Studio 2005 which is linked using \subsystem:windows such that
I have this function: function validate($data) { $newData = str_replace(&nbsp;, , $newData); $newData =
I have a sentence like this. 1 &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; 3
First, I would have created this example in JSFiddle but they are in read-only
Hi (I'm pretty new to this), I have created a portal where the user

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.