Results 1 to 3 of 3
Thread: Inheritance confusion
- 11-02-2012, 07:12 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
Inheritance confusion
Hello, I am currently a bit stuck on a problem with inheritance.
Basically I have 5 classes...
Vehicle
Bike that inherits Vehicle
Car that inherits Vehicle
Commercial that inherits Vehicle
Registration
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?
Cheers!Last edited by blug; 11-02-2012 at 07:18 AM.
- 11-02-2012, 07:21 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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
- 11-02-2012, 07:21 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Object Confusion
By Huskies in forum New To JavaReplies: 13Last Post: 07-29-2011, 09:54 AM -
TimeZone confusion
By justc1 in forum Advanced JavaReplies: 1Last Post: 06-22-2011, 07:31 AM -
Please help clear up confusion
By jim01 in forum New To JavaReplies: 8Last Post: 04-17-2011, 08:55 PM -
Confusion here @@' Help!
By pleasurelyours in forum New To JavaReplies: 7Last Post: 06-09-2010, 03:42 PM -
Tic Tac Toe confusion
By jigglywiggly in forum New To JavaReplies: 15Last Post: 04-12-2009, 01:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks