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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:35:15+00:00 2026-05-23T07:35:15+00:00

I have a super class Vehicle and three classes that extend on it: Bus

  • 0

I have a super class Vehicle and three classes that extend on it: Bus, Car and Truck. I want to have a linked list that will contain vehicles of different types, I use

list = new LinkedList<Vehicle>()

and it seems to work when I use System.out.println(list.get(2)), but I don’t understand why? I’ve added as an experiment toString() function to Vehicle class which is deferent, but it still uses the extended classe’s toString(). When will it use the father’s function, and when it’s sons?

All the different classes have the same functions, but different private variables.

the classes are:

public class Vehicle {

    protected String maker;
    protected int year;
    private int fuel; //0 1 2 3 4

    public Vehicle (String maker, int year) {
        this.maker = maker;
        this.year = year;
        this.fuel = 0;
    }

    public void drive () {...}

    public void fill () {...}
}

Bus:

public class Bus extends Vehicle{

    private int sits;

    public Bus (String maker, int year, int sits) {
        super (maker, year);
        this.sits = sits;
    }

    public String toString () {...}
}

Truck:

public class Truck extends Vehicle{

    private int weight;

    public Truck (String maker, int year, int weight) {
        super (maker, year);
        this.weight = weight;
    }

    public String toString () {...}
}

Car:

public class Car extends Vehicle{

    private float volume;

    public Car (String maker, int year, float volume) {
        super (maker, year);
        this.volume = volume;
    }

    public String toString () {...}
}
  • 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-23T07:35:16+00:00Added an answer on May 23, 2026 at 7:35 am

    Ofcourse you can make a List that contains Vehicle objects:

    List<Vehicle> list = new LinkedList<Vehicle>();
    

    When you do System.out.println(list.get(2)); then it will get the Vehicle object at index 2 and call toString() on it, and then this string is printed to the console (you can ofcourse easily try that out yourself).

    Note that if you want to call a method that is specific to a Bus, Truck or Car (i.e., defined in one of those classes and not in a superclass), then there is no way to do that without casting the result of list.get(...).

    You can call any method that’s declared in class Vehicle (or superclasses – toString() is declared in class Object) without casting, and the method appropriate for the specific object (Bus, Truck or Car) will be called – that’s what polymorphism is all about.

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

Sidebar

Related Questions

Let's say I have a table that represents a super class, students . And
I have a base class Vehicle and a derived class Car. package Game {
I have a super-class named ClassA and two sub-classes Class1 and Class2. I have
I have a generic class that is also a mapped super class that has
I have a abstract super class called RealAlgebraicNumber and two inherited classes called IntervalRepresentation
I have this set of classes: Node (SUPER Class) |------ NodeType1 (Class) |----------NodeType2 (Class)
i have a java class used as an entity that has 2 classes that
If I have a Class A that contain a List of children with class
I have a super class 'Node' and several sub classes e.g. 'SubNode1', 'SubNode2' etc.
I have set of classes which inherit from a single super class: Super |

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.