Hi ,I have super class vehicle,truck extends vehicle,and I created array in test class full with objects from vehicle and truck,how can I call method from vehicle,is this the way
(truck)object1[1].getMethod()

Dani AI

Generated

A couple of clarifications and safe options that build on what and said.

Because your array is declared as Vehicle[], each element has the compile-time type Vehicle. That allows calling methods declared on Vehicle (your print() worked) but not Truck-only methods like getLoadCap() — the compiler won’t let you call them unless you tell it the element is a Truck at runtime.

Safer runtime approach (works on older Java and newer):

for (int i = 0; i < data.length; i++) {
    if (data[i] instanceof Truck) {
        Truck truck = (Truck) data[i];
        System.out.println("Load cap: " + truck.getLoadCap());
    }
}

If you can use modern Java, pattern-matching for instanceof avoids the explicit cast:

for (Vehicle v : data) {
    if (v instanceof Truck t) {
        System.out.println(t.getLoadCap());
    }
}

(pattern matching requires a recent JDK — check your Java version.)

Design alternatives to reduce casting

  • If every Vehicle should expose capacity, put getLoadCap() on Vehicle (or make it abstract).
  • Or define an interface (e.g., Loadable with getLoadCap()), have Truck implement it, and operate against that interface or check instanceof Loadable before calling. That makes intent clearer and is easier to test.

Cautions

  • Don’t rely on catching ClassCastException for normal flow; use instanceof checks.
  • If you frequently need truck-specific behavior, consider storing trucks in a Truck[] or List<Truck> rather than a mixed Vehicle[].

Summary: casting is fine when necessary, but prefer interface/abstraction or typed collections to make code safer and clearer.

Recommended Answers

All 3 Replies

Hi ,I have super class vehicle,truck extends vehicle,and I created array in test class full with objects from vehicle and truck,how can I call method from vehicle,is this the way
(truck)object1[1].getMethod()

If you want to use the method from the vehicle class then cast the object as a vehicle:

((vehicle)object1[1]).getMethod();

Notice the extra set of parentheses there; without them you'll get a compile error, as Java will believe that you want to run getMethod() and then convert the result into a vehicle.

Thanks
vehicle is the super class,and truck is the child
getLoadCap is in truck class

public static void main(String[] args) {
		
		Vehicle data[]=new Vehicle[4];
		Vehicle vehObj=new Vehicle("Lamborgini",8);
		data[0]=vehObj;
		Vehicle vehObj1=new Vehicle();
		data[1]=vehObj1;
		Truck trObj=new Truck("Ford",4,1111,111);
		data[2]=trObj;
		Truck trObj1=new Truck();
		data[3]=trObj1;
		for(int i=0;i<4;i++){
			data[i].print();
		}
        data[2].getLoadCap();//here is the problem
       
	}
data[2].getLoadCap();

should be

((Truck)data[2]).getLoadCap();

as already explained.

And of course ideally you'd handle errors doing the casting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.