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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:33:03+00:00 2026-06-12T17:33:03+00:00

I’m working with some code that needs to send either a superclass or subclass

  • 0

I’m working with some code that needs to send either a superclass or subclass object to a method.

The method public void repair(Vehicle vehicle) will ONLY access methods in the super class object.

public class Vehicle {
    //class stuff
}

public class Car extends Vehicle {
    //class stuff
}

public static void main(String[] args) 
{
    // do stuff to determine whether working with a vehicle or car
    if (someCondition)
    {
        Car = new Car();
        // do some stuff...
        repair(Car);
    }
    else
    {
        Vehicle = new Vehicle();
        // do some stuff...
        repair(Vehicle);
    }
}   

I figure I have three options:

  1. Leave the code as it is, it seems to be working. – I don’t like this option, it feels like I’m making assumptions and I suspect car only methods could be accidentally called doing this, leading to unexpected behaviour.
  2. Create a getVehicle() method in car, to return a Vehicle. Then use repair(Car.getVehicle()); – this feels a little better
  3. Change Car = new Car(); to Vehicle = new Car(); which I believe would create an object (vehicle) that can only perform methods of type vehicle. – This feels the safest, as I’m now restricting what can be done, to prevent unexpected behaviour.

Is 3, the best approach, given that the repair method is only ever expecting vehicles?

Also, is there anything I could/should to to the: public void repair(Vehicle vehicle) method declaration?

EDIT: It seems I should be using:

Leave the code as it is

since the repair() method casts the subclass object to a superclass object anyway.

  • 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-12T17:33:04+00:00Added an answer on June 12, 2026 at 5:33 pm

    There is no definition of repair but I think you want something like this

    public abstract class Vehicle {
        //class stuff
    }
    
    public class Car extends Vehicle {
       //class stuff
    }
    
    
    public class Garage {
       public void repair(Vehicle vehicle){
       ....
       }
    }
    

    Then you can pass any subclass of Vehicle to the repair method. In this case it is only Car but you could extend to have bike, motorcycle etc.

    Now you will not need to check with an if statement. You can just pass your object (or Car or anything else) into the repair method.

    You main just becomes

    public static void main(String[] args)  {
        Garage g = new Garage();
        Vehicle c = new Car();
        Vehicle b = new Bike(); //assuming Bike is a subclass of Vehicle.
        g.repair(c);
        g.repair(b);
    }  
    

    If when accessing variable b and c you need Car and Bike specific method then you can change their declarations to be

    Car c = new Car();
    Bike b = new Bike();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I know there's a lot of other questions out there that deal with this

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.