Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Can a class array contain subclass items?

$
0
0
I have one class named "Vessel". I have other 2 classes named "Ferry" & "Cargo" that extend the class "Vessel". I need to make a 4th class that has the attribute "schedule", which is an array containing every ship. Each ship must be either of type "Ferry" or "Cargo". So is this correct? Ferry schedule[] = new Ferry[]; Could the above array contain objects of type "Ferry" and "Cargo" ? If not, how can I accomplish this? Example: class Vessel { String name; int length; int year; Vessel(String n, int l, int y) { name = n; length = l; year = y; } } class Ferry extends Vessel { int passengerCount; int carCount; } class Cargo extends Vessel { int load; } class Port { String portName; Ferry schedule[] = new Ferry[]; }

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>