-1

I have created a superclass Airplanes and a subclass Gliders

public class Airplanes
{
public String hasWings;
public int numberWheels;
public int numberPassengers;

    public Airplanes(String hasWings, int numberWheels, int numberPassengers)
    {
    // initialise instance variables
    this.hasWings=hasWings;
    this.numberWheels=numberWheels;
    this.numberPassengers=numberPassengers;
    }
}

and

public class Gliders extends Airplanes
{
private String hasEngine;

public Gliders(String hasWings,int numberWheels,int numberPassengers,String hasEngine)
{
    super(hasWings,numberWheels,numberPassengers);
    this.hasEngine=hasEngine;
}

public void getSpec()
{
    System.out.println("Glider With wings: " + super.hasWings);
    System.out.println("Glider Wheels: " + super.numberWheels);
    System.out.println("Glider Number of passenger: " + super.numberPassengers);
    System.out.println("Glider has engine: " + hasEngine);
}
}

A new class test has to initialise Gliders and is written like this:

public class Test
{
   public Test()
   {
    Airplanes alfa = new Gliders("Yes",2,2,"Np");
    Gliders.getSpec();
   }
}

When i try to run Gliders.getSpec() it prints an error saying that i'm trying to reference a non static method from a static context.

I'm just wondering if what i'm trying to do is legal or not, and if it is how can i fix it?

VedantK
  • 9,728
  • 7
  • 66
  • 71
BioShock
  • 763
  • 2
  • 13
  • 33

1 Answers1

2

Your getSpec method is non static method (instance method). So you need to create an instance of Gliders first.

new Gliders().getSpec();

or

Gliders glider = new Gliders();
glider.getSpec();

if you still want to call getSpec like that, you need to change getSpec to static method.

public static void getSpec()
{
}
Iswanto San
  • 18,263
  • 13
  • 58
  • 79