954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Type expected error

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!!!");
        }
}
vs.vaidyanathan
Newbie Poster
23 posts since May 2009
Reputation Points: 28
Solved Threads: 0
 

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

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: