Reputation: 1
I have a class Vehicule
and 3 subclasses Voiture
, Camion
and Moto
.
I also have a method getVehicules
for the all vehicules (i.e. camion, voiture, moto) but I want to print
a specific type like Voiture.
I don't know how to solve it .
List<Vehicule> vehicules = new ArrayList<>();
public void getVehicules() {
vehicules.forEach(vehicule->{
System.out.println(vehicule);
});
}
public void getVoitures() {
Iterator iterator = vehicules.iterator();
while(iterator.hasNext()) {
//I want to do a condtion if vehicule is voiture it will disaplay
if(vehicules.equals(voiture)) {
System.out.println(iterator);
}
}
}
Upvotes: 0
Views: 148
Reputation: 339372
switch
A new feature being previewed in Java 20 enables using a switch
statement to test for subclasses. See: JEP 433: Pattern Matching for switch (Fourth Preview).
switch ( véhicule )
{
case Voiture v -> System.out.println( "Voici une voiture." );
case Camion c -> System.out.println( "Voici un camion." );
case Moto m -> System.out.println( "Voici une moto." );
default -> System.out.println( "Ooops! Il n'y a pas de véhicule." );
}
Wrap that in a for-each loop. Here is a complete example .java
file.
package work.basil.example.subs;
import java.util.List;
public class App
{
public static void main ( String[] args )
{
List < Véhicule > véhicules = List.of( new Voiture() , new Camion() , new Moto() );
for ( Véhicule véhicule : véhicules )
{
switch ( véhicule )
{
case Voiture v -> System.out.println( "Voici une voiture." );
case Camion c -> System.out.println( "Voici un camion." );
case Moto m -> System.out.println( "Voici une moto." );
default -> System.out.println( "Ooops! Il n'y a pas de véhicule." );
}
}
}
}
class Véhicule { }
class Voiture extends Véhicule { }
class Camion extends Véhicule { }
class Moto extends Véhicule { }
When run:
Voici une voiture.
Voici un camion.
Voici une moto.
default
caseNotice the default
at the bottom of the switch
to catch any other subclasses that may defined at the end.
Another new feature delivered in Java 17 can eliminate the need for that default
, if we choose to disallow further direct subclasses. The new feature is sealed classes. See JEP 409: Sealed Classes. This feature defines a way to declare a list of known subclasses, and to declare that no more will be allowed. Therefore the compiler knows at compile-time all possible subclasses. The compiler can then check that your switch
statement has covered all the possibilities.
sealed class Véhicule permits Voiture, Camion, Moto { }
final class Voiture extends Véhicule { }
final class Camion extends Véhicule { }
final class Moto extends Véhicule { }
Upvotes: 1
Reputation: 719239
How to compare two data type in Java You can get the type (a
java.lang.Class
) of an object usingobject.getClass()
.
You can compare two classes using equals(...)
; e.g.
if (obj1.getClass().equals(obj2.getClass())
You can test if an object has a specific class in a couple of ways
if (obj.getClass().equals(Car.class))
or
if (obj instanceof Car)
(They do slightly different things. The latter tests if obj
is a Car
or a subclass of Car
and will return false
if obj
is null
.)
There are methods on the Class
class that will give the classes simple name or its full name ... or you can just use the Class.toString()
method.
Just for the record, another way to solve the problem that doesn't involve using the class names would be to define a second (abstract) method in the superclass that returns a String that denotes the kind of the vehicle; e.g.
public abstract class Vehicle {
...
public abstract String getType();
}
public class Car extends Vehicle {
@Override
public String getType() {
return "Car";
}
}
Upvotes: 0
Reputation: 197
Check this out, instanceof would help here :-
class Vehicule {
}
class Voiture extends Vehicule {
@Override
public String toString() {
return "Voiture";
}
}
class Camion extends Vehicule {
@Override
public String toString() {
return "Camion";
}
}
class Moto extends Vehicule {
@Override
public String toString() {
return "Moto";
}
}
public class Operation {
static List<Vehicule> vehicules = new ArrayList<>();
public static void main(String[] args) {
vehicules.add(new Camion());
vehicules.add(new Moto());
vehicules.add(new Voiture());
//Prints all objects
getVehicules();
//Prints only voiture
getVoitures();
}
public static void getVehicules() {
vehicules.forEach(vehicule -> {
System.out.println(vehicule);
});
}
public static void getVoitures() {
Iterator iterator = vehicules.iterator();
while (iterator.hasNext()) {
Object vehicule = iterator.next();
if (vehicule instanceof Voiture) {
System.out.println(vehicule);
}
}
}
}
Upvotes: 0