How to write a method that returns an instance of an abstract class?
<p>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.</p>

<pre><code>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() &gt;= 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() &lt; 5000) {
            consum = getFabricationConsum();
        } else {
            consum = getFabricationConsum() + (getFabricationConsum() * (0.01 * (getMileage() / 5000)));
        }
        return consum;
    }
}
</code></pre>

<p>This is the main.       </p>

<pre><code>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 "));
    }
}
</code></pre>

<p>And the output should be:</p>

<pre><code>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
</code></pre>
