Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

A few questions about concrete and abstract classes

(If you don't already know concrete just means non abstract)

Consider the following code:

public abstract class Vehicle {
    private String type;
    public abstract void goUpHill();
    public String getType() {
        return type;
    }
}

public abstract class Car extends Vehicle {
    public abstract void goUpHill();
    public abstract void goDownHill();
    public void doCarThings() {
        // special car code goes here
    }
}

public class MiniCooper extends Car {
    public void goUpHill() {
        // MiniCooper-specific going uphill code goes here
    }
    public void goDownHill() {
        // Do I also need to implement this method?
    }
}

public class Subaru extends Car {
    // Does the Subaru subclass need to also implement the goUpHill();
    // and goDownHill(); methods?
}

I have a few questions:

  1. Since both MiniCooper & Subaru extends Car does this mean Car is the superclass of MiniCooper & Subaru? Or is Car, MiniCooper, and Subaru all considered subclasses of Vehicle? Since Car is a subclass of Vehicle.
  2. Does the class Subaru also need to implement the goUpHIll(); and goDownHill(); abstract methods since it extends the abstract class Car? Or does only the FIRST concrete class(MiniCooper) below an abstract superclass have to implement that superclass's abstract methods?
  3. For whatever reason, if Subaru extended MiniCooper would I still have to implement the goUpHill(); and goDownHill(); abstract methods? (I would guess no, because it would be extending a concrete class, but I'm not sure if I am right)
  4. Finally, does MiniCooper need to also implement the goDownHill(); method?

1 Answer

deckey
deckey
14,630 Points

Hey Derek, good questions

Since both MiniCooper & Subaru extends Car does this mean Car is the superclass of MiniCooper & Subaru? Or is Car, MiniCooper, and Subaru all considered subclasses of Vehicle? Since Car is a subclass of Vehicle.

  • Yes for the 1st one, but more like a grandparent on a second question ;) Vehicle > Car > MiniCooper / Subaru - that's the relation.

Does the class Subaru also need to implement the goUpHIll(); and goDownHill(); abstract methods since it extends the abstract class Car? Or does only the FIRST concrete class(MiniCooper) below an abstract superclass have to implement that superclass's abstract methods?

  • If a class extends another superclass it inherits super methods, as they are defined in the super class. So if you need MiniCooper to have a different goUpHill() method than Car, then you need to re-declare it. Order of declaration has no meaning.

For whatever reason, if Subaru extended MiniCooper would I still have to implement the goUpHill(); and goDownHill(); abstract methods? (I would guess no, because it would be extending a concrete class, but I'm not sure if I am right)

  • If a class is not abstract but extending an abstract class, it needs to implement all abstract methods. So, MiniCooper needs to implement all declared Car methods. Same goes for Subaru.

Finally, does MiniCooper need to also implement the goDownHill(); method?

  • Yes, as explained in 3.

Hope it helps, good luck! Deckey

Hey deckey, thanks for the helpful and informative answers, I think I'm all good now :) It all makes a lot more sense to me. Just to clarify, when you said "If a class is not abstract but extending an abstract class, it needs to implement all abstract methods." Does that mean if Subaru extended MiniCooper, Subaru wouldn't have to implement the abstract methods since both Subaru and MiniCooper are concrete classes?

deckey
deckey
14,630 Points

Hi Derek, yes.

If Subaru extends MiniCooper it wouldn't need to implement it's methods, but MiniCooper would need since it's inheriting from abstract class.

good luck! Deckey

Gotcha, thanks for the clarification deckey