2

Let me explain my situation:

 public class Car extends Vehicle{
      public Car() { }
      public Car(String model){
        super(model);
        AutoParts autoParts=new AutoParts(Car.this.getSuperClass); //get compile error here
      }
    }


   public class AutoParts{
     Vehicle _Vehicle;
     public AutoParts() { }
     public AutoParts(Vehile vehicle){
        this._Vehicle=vehicle;
     }
   }

Sorry for the horrible example. But during the initialization, Ford ford=new Ford(Car.this.getSuperClass);, I want to be able to pass an instance of the superclass as a parameter to the constructor.

How do I do it? How do I get the instance of a superclass?

**

Edit:

Class name, Ford renamed to AutoParts

Irshu
  • 8,248
  • 8
  • 53
  • 65
  • 1
    Try with pure new `Ford(this)`. Every instance of `Car` is also an instance of `Vehicle`, no dawdling with introspection required. Note, however, that this might give you a [leaking this in constructor](https://stackoverflow.com/questions/3921616/java-leaking-this-in-constructor) warning in some ides (and rightfully so). – dhke May 01 '16 at 06:55
  • You don't have an instance of a superclass anywhere. You have a class reference, but I don't see why you want that – OneCricketeer May 01 '16 at 06:55
  • 8
    Why do you want to do that? Just pass an instance of `Car`, and in `Ford`, use it as one of `Vehicle`. An instance of a subclass can be used as an instance of a superclass – gefei May 01 '16 at 06:56
  • 5
    In terms of design, why would a `Ford` *compose* a `Vehicle` anyway? I would expect `Ford` to be a subclass of `Car`... – Jon Skeet May 01 '16 at 06:57
  • 1
    As stated by @gefei : Ford f = new Ford(new Car("Mondeo")); will work. – RubioRic May 01 '16 at 06:58
  • @gefei, @cricket_007, @Jon Skeet: sorry for the late reply. Because i have some methods in the SuperClass that needs to be available in `Ford` (now renamed to `AutoParts`), actually i have several sub classes, and instantiating the class multiple times didnt make sense. Thanks for the answer. Do you think i'm following a bad practice here? – Irshu May 01 '16 at 10:36
  • I think that the point we can not understand is: why do you not invoke just AutoParts autoParts=new AutoParts(this);? If "this" is a Car, it is also a Vehicle, you can access Vehicle methods. – RubioRic May 01 '16 at 10:52
  • @RubioRic, i thought this refers to the Vehicle object. It works. – Irshu May 01 '16 at 11:03

2 Answers2

3

The first question is why do you want an instance of the superclass specifically? I shall assume that you do not want your Ford to have access to a Car's additional members (that extend the Vehicle class), because a Ford may be, say, a truck. You can use casting like so to achieve this:

Ford ford=new Ford((Vehicle)this);

However, if you have any method overrides in your Car (that is, the "this" object) these will still be in place in the Vehicle that you pass to your Ford constructor, is that what you want?

Without being patronising, I think we're off the beaten track of OO design here. A Ford doesn't have a Vehicle, so having a Vehicle field is wrong. A Ford IS a vehicle, so therefore it should extend Vehicle. Also how come when creating a Car, does it necessarily create a Ford? A car IS a Ford, so I would have the Car class extend Ford (not the other way round, Jon Skeet, as there could be Ford trucks/motorbikes etc.)

1

I'm not sure if the following is the right way of OP but I adapt that what you want to do

public class Vehicle{
    private String model;

    public Vehicle() {
    }

    public Vehicle(String model) {
        this.model = model;
    }

    public String getModel() {
        return model;
    }
}

public class Car extends Vehicle {

    public Car() {
    }

    public Car(String model) {
        super(model);
        AutoParts autoParts = new AutoParts((Vehicle)this); //Cast the instance of this class to it's superclass and pass the instance to the constructor of the autoParts object
        System.out.println(autoParts.getVehicle().getModel());
    }
}

public class AutoParts {

    Vehicle vehicle;

    public AutoParts() {
    }

    public AutoParts(Vehicle vehicle) {
        this.vehicle = vehicle;
    }

    public Vehicle getVehicle() {
        return vehicle;
    }
}

Now the following usage will works:

Car car = new Car("Model T");
// Output prints in the car constructor: Model T