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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:28:39+00:00 2026-06-17T09:28:39+00:00

So this code has an error because the base class has parameters right? Because

  • 0

So this code has an error because the base class has parameters right? Because the default constructor of each subclass calls the default constructor of the base class? and we don’t have a default constructor for the base class which causes the error?

Did I misunderstand it, also what is the best way around this while implementing the principle of code-reuse because I’m trying to practice programming in OOP

class A {
    int item;

    A(int item) {
        this.item = item;
    }
}

class B extends A {
    int subitem;

    B(int item) {
        super(item);
        subitem = item * 2;
    }
}

class C extends A {

}
  • 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-17T09:28:39+00:00Added an answer on June 17, 2026 at 9:28 am

    Firstly, on the title of this question:

    Subclasses inherting base class constructor while maintaining principle of code-reuse

    Constructors are never inherited. It just doesn’t happen. What you’ve been demonstrating is not inheritance – it’s just the requirement that any constructor (other than the one in java.lang.Object) has to either chain to another constructor in the same class, or chain to a constructor in the superclass.

    So this code has an error because the base class has parameters right?

    Well, to be precisely, it’s because the base class has no parameterless constructor.

    Because the default constructor of each subclass calls the default constructor of the base class?

    A default constructor is only provided if you don’t specify any constructors explicitly. The default constructor always calls a parameterless constructor in the base class, implicitly.

    and we don’t have a default constructor for the base class which causes the error?

    We don’t have a parameterless constructor for the base class. Basically it’s equivalent to writing:

    class C extends A {
        C() {
            super();
        }
    }
    

    If you understand why that doesn’t compile, you understand why the version without any explicitly-declared constructors doesn’t compile, as they’re equivalent.

    Did I misunderstand it, also what is the best way around this

    Around what? You can’t create an instance of A without providing an item. It would be bad if it did compile – what would the item be? Every instance of C can also be considered to be an A, which requires an item…

    You could write a parameterless constructor which provides some sort of default to the superclass constructor:

    public C() {
        super("default item");
    }
    

    if that’s what you want. But beyond that, we can’t really suggest alternatives without knowing what you’re trying to achieve.

    Note that inheritance is far from the only way of achieving code reuse – and in fact I personally prefer reuse via composition instead of inheritance in general. Inheritance is powerful, but overused IMO.

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

Sidebar

Related Questions

this code produces 17 error C2995: function template has already been defined; there was
I'm getting a javascript error. It has to do with this section of code:
All my controller has this code on the top of class. public class TestController
Consider this piece of code: #include <vector> #include <iostream> using namespace std; class Base
This code has an interesting bug: some_struct struct_array1[10] = {0}; some_struct struct_array2[10] = {0}
This code has generated a 1 result for r on the 1st toss 200
I translated this code(it has bad side effect that it just capture the outer
I have this code that has one button that let's me choose an entry
I was checking out this question which has this code - (NSArray *) percentagesRGBArray:(float[])
I'm using a tutorial plugin - http://particlebits.com/code/jquery-tutorial/ which has this code attached to the

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.