laura
laura

Reputation: 2135

How to write a method that returns an instance of an abstract class?

I am a beginner in Java and i trying to understand the abstract classes. Below is the code that I've written; the question is: how do i write a method that will return an instance of that class.

public abstract class VehicleEngine
{
    protected String name;
    protected double fabricationCons;
    protected double consum;
    protected int mileage;

    public VehicleEngine(String n, double fC)
    {
        name = n;
        fabricationCons = fC;
        mileage = 0;
        consum = 0;
    }

    private void setFabricationCons(double fC)
    {
        fabricationCons = fC;
    }

    public abstract double currentConsum();

    public String toString()
    {
        return name + " : " + fabricationCons + " : " + currentConsum();
    }

    public void addMileage(int km)
    {
        mileage += km;
    }

    public double getFabricationConsum()
    {
        return fabricationCons;
    }

    public String getName()
    {
        return name;
    }

    public int getMileage()
    {
        return mileage;
    }

    //public VehicleEngine get(String name){ 
    //if(getName().equals(name)){
    //return VehicleEngine;
    //}
    //return null;
    //}
}

public class BenzinVehicle extends VehicleEngine
{
    public BenzinVehicle(String n, double fC)
    {
        super(n, fC);
    }

    @Override
    public double currentConsum()
    {
        if (getMileage() >= 75000) {
            consum = getFabricationConsum() + 0.4;
        } else {
            consum = getFabricationConsum();
        }
        return consum;
    }
}

public class DieselVehicle extends VehicleEngine
{
    public DieselVehicle(String n, double fC)
    {
        super(n, fC);
    }

    @Override
    public double currentConsum()
    {
        int cons = 0;
        if (getMileage() < 5000) {
            consum = getFabricationConsum();
        } else {
            consum = getFabricationConsum() + (getFabricationConsum() * (0.01 * (getMileage() / 5000)));
        }
        return consum;
    }
}

This is the main.

public class Subject2
{
    public static void main(String[] args)
    {
        VehicleEngine c1 = new BenzinVehicle("Ford Focus 1.9", 5.0);
        DieselVehicle c2 = new DieselVehicle("Toyota Yaris 1.4D", 4.0);
        BenzinVehicle c3 = new BenzinVehicle("Citroen C3 1.6",5.2);

        c1.addMileage(30000);
        c1.addMileage(55700);
        c2.addMileage(49500);
        c3.addMileage(35400);

        System.out.println(c1);
        System.out.println(c2);
        System.out.println(VehicleEngine.get("Citroen C3 1.6")); //this is the line with problems
        System.out.println(VehicleEngine.get("Ford Focus "));
    }
}

And the output should be:

Ford Focus 1.9 : 5.0 : 5.4
Toyota Yaris 1.4D : 4.0 : 4.36
Citroen C3 1.6 : 5.2 : 5.2
null

Upvotes: 1

Views: 184

Answers (3)

SerkanC
SerkanC

Reputation: 482

Abstract classes do not keep a list of their instances. Actually no Java class does that. If you really want to do that, you could add a static map to VehicleEngine like this:

private static Map<String, VehicleEngine> instanceMap = new HashMap<String, VehicleEngine>();

and change your get method to a static one like this:

public static VehicleEngine get(String name) {
    return instanceMap.get(name);
}

and add this line to the end of the constructor of VehicleEngine:

VehicleEngine.instanceMap.put(n, this);

this way every new instance created puts itself into the static map. However this actually is not a good way to implement such a functionality. You could try to use a factory to create instances, or you could consider converting this class into an enum if you will have a limited predefined number of instances.

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 235994

You can not return an instance of an abstract class, by definition. What you can do, is return an instance of one of the concrete (non-abstract) subclasses that extend it. For example, inside the VehicleEngine you can create a factory that returns instances given the type of the instance and the expected parameters, but those instances will necessarily have to be concrete subclasses of VehicleEngine

Upvotes: 3

Peter Bratton
Peter Bratton

Reputation: 6408

Have a look at the Factory Method pattern. Your concrete classes will implement an abstract method that returns a class instance.

Upvotes: 1

Related Questions