≡ Menu

Java Inheritance

In Object Oriented programming (i.e. the Java programming language) Inheritance is one of the key principles that is beneficial to use in the design of any software application. Java inheritance allows for a neat way to define relationships between your Objects (and in turn re-use your code so you don’t have to type the same stuff over and over again).

What is Java Inheritance

So what do I mean when I say that Inheritance allows you to define relationships between Objects? Well, let’s think of some examples of Objects that DO have one or more relationships. Think the object Vehicle, this is a fairly generic term for:

  1. Car
  2. Bus
  3. Motorcycle

Do you see how a Car is a Vehicle, how a Bus is a Vehicle, how a Motorcycle is a Vehicle etc. This is a relationship is what Java Inheritance is all about. When you can verbally say that something is a something else, then you have a relationship between those two Objects, and therefore you have Inheritance.

How does Inheritance help us?

Well, with the examples given above, this means that a Car inherits behaviours and/or attributes from a Vehicle. So let’s think about this for a second, what is a Car? Well, it’s a Vehicle with four wheels, doors and around 5 seats. Okay, what’s a Bus? It’s also a Vehicle likely with more than 4 wheels, doors and probably somewhere around 30 seats. What’s a Motorcycle? It’s a Vehicle with two wheels, no doors and one or two seats. Once you start to “map” out all of the characteristics of your Objects, you’ll begin to see what similarities they have (i.e. what they have in relation), and also what they don’t have in relation to each other. This is very important with Java inheritance. If all your Objects share something in common, then this can be considered an attribute of the super class. Whatever they do not have in common will be attributes of the child classes.

What is a super Class and what is a child Class?

With our example, the super class is the Vehicle object and the child classes are the Car, Bus and Motorcycle. The super Class is essentially the Object that will hold all the attributes that are common. So our Vehicle super Class would have the following attributes:

  • Wheels
  • Seats

From our inspection of all the types of Vehicles above, we identify that all types of Vehicles have wheels and seats. But notice that I didn’t put doors as part of the Vehicle object. This is because Motorcycles don’t have doors! Doors would only be attributes of Cars and Busses, so we will have a door attribute on the Car and Bus Objects. Make sense?

Coding Inheritance in Java

When we’re coding this thing called Inheritance in Java, what does it look like? Well it can take the form of either an Interface or an Abstract Class. I’ll talk more about what these are specifically in a later post, but for now all you need to know about them is this:

  • Interface = An outline (or skeleton) of an Object with no implementation
  • Abstract Class = An outline of an Object that can contain an implementation

Without further delay, let’s look at some examples of an Interface and an abstract class.

Interface

public interface Vehicle
{
  public Integer getNumberOfSeats();
  public Integer getNumberOfWheels();
  public String getVehicleType();
}

Here we’ve declared an Interface for our Vehicle and it has three methods, getNumberOfSeats(), getNumberOfWheels() and getVehicleType(). As you can see, there is no implementation of the code, we’ve just outlined some methods. So, now to make this interface useful, we need to implement it somewhere! So let’s see what that would look like:

public class Car implements Vehicle
{
  @Override
  public Integer getNumberOfSeats()
  {
    return 5;
  }
 
  @Override
  public Integer getNumberOfWheels()
  {
    return 4;
  }
 
  @Override
  public String getVehicleType()
  {
    return "Car";
  }
 
  public Integer getNumberOfDoors()
  {
    return 2;
  }
}

This is what the Car‘s implementation of the Vehicle interface would look like! For this example, I’ve stated (in code) that a Car has 5 seats, 4 wheels and 2 doors. Let’s see what a Bus would look like:

public class Bus implements Vehicle
{
 
  @Override
  public Integer getNumberOfSeats()
  {
    return 35;
  }
 
  @Override
  public Integer getNumberOfWheels()
  {
    return 6;
  }
 
  @Override
  public String getVehicleType()
  {
    return "Bus";
  }
 
  public Integer getNumberOfDoors()
  {
    return 4;
  }
}

Pretty self explanatory right? Well, except for those @Override lines. What do those mean? These are called annotations and these were introduced in Java version 5 (we are currently on Java version 7). An annotation is anything that you see with an @ (at) symbol before some text above a method declaration or a class declaration. These particular @Override annotations are just saying that the method below it are from a parent (or super) class, and we are implementing the desired behaviour in this particular class. In the case of an Interface, we have to override the methods, as it’s a requirement with Interfaces.

Take special note that we don’t have an @Override annotation on our getNumberOfDoors() method. This is because it wasn’t declared in our Interface. Remember why? Because a Motorcycle doesn’t have doors, so it wouldn’t make sense to put it in the Interface! Now, don’t get me wrong, in the world of programming there are always several ways to solve the same problem, so you could have put something like hasDoors() in the Vehicle Interface and had it return a Boolean value of true or false (true in the case of Car and Bus, false in the case of Motorcycle). But, for the purpose of illustrating that you can have your own non-overridden methods in your child classes, I chose to do it the way I did it.

Abstract Class

So how about we look at abstract classes now. Remember that abstract classes don’t necessarily need their methods overridden, and the methods can contain implementation if you want. If we were to create an abstract class for the Vehicle object, it could look like this:

public abstract class Vehicle
{
  public String vehicleType;
 
  public Integer getNumberOfSeats()
  {
    if (this.vehicleType.equals("Car"))
    {
      return 5;
    }
    else if (this.vehicleType.equals("Bus"))
    {
      return 20;
    }
    else if (this.vehicleType.equals("Motorcycle"))
    {
      return 1;
    }
 
    // the vehicleType variable has not yet been set to anything,
    // so we cannot say what number of seats this vehicle has, so
    // we will return null.
    return null;
  }
 
  public String getVehicleType() 
  {
    return this.vehicleType;
  }
 
  public abstract Integer getNumberOfWheels();
}

So as you can see here, we have some real code implemented in our getNumberOfSeats() method. The code relies on the vehicleType attribute. So let’s take a look at how a child class would use this Vehicle abstract class:

public class Car extends Vehicle
{
  public Car ()
  {
    this.vehicleType = "Car";
  }
 
  public Integer getNumberOfWheels()
  {
    return 4;
  }
}

The first noticeable difference between an interface and an abstract class, is that you need to use the keyword implements when you want a child class to use an Interface and you need to use the keyword extends when you want a child class to use an abstract class. We’ve also done something interesting with this code:

  public Car ()
  {
    this.vehicleType = "Car";
  }

This is called a constructor. The purpose of a constructor in Java is to outline a section of code that will be executed when an Object is first instantiated. So, this just means that when someone creates an instance of our Car Object, Java will automatically set the vehicleType to be “Car”. I’ll post a video that shows the code flow of constructors in Java, so no worries if this doesn’t make sense right away.

So now, if we were to write some code get the number of seats that our Car has, we would see that it has 5, because Java will see that the super class (Vehicle) has a method that defines the number of seats (getNumberOfSeats()).

Okay, so now for those who want to go the extra mile, I challenge you to put together a Java program that will allow you to use an abstract Vehicle class and properly output the following console lines:

My Car has 2 seats.
My Car has 4 wheels.
My Bus has 20 seats.
My Bus has 6 wheels.

Using this Java main class:

  public static void main(String[] args)
  {
    Vehicle myCar = new Car();
    System.out.println("My " + myCar.getVehicleType() + " has " + myCar.getNumberOfSeats() + " seats.");
    System.out.println("My " + myCar.getVehicleType() + " has " + myCar.getNumberOfWheels() + " wheels.");
 
    Vehicle myBus = new Bus();
    System.out.println("My " + myBus.getVehicleType() + " has " + myBus.getNumberOfSeats() + " seats.");
    System.out.println("My " + myBus.getVehicleType() + " has " + myBus.getNumberOfWheels() + " wheels.");
  }

Try your best and if you get stuck, just leave a comment on this post and I’ll see what I can do to help you out! Remember, the best way to learn is to practice, practice, practice! So running into problems is a good thing, you just need to make sure you don’t get frustrated an give up, so please ask me for help before you give up! Also, if you want to know when the video about constructors is ready, just sign up to my mailing list below, and if you want to support me, please share this post by clicking like to the left of this text (or tweet / G+ / stumble upon).

Thanks and best of luck with your coding!

 

{ 35 comments… add one }

  • gefei August 7, 2012, 7:01 am

    You may want to mention that the most important thing about inheritance is polymorphism which means that an instance of a sub-class can be used as one of a super-class. For instance, wherever you need a Vehicle, you can use a Car.

    Another note: there is no need to implement “getNumberOfSeats” in Vehicle. Just declare this methods to be abstract and hence force every subclass to implement it. This way, you implement the number of a (concrete) vehicle only once, in the concrete sub-class, instead of (as in your code) twice, once by if-then-else in Vehicle, and once in the concrete sub-class.

  • Anon E. Mouse August 7, 2012, 7:58 am

    myCar.getNumberOfWheels() + ” seats.”

    “My car’s ” + myCar.getNumberOfWheels() + ” wheels are also seats.”

    • Trevor Page August 7, 2012, 8:08 am

      Copy paste fail… I’ve updated the post

      Thanks Anon :)

  • Javin August 7, 2012, 8:05 am

    Great tutorial , indeed quite detailed. Thanks for sharing .

  • nick August 9, 2012, 7:59 am

    Hi Trevor,

    Okay I’m stuck already, before I even try entering code. I don’t know if my problem is my understanding of how to use the IDE or what.

    My very outdated programming experience would have me create a main function followed by all the other functions that I would need to call, listed underneath the main function. But I’m assuming with Java each ‘Class’ is a separate file that can contain a number of ‘methods’ within itself?

    So I see you have given us the main class, I’m assuming we would just need to create the ‘Vehicle’ class? Also, am I correct in assuming the main class is creating the two child classes for ‘Car’ and ‘Bus’?

    When I try to create a class I can’t seem to make it ‘abstract’, is this just amended by hand or am I missing something?

    I feel like I’ve missed something along the way.

    Thanks for your help and for taking the time to produce these tutorials.

    • Trevor Page August 9, 2012, 8:09 am

      You are correct in that every file is a Class (the filename needs to match the Class name). SO if you create Vehicle.java, your class should look like:

      public class Vehicle
      {

      }

      And each class can contain methods, so:

      public class Vehicle
      {

      public static void main (String[] args)
      {
      // this is where java will start running the code,
      // you should really only have one “main” class per project
      }
      }

      So conceptually, I think the problem is that you’re saying that “main” is a class, where that’s not the case. “main” is a method INSIDE of your class :)

      • nick August 9, 2012, 8:24 am

        Aha! Thank’s for clearing that up for me, I think your description above the main method example of: “Using this Java main class:” through me a little but after a second look I can see there is no ‘class’ declaration (if that’s the correct word to use).

        So if you can explain, should only one Class in an application have a main method or can each class have a main method and if so where does the project/program know where to start from?

        Thank’s again.

        • Trevor Page August 9, 2012, 8:53 am

          One Class in an application should have a main method… as it’s the “point of entry” for the code. The entire application will run from that main method as a starting point.

  • Joe August 9, 2012, 11:00 am

    Another Small Error:

    Okay, so now for those who want to go the extra mile, I challenge you to put together a Java program that will allow you to use an abstract Vehicle class and properly output the following console lines:

    My Car has 2 seats.
    My Car has 4 “seats.” <– " wheels."
    My Bus has 20 seats.
    My Bus has 6 "seats." <– " wheels."

  • sibudi August 9, 2012, 11:25 am

    hi, thanks for sharing.
    I still waiting your blog post about static keyword :)

  • Corey August 20, 2012, 4:49 pm

    Thank you so much Trevor for these tutorials. I really appreciate the time you’ve taken to explain these concepts clearly and concisely.

    I’m trying to put together the Vehicle program and I’m hitting just one small snag in the following method:

    public String getVehicleType() 
    	  {
    	    return this.vehicleType;
    	  }
    

    My IDE (Eclipse) is warning me “vehicleType cannot be resolved or is not a field”

    I have this method placed after the methods establishing the vehicle class and the attributes of each vehicle type, but before the main method. Any thoughts? Thank you!

    Here is the whole thing (without the intro lines, to save a bit of space here).

    	public abstract class Vehicle	{
    	  public String vehicleType;
    	  public abstract Integer getNumberOfSeats();
    	  public abstract Integer getNumberOfWheels();
    	}
    	public class Car extends Vehicle	{
    	  public Car ()	  {
    	    this.vehicleType = "Car";
    	  }	   
    	  public Integer getNumberOfWheels()	  {
    	    return 4;
    	  }
    	  public Integer getNumberOfSeats()	  {
    		return 5;
    	  }
    	}
    	public class Bus extends Vehicle	{
    	  public Bus ()
    	  {
    	    this.vehicleType = "Bus";
    	  }	   
    	  public Integer getNumberOfWheels()	  {
    	    return 6;
    	  }
    	  public Integer getNumberOfSeats()	  {
    		return 20;
    	  }
    	}
    	public String getVehicleType() 	{
    		return this.vehicleType;
    	}
    	public static void main(String[] args) {
    		Vehicle myCar = new Car();
    	    System.out.println("My " + myCar.getVehicleType() + " has " + myCar.getNumberOfSeats() + " seats.");
    	    System.out.println("My " + myCar.getVehicleType() + " has " + myCar.getNumberOfWheels() + " wheels.");	 
    	    Vehicle myBus = new Bus();
    	    System.out.println("My " + myBus.getVehicleType() + " has " + myBus.getNumberOfSeats() + " seats.");
    	    System.out.println("My " + myBus.getVehicleType() + " has " + myBus.getNumberOfWheels() + " wheels.");
    	  }
    
    • Trevor Page August 20, 2012, 5:30 pm

      Hi Corey,

      Thanks for your comment… after reviewing your code, I have a couple of questions:

      1) Do you have all of that code in one file? If so, that’s a problem. Each time you declare a Class, that code should exist in a separate file named after the Class’ name. For example, public class Car extends Vehicle should be in a file called “Car.java”. public class Bus extends Vehicle should be in a file called “Bus.java”. Make sense?

      2) Does your public static void main (String[] args) method exist INSIDE of a Class? What I mean by this is does the “main” method appear inside of the curly braces {} of your Vehicle Class?

      • Corey August 20, 2012, 6:23 pm

        Many thanks for your reply. I’m a total beginner, obviously, and I had no idea that each Class has to have it’s own file. I restructured the code into three class files (Vehicle + main, car, and bus) and now it works! On to the next tutorial :)
        You are the man!

  • Phil October 29, 2012, 8:34 pm

    Hi Trevor,

    I’m finding your tutorials very helpful, thanks for writing them! I just have a quick question. In the Car class, you have the following lines:

    public class Car extends Vehicle
    {
      public Car ()
      {
        this.vehicleType = "Car";
      }
    }
    

    Where you say that this sets the vehicleType = “Car” by default. Why does it have to be in a separate method called Car? Will it still be automatically set when a Car object is created if I write it like this?

    public class Car extends Vehicle
    {
        this.vehicleType = "Car";
    }
    

    Thanks so much for your help.

    • Trevor Page October 30, 2012, 11:07 am

      Hi Phil,

      Excellent question. The secret lies in something called a “constructor”. The Car “method” you mentioned is actually not a method, it’s a constructor.

      A constructor is automatically called when the object is instantiated. In other words, when someone creates a new Car object, the constructor will be invoked. The instantiation of the Car object would look like this:

      Car aCar = new Car();
      

      Now, with regards to the code you’ve suggested, I don’t believe it will compile properly. The area where you’re putting the reference to this.vehicleType = "Car"; is not the correct area for an assignment. This area is meant mostly for creating variables, if you wish to just assign a value to a variable, it must be inside of a method/constructor. If you were to type this into an IDE like STS, you’ll see that it does not compile.

      The way I suggested setting a default value via a constructor is a convention in Java, and most other object oriented languages.

      I hope that helped clear things up for you, thanks for the question and let me know if you need any other guidance.

  • Durim March 1, 2013, 2:50 am

    Hello Trevor!

    Look at my code, is it ok ?! I changed something, and I think everything works fine yet!
    Are these ok:
    public int getNumberOfSeats { …
    return 0
    }
    I used “int” instead of “Integer”, is there any problem that I can’t see ?!

    Car myCar = new Car(); instead of Vehicle myCar = new Car(); etc!

    Thank you!

    The Code:

    package com.howtoprogramwithjava.example;
    
    public abstract class Vehicle {
    	public String vehicleType;
    	
    	public int getNumberOfSeats() {
    		if (this.vehicleType.equals("Car")) {
    			return 2;
    		} else {
    			if (this.vehicleType.equals("Bus")) {
    				return 20;
    			} else {
    				if (this.vehicleType.equals("Motorcycle")) {
    					return 1;
    				}
    			}
    		}
    		
    		// the vehicleType variable has not yet been set to anything,
    	    // so we cannot say what number of seats this vehicle has, so
    	    // we will return 0, because 0 doesn't make sense!
    		return 0;
    	}
    	
    	public String getVehicleType() {
    		return this.vehicleType;
    	}
    	
    	public abstract int getNumberOfWheels();
    }
    
    package com.howtoprogramwithjava.example;
    
    public class Car extends Vehicle {
    	
    	public Car() {
    		this.vehicleType = "Car";
    	}
    	
    	@Override
    	public int getNumberOfWheels() {
    		return 4;
    	}
    }
    
    package com.howtoprogramwithjava.example;
    
    public class Bus extends Vehicle {
    	
    	public Bus() {
    		this.vehicleType = "Bus";
    	}
    	
    	@Override
    	public int getNumberOfWheels() {
    		return 6;
    	}
    }
    
    package com.howtoprogramwithjava.example;
    
    public class StartUpClass {
    	public static void main(String[] args) {
    		Car myCar = new Car();
    		System.out.println("My "+myCar.getVehicleType()+" has "+myCar.getNumberOfSeats()+" seats.");
    		System.out.println("My "+myCar.getVehicleType()+" has "+myCar.getNumberOfWheels()+" wheels.");
    		
    		Bus myBus = new Bus();
    		System.out.println("My "+myBus.getVehicleType()+" has "+myBus.getNumberOfSeats()+" seats.");
    		System.out.println("My "+myBus.getVehicleType()+" has "+myBus.getNumberOfWheels()+" wheels.");
    	}
    }
    
    • Trevor Page March 1, 2013, 6:42 pm

      From what I can see there are a couple of errors:

      When you’re using Inheritance it’s important to “program to the interface”. In other words you should favor the statement:


      Vehicle car = new Car();

      This is the case because Vehicle is the super class and Car is a type of Vehicle.

      The second issue I see is that you’re declaring a getNumberOfSeats() method in BOTH the super class and the child class AND it looks like they have duplicate functionality. I think that you just need to override this method in the child classes and remove it from the Vehicle super class.

      • Durim March 2, 2013, 2:30 pm

        Ok, I’ll fix this: Vehicle myCar = new Car();

        But, look again what you say in the second issue :$

        You said that I have decleared getNumberOfSeats() method in child classes too!!
        I don’t see any getNumberOfSeats() in Car and Bus classes ?!
        Or maybe you mean anything else :$

        • Trevor Page March 2, 2013, 3:15 pm

          My apologies, I got your methods mixed up… I would just move the getNumberOfSeats() out of the super class and into the child classes.

          • Durim March 3, 2013, 1:50 am

            Ahh right, thanks:)
            But, can you tell me a little more, why doing in that way is better?!
            thank you, thank you, thank you:)

          • Trevor Page March 3, 2013, 8:59 am

            In general, you want to try and keep the functionality in the specific Object that it applies to. In the case of your code, you were defining rules for the Car, Bus and Motorcycle in the Vehicle class. This isn’t optimal, as those specific rules should just belong in the specific Objects.

            If you had a general rule that applies to ALL Busses, Motorcycles and Cars, then you would place that in the Vehicle object (as it’s more generic and can’t be applied to a more “specific” Object than the Vehicle Object).

            Does that explanation make sense?

  • Paul March 20, 2013, 8:14 pm

    Hi Trevor,

    Slowly but confidently getting to grips with Java thanks to your help so far.

    My only query is for the examples so far the IDE I’m using is Netbeans, should I switch to the one you proposed earlier to get a better grip of the software package – what is the industry standard?

    All the best,

    Paul

    • Trevor Page March 20, 2013, 10:08 pm

      Honestly, Netbeans works just fine as an IDE, if you’re used to it then stick with it… if you’re new to using Netbeans, then I think it would be a good idea to switch to the Spring Tool Suite since it’ll integrate well with my teachings here.

      As for which it used in the industry, I’ve worked for two software companies professionally, but they both used the Spring Tool Suite. That’s by no means a large enough sample size to come to any sort of conclusion, so that’s why I’d defer to my statement above :)

  • Fawwaz July 25, 2013, 12:12 pm

    Hi Trevor!

    I finally did your ‘extra mile’ challenge!!
    My finish results are:

    My car has 5 seats
    My car has 4 wheels
    My bus has 30 seats
    My bus has 6 wheels

    The first time I tried to run the scripts it came up with a bunch of errors. The second time i tried it said:

    My null has 5 seats
    My null has 4 wheels
    My null has 30 seats
    My bus has 6 wheels

    But then i found out that i spelled Vehicle incorrectly (I spelled it Vechile :D)! So then I had to cut the scripts, delete the “Vechile” class and create a new one! Then I have to re-type the Car and Bus class’ scripts so it works again.

    Thank you so much for showing me how to use Java and Javascripts. I’ve read all your tutorials up till here.

    Thanks again,
    Fawwaz

    P.S. I wish I know how to put screenshots in here so I can show you! ;)

  • Ryu July 31, 2013, 6:52 am

    This is my Vehicle class and Car class
    ———–
    package com.vehicleType;

    public abstract class Vehicle {
    public String vehicleType;
    public String getVehicleType()
    {
    return this.vehicleType;
    }
    public abstract Integer getNumberOfSeats();
    public abstract Integer getNumberOfWheels();
    }
    —————–
    package com.vehicleType;

    public class Car extends Vehicle {
    public Car ()
    {
    this.vehicleType = “Car”;
    }
    public Integer getNumberOfWheels()
    {
    return 4;
    }
    public Integer getNumberOfSeats()
    {
    return 2;
    }
    }
    ——————-

    I have to ask, beside how I can see there’s no need for @Override, what is the difference between an Abstract class and Interface one? I didn’t quite get the idea myself. Thank you for reading.

  • carolyn October 10, 2013, 10:22 am

    Thanks Trevor, really appreciate this

  • Gopal December 20, 2013, 3:59 am

    Program to print elements which occurs thrice in the array with single loop.

    package com.test;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    public class ArrayTest {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    int[] array = {1,2,3,4,1,2,4,3,2,3,4,4,4,1,5,6,5,7,5,6,8,7,6,7};
    int[] finalArray = new int[20];
    System.out.println(array);
    Arrays.sort(array);
    System.out.println(array);
    int value=0;
    int counter=1;
    for (int i = 0; i < array.length-1; i++)
    {
    if (array[i] == array[i+1]) {
    counter++;
    } else {
    counter=1;
    }
    if(counter==3) {
    finalArray[value++] =array[i];
    } else if (counter==4) {
    finalArray[--value]=0;
    }
    }

    for (int i = 0; i < finalArray.length-1; i++)
    {
    System.out.println(finalArray[i]);
    }
    }

    }

  • JRC September 2, 2014, 8:35 pm

    Hey Trevor,
    i keep getting the error “Exception in thread “main” java.lang.Error: Unresolved compilation problem:

    at Vehicle.Vehicle.main(Vehicle.java:53)”

    this is what i have…

    public abstract class Vehicle
    {
    public String VehicleType;

    public class Car extends Vehicle
    {
    public Car ()
    {
    this.VehicleType = “Car”;
    }

    }

    public class Bus extends Vehicle
    {
    public Bus ()
    {
    this.VehicleType = “Bus”;
    }
    }

    public Integer getNumberOfSeats()
    {
    if (this.VehicleType.equals(“Car”))
    {
    return 5;
    }
    else if (this.VehicleType.equals(“Bus”))
    {
    return 35;
    }
    }
    public Integer getNumberOfWheels()
    {
    if (this.VehicleType.equals(“Car”))
    {
    return 4;
    }
    else if (this.VehicleType.equals(“Bus”))
    {
    return 6;
    }
    }
    public String getVehicleType()
    {
    return this.VehicleType;
    }

    public abstract Integer getNumberOfWheels();

    public static void main(String[] args)
    {
    Vehicle myCar = new Car();
    System.out.println(“My ” + myCar.getVehicleType() + ” has ” + myCar.getNumberOfSeats() + ” seats.”);
    System.out.println(“My ” + myCar.getVehicleType() + ” has ” + myCar.getNumberOfWheels() + ” wheels.”);

    Vehicle myBus = new Bus();
    System.out.println(“My ” + myBus.getVehicleType() + ” has ” + myBus.getNumberOfSeats() + ” seats.”);
    System.out.println(“My ” + myBus.getVehicleType() + ” has ” + myBus.getNumberOfWheels() + ” wheels.”);
    }
    }

    not going to lie, i feel like i have no idea what im doing lol

    • Trevor Page September 3, 2014, 2:04 pm

      You’ve almost got it!

      The only thing going wrong here is that you need to break up the code into different files. Three files to be exact:

      Vehicle.java
      Car.java
      Bus.java

      And the respective code needs to go into each class (ie. each individual java file).

      Let me know if you need any more help in getting this problem solved :)

      • JRC September 4, 2014, 5:36 pm

        Hey trevor, that sounds great, however one quick question lol, how do i seperate them into different files, ive been trying to get this seperated for abit today, not sure where in the code it needs to be seperate, and furthermore what to write in order to actually seperate them?

        JRC

  • JRC September 6, 2014, 5:53 pm

    Hey trevor, i cant see any of the comments on the bottom of any of the pages. so i have no idea if you responded to me, if so, please email me your response. something wrong with the page i believe.

    smam3579@gmail.com

    thanks,
    joe

Leave a Comment

Powered by sweet Captcha