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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:41:55+00:00 2026-06-18T12:41:55+00:00

For starters, I hope I have worded the question well enough. If not, please

  • 0

For starters, I hope I have worded the question well enough. If not, please feel free to edit it or suggest changes.
I’m working through a Java textbook to help with my university course and I came across an exercise asking me to create an abstract class Airplane, with three subclasses, B747, B757 and B767. Each object of those three subclasses has a unique serial number.

Cutting out a lot of the other parts of the question (which I hope aren’t relevant!), I did this:

public abstract class Airplane {
    String serialNumber;

    public String toString() {
    return getClass().getName() + ": " + serialNumber
    }
}

I then declared three subclasses, each of which looked like this:

public class B747 extends Airplane {
    B747(String serial) {
    serialNumber = serial;
    }
}

Finally, in my main program, I instantiated new objects using the following code:

Airplane a = new B747("ABC101");
System.out.println ("Airplane a: " + a);

Now, the code works fine. However, when I looked at the answers in the book, they chose to do it a different way. They instantiated each object in the same way, but instead in each class had the following method:

public B747 (String serial) {
    super (serial);
}

In the abstract superclass Airplane, they then had:

public Airplane (String serial) {
    this.serial = serial;
}

Was this just a matter of personal preference, to elevate the String to the superclass and act on it there, or does this provide any benefits, whether security or otherwise?

  • 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-18T12:41:56+00:00Added an answer on June 18, 2026 at 12:41 pm

    Was this just a matter of personal preference, to elevate the String to the superclass and act on it there, or does this provide any benefits, whether security or otherwise?

    It’s only changing where the variable is accessed – not where it’s declared. And it’s a good thing:

    • Every Aeroplane should have a serial number, so it makes sense for the Aeroplane class to enforce that
    • It allows the field to be private (and final) within Aeroplane, with a “getter” to provide read-only access to it as widely as is required

    Both parts are important – but if you’re not sure about the first aspect, consider what would happen if you had another few subclasses. Do you really want the assignment to occur in every single subclass? There will always be a superconstructor call – whether implicit or explicit – but there’s no reason to repeat the assignment.

    I definitely prefer the book’s solution to your one – particularly if they make the field private and final. In my code, I hardly ever use non-private fields, other than for constants.

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

Sidebar

Related Questions

For starters, this question is not so much about programming in the NetBeans IDE
Ok, a few things I'm not quite sure about. For starters: I have a
For starters this is a homework question. I have an idea but I am
i starter in jqgrid, i want implement inline edit in jqgrid i have this
For starters I have whored myself out to the Internet in general as far
For starters: I'm working with Flash CS3 and Actionscript 2.0 I'm trying to remake
I do not write in C often and am having some trouble. For starters
I have a form I'm working with that allows the user to continually add
This is my first question ever at stackoverflow, hope I make sense out of
For starters, I am not a programmer, my wife is making me learn how

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.