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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:14:13+00:00 2026-05-26T20:14:13+00:00

Basically I’m having a little issue here. I have a superclass and a subclass.

  • 0

Basically I’m having a little issue here.

I have a superclass and a subclass. I am supposed to do an assignment where I execute a method from the subclass, which overrides the method from the superclass. This works fine. The issue I am having is with “dynamic binding.” When I declare the reference variable type as of the same class as the subclass, it works fine. However, when I declare the type to be of the superclass, it does not recognize any of the subclass’s methods to even exist.

Here’s an example. I have a class called Ship and another subclass of Ship called BabyShip

If I declare a reference of ShippyShip as:

BabyShip subref = new BabyShip();  

It works fine. However, when I declare it as:

Ship subref = new BabyShip();

The compiler doesn’t even recognize any of the methods from BabyShip if I declare the type of subref to be Ship… but my book clearly says that it should. Why is it doing this?

It gives a regular cannot find symbol error when I compile it.

ShipTester.java:8: error: cannot find symbol
            ship.setMaxPassengers(1);
                ^
  symbol:   method setMaxPassengers(int)
  location: variable ship of type Ship
1 error
  • 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-05-26T20:14:13+00:00Added an answer on May 26, 2026 at 8:14 pm

    Are you sure you’re not misreading the book? Java doesn’t do dynamic binding like this. If setMaxPassengers is only declared in BabyShip, then the compiler is doing exactly the right thing. You can only access members which are known to the compile-time type of the expression you’re accessing them through – in this case ship is of type Ship, so only the members of Ship (and its superclasses) are available.

    It’s hard to know exactly what the problem is without knowing exactly what the book says. The only sort of dynamic binding that occurs in normal Java is for overriding – if you’d declared setMaxPassengers in Ship but then overridden it in BabyShip, then the overridden method would be called at execution time.

    Java 7 has some new features when it comes to dynamic binding, but it doesn’t sound like that’s what you’re talking about here.

    If you can quote a specific bit of the book which you think implies this should work, please do so.

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

Sidebar

Related Questions

Basically I am trying to restart a service from a php web page. Here
Basically I have an iframe loaded that is accessed from the parent whenever it
Basically, I have a JTextPane to hold some text which I wish to style.
Basically in my in my aspx page I have a gridview which displays the
Basically I have some sort of third party software running on Windows 7 which
Basically I have some code to check a specific directory to see if an
Basically I'm going to go a bit broad here and ask a few questions
Basically from a database I am getting data that is formatted like this nameofproject101
Basically I need some text like: I have an ice cream cone. You are
Basically I'm wondering if it's possible to have a form on a site outside

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.