Java Is it possible to extend all subclasses of a class with a single class?

Java Is it possible to extend all subclasses of a class with a single class?

To explain this with an example, the actual code is more complex. I have an Animal class with my own class hierarchy. Let's say that it has two subclasses: Testarrosa and Viper.

public class Car { public abstract String getManufacturer(); } public class Testarossa extends Car{ public String getManufacturer(){ return "Ferrari"; } } public class Viper extends Car{ public String getManufacturer(){ return "Dodge"; } } 

I want to extend all subclasses of Car with a subclass of RegisteredCar.

 public class RegisteredCar extends Car { private String plateNumber; public RegisteredCar (String plateNumber){ this.plateNumber=plateNumber; } public String getPlateNumber() { return plateNumber; } } 

At some point, I should be able to create a new RegisteredCar for a specific subclass. Sort of

 RegisteredCar c = new RegisteredCar<Viper>("B-3956-AC"); 

And call c.getManufacturer () to get "Dodge" and c.getPlateNumber () to get B-3956-AC. Obviously, I would still have to create Car c = new Viper();

This is an example. Having an attribute in Car with a null value, if it is not registered, is not enough for what I need.

+6
source share
8 answers

As others said, no, that’s really impossible, and your example could be solved by changing your model.

As an alternative to inheritance, you can use another class to transfer the Car instance.
I would make a Car interface (although working with RegisteredCar extend Car should also work), and then try to do something like the following pseudocode:

 class RegisteredCar<T extends Car> implements Car { private final T car RegisteredCar(T car) { this.car = car; } ... methods for RegisteredCar ... methods from Car delegating to `this.car` } 

Please excuse some bad code, I do not have an open IDE, and I always use generics without using an IDE.

Another possible solution is to use AOP, although I don’t know how it is in fashion, which these days, but what you are describing may be a problem with cross-references.

The ultimate alternative could be to use a language that allows the use of extensions, traits, protocol, or some other type of "mix in"

+4
source

In short, this is not possible. You must, unfortunately, change your object model.

For example, for the Registration class as follows:

 public interface Registration<C extends Car> { C getCar(); String getPlateNumber(); } 

In this way, you can retrieve information related to registration in one class while maintaining your Car models.

Then you can use helper methods, for example:

 Registration<Viper> registeredViper = createRegistration(new Viper(), "B-3956-AC"); 
+10
source

In java it is forbidden to distribute more than 1 class. For example, you can build a chain of classes to extend. To solve the problem of mutual inheritance in Java → an interface is used

+2
source

You should avoid inheritance as much as possible. Use abstractions (interfaces) to make your code elegant and maintainable . Just google why renewal is evil.

  public interface Car{ String getManufacturer(); } public interface Registerable{ boolean isRegistered(); void register(String plateNumber); void getPlateNumber(); } public class Viper implements Car, Registerable { //all methods } 
+2
source

No, this is not like C ++. Multiple inheritance is not possible in Java. However, you can implement several interfaces.

+1
source

With the approach of the Generic class, as described in another answer, you will not be able to use RegisteredCar , where you need to pass a Car object. for example, suppose you need to generate an invoice.

 Invoice getInvoice(Car c); 

You cannot use RegisteredCar in this method since it is not of type Car. All APIs that require a car are not RegisteredCar . In some cases, you may need a Plate number as well as a car. There, you may need to map the plate and Plate cars. I would suggest the following approach based on the Decorate Pattern and delegate all car calls to the passed vehicle object.

  public class RegisteredCar extends Car{ public RegisteredCar(Car c, String plateNumber){ } @Override String getColor(){ c.getColor(); } } 
+1
source

You cannot achieve this with inheritance. Your best option is to make the RegisteredCar type shared, and then have a shared instance variable that contains the car of the intended type:

 public class RegisteredCar<T extends Car> { private String plateNumber; private T car; public T getCar() { return this.car; } public T setCar(T car) { this.car = car; } public RegisteredCar (String plateNumber){ this.plateNumber=plateNumber; } public String getPlateNumber() { return plateNumber; } } 

With this, you can pass an object of any type to the RegisteredCar, which is a subclass of Car.

As you can see, I deleted the extends Car part of this class, since it should not be a subclass of the car itself.

0
source

Is there a reason in real classes that you couldn't just add a new function to an existing base class?

 public abstract class Car { public abstract String getManufacturer() ; protected String plate_number = null ; public String getPlateNumber() { return this.plate_number ; } public boolean isRegistered() { return ( this.plate_number != null ) ; } } 
0
source

All Articles