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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:57:34+00:00 2026-06-14T15:57:34+00:00

The output produced from this code is not making sense, perhaps due to my

  • 0

The output produced from this code is not making sense, perhaps due to my lack of understanding. Correct me if I am wrong

import java.util.*;
class DemoA
{
  public DemoA(){
    System.out.println("DemoA object created");
  }
  public String methodA() {
    return "methodA in DemoA";
  }
}
class DemoB extends DemoA
{
  public DemoB(){
    super();
    System.out.println("DemoB object created");
  }
  public String methodA() {
   return "methodA in subclass (DemoB)";
  }
}
public class ExamQ1b
{
  public static void main(String[] args) {
    ArrayList<DemoA> aList = new ArrayList<DemoA>();
    aList.add(new DemoA());
    aList.add(new DemoB());
    for (DemoA obj: aList)
      System.out.println(obj.methodA());
  }
}

The output is

DemoA object created
DemoA object created
DemoB object created
methodA in DemoA
methodA in subclass (DemoB)

At first I didn’t understand how the output came about, but then i used the debugging feature and found why it is behaving such a way and discovered something rather confusing (not amazing).

Why is that these lines of code are producing the output?

aList.add(new DemoA());
    aList.add(new DemoB());

the output from the above lines are these, but in my thinking these should just add to the list not produce any output, what am I missing here?

DemoA object created
DemoA object created
DemoB object created
  • 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-14T15:57:35+00:00Added an answer on June 14, 2026 at 3:57 pm

    In DemoBs constructor,

      public DemoB(){
        super();
        System.out.println("DemoB object created");
      }
    

    you call super() which calls the constructor for DemoA. Since both constructors have println statements, these two lines will get printed when creating a DemoB object.

    DemoA object created

    DemoB object created

    These lines are printed when you create the object because System.out.println("DemoB object created"); is inside the constructor method that is called when you create the object.

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

Sidebar

Related Questions

The following code produces wrong and inconsistent output with gcc (4.1.2 20080704) but correct
any idea how i can get the code below to produce this output? 1
This code produces a different output in Python 2 and Python 3 . class
Why does this code produce the same output for both method calls? I would
Follows on from this question Algorithm to generate (not quite) spanning set in Python
This line of code does not produce the result I am expecting. NSString *tmpstoryTitle2
Would it be possible to hide the following output produced by Jetty? 2012-04-08 10:52:35.467:INFO:oejs.Server:jetty-8.y.z-SNAPSHOT
Here is the sample code, which produces interesting output: > gg<-data.frame(x=c(a,b),y=as.integer(c(1000,100000))) > gg x
This is an Example from MSDN about Object Class in .NET FrameWork. using namespace
I'm having problems with the redirected output from a console application being presented in

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.