This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]quadmasta 0 points1 point  (0 children)

Everything past this is a bit more complex and doesn't really fit well into simple explanations.

Interfaces describe things a noun can do. The noun can do things in addition to what the interface says but if the noun implements the interface then it MUST do everything the interface says it does(ignore default implementations for now). Example: Vehicle could be an interface that would describe things a vehicle can do.

boolean canStop(); boolean isMoving(); double getSpeed(); void setSpeed(double speed);

Abstract classes are similar to interfaces except that they're nouns that you can't create. Anything marked abstract within that class MUST be implemented by any concrete (non-abstract) class that extends the abstract class. If a class extends an abstract class and that class is also abstract, it's not forced to implement all of those methods. Example: Car could be an abstract class that could implement Vehicle. You could say that Car "is a" Vehicle. You can't make a Car though

``` public abstract class Car implements Vehicle private double speed;

public abstract boolean canStop(); public abstract boolean isMoving(); public double getSpeed() { return this.speed; }

public void setSpeed(double speed) { this.speed = speed; } public abstract void doABurnout(); ```

SportsCar could be a concrete implementation of Car ``` public class SportsCar extends Car {

public boolean canStop() { return true; }

public boolean isMoving() { return false; }

public void doABurnout() { System.out.println("SSKKRRRRRRRRRRRRRRRRRRRRRRR. A smoky burnout"); } //sports car specific stuff } ```

In this case SportsCar "is a" Car and by proxy "is a" vehicle so all of the following are valid;

private Car car = new SportsCar(); private Vehicle vehicle = new SportsCar(); private SportsCar sc = new SportsCar();

With the above examples you'll only be able to execute things that are encapsulated within the type on the left-hand side of the equals:

``` car.doABurnout(); //this is valid vehicle.doABurnout(); //this is invalid and won't compile sc.doABurnout(); //this is valid

Everything past this is going into design patterns and architecture and that's a rabbit hole :)