Results 1 to 3 of 3
  1. #1
    blug is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default Inheritance confusion

    Hello, I am currently a bit stuck on a problem with inheritance.

    Basically I have 5 classes...

    Bike that inherits Vehicle
    Car that inherits Vehicle
    Commercial that inherits Vehicle

    Now as you may have guessed, Bike/Car/Commercial were created because they have a few extra methods/instance variables that vehicle does not.

    Within the class Registration, I make an array of type Vehicle. Within this array, I put in different kinds of types that inherit Vehicle such as car, bike, and commercial. If I want to call methods from things within the array however, I can only access the methods from the base class Vehicle (Which makes sense to me as it's an abstract concept).

    What is the best way around this?

    Last edited by blug; 11-02-2012 at 07:18 AM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Rep Power

    Default Re: Inheritance confusion

    Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Read the section "The Type Comparison Operator instanceof" and cast the vehicle to the subclass type

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Inheritance confusion

    Either make class Vehicle abstract and define abstract methods in it that are implemented in the Bike, Car etc. classes, or make Vehicle an interface that is implemented by the other classes.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Object Confusion
    By Huskies in forum New To Java
    Replies: 13
    Last Post: 07-29-2011, 09:54 AM
  2. TimeZone confusion
    By justc1 in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2011, 07:31 AM
  3. Please help clear up confusion
    By jim01 in forum New To Java
    Replies: 8
    Last Post: 04-17-2011, 08:55 PM
  4. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 03:42 PM
  5. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 01:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts