Skip to content
Advertisement

Tag: subclass

How to cast an object of an ArrayList of objects (of type superclass) to an object of type subclass

If I have a superclass, let’s call it Car, with the constructor parameters String name, String color, double wheelSize, and a subclass of this, let’s call it Truck, with the constructor parameters String name, String color, double wheelSize, and double truckBedArea, and in the subclass (Truck), I have a method called modifyCar with the paramaters Car car, String newName, String

Behavior of costructors in sub classes in Java

let’s say I have the following code: What are the outputs when I try to execute I thought it would first call the constructor of B and the constructor of B will call the constructor of A but the I don’t know which between “creating A” or “Hi” message will be shown first. The first thing invoked on a class

Java: how to have an array of subclass types?

Say I have a super class “Animal” and subclasses “Cat”, Dog, Bird”. Is there a way to have an array of subclass type rather than class instances with which I’ll be able to instantiate instances of each possible subclass? To simplify, I want this: How can I do that? Edit: I don’t want an array of instances of these subclasses,

Advertisement