Interfaces, Abstract classes & Inheritance

Making abstractness concrete

Quick review

  1. When to use the keyword this?
     
  2. How Java stores objects?
     
  3. What is enum?
     
  4. What is a static field in a class?

Contents

  1. Interfaces

  2. Abstract classes

  3. Inheritance

Wacky Racer

Bad joke
of the day

Interface - Switch

You don't know how it works

It has two operations - turnOn & turnOff

You can use it to turn on the lights, heater etc

Interface

Manipulate objects without
knowing how they work

Contract between the user of the code
and the one who wrote it

Useful when you have similar but not identical objects

Define Vehicle interface

public interface NAME {

    void METHOD_NAME();

}
public interface Vehicle {

    int getPosition();

    PaintColor getColor();

    Driver getDriver();

    int getTopSpeed();

    void moveLeft();

    void moveRight();

    int getInitialSpeed();

    VehicleType getVehicleType();
}

Inheritance

Reuse all the methods and fields from other class

Create is a relationship

e.g. Car is a Vehicle

Add new fields and methods or replace existing ones

Inheritance in Java


class SUBCLASS_NAME extends PARENT_NAME {



    @Override
    public void PARENT_METHOD_NAME() {

    }
}

Extend existing class

Replace parent

method

You can inherit only from 1 class

How to implement Interface

public class CLASS_NAME implements INTERFACE_NAME {

    @Override
    public void METHOD_NAME() {

    }
    // All methods from the interface
}

A class can implement multiple interfaces

@Override tells the compiler to replace the parent method with the new one

Make BaseVehicle

public class BaseVehicle implements Vehicle {

    @Override
    public int getPosition() {
        return position;
    }

    @Override
    public PaintColor getColor() {
        return color;
    }

    ...
}

Protected fields & methods

public class BaseVehicle implements Vehicle {

    protected int position;

    ...
}

Protected components can be accessed only from the children of a class

Break (10 min)

Abstract class

  • An abstract class is a class that is declared abstract—it may or may not include abstract methods
  • Cannot be instantiated
  • Can be subclassed

Abstract class (2)

public abstract class BaseVehicle implements Vehicle {
    ...
}
BaseVehicle vehicle = new BaseVehicle();
public class Car extends BaseVehicle {
 ...
}

NO

YES

Abstract class (3)

Abstract Classes Compared to Interfaces

Similarities:

Differences:

  • cannot instantiate them
  • in abstract classes can be defined concrete methods
  • abstract classes have fields
  • extend only one abstract class, but implement many interfaces

Abstract class (4)

In the code:

public abstract class BaseVehicle implements Vehicle {

    private PaintColor color;
    private Driver driver;
    private final int initialSpeed;
    private int topSpeed;
    protected int position;

    public BaseVehicle(PaintColor color, Driver driver, 
        int initialSpeed, int topSpeed, int position) {
        ...
    }

    @Override
    public int getPosition() {
        return position;
    }

    ...
}

Constructor

implementation

More getters

Time to code

Implement the missing methods
in Spaceship & Motorcycle

Summary

  1. Interfaces - contracts between developer & the user of the code
  2. Inheritance - Create is a relationship and reuse methods & fields
  3. Abstract classes - can have concrete methods and cannot be instantiated

Daily quiz

Questions?

Thank you :)

Lecture 8 - Interface, Abstract class, Inheritance

By naughtyspirit

Lecture 8 - Interface, Abstract class, Inheritance

  • 359