When i run this, i get an error message saying TYpe expected and it points to the line having a.drive(100)... Any help please? Newbie...

class Car
{
        public Car(){
                int odometer=0;
                System.out.println("Car constructed!");
        }
        public void drive(int miles)
        {
                System.out.println("Driving.....");
                odometer+=miles;
        }
        public long odometer;
}
class Guzzler extends Car
{
        public Guzzler(){
                System.out.println("Guzzler Creeated!");
        }
        public void guzzle()
        {
                System.out.println("Guzzling....");
        }
}
public class InheritanceExample
{
        Car a=new Car();
        Guzzler g=new Guzzler();
        a.drive(100);
        g.drive(200);
        System.out.println("Car mileage : "+c.odometer);
        System.out.println("Guzzler mileage  :  "+g.odometer);
        g.guzzle();
        if(g instanceof Car)
        {
                System.out.println("Guzzler is a car!!!");
        }
}

Your code in the InheritanceExample class needs to be inside the main method for it to run as you want it. Wrap up all the code inside the InheritanceExample class inside a main as shown below:-

public class InheritanceExample {
  public static void main(String[] args) { //-> My Addition
        Car a=new Car();
        Guzzler g=new Guzzler();
        a.drive(100);
        g.drive(200);
        System.out.println("Car mileage : "+c.odometer);
        System.out.println("Guzzler mileage  :  "+g.odometer);
        g.guzzle();
        if(g instanceof Car)
        {
                System.out.println("Guzzler is a car!!!");
        }
  } //--> My Addition
}

Here is a tutorial on the structure of a basic Java program.


Also another note on the code inside your Car constructor:-

public Car(){
                int odometer=0;
                System.out.println("Car constructed!");
        }

The above chunk of code does not initialize the "odometer" attribute of the Car class, instead it just declares another local variable by the same name and sets it to '0'. The only reason why this hasn't caused a problem is cause Java initializes class level variables to some assumed defaults, example 'int', 'long' are initialized to '0', Object references to null and so on

When you get errors, please copy and paste the full text of the error message here.
Don't edit the error messages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.