Introductie tot objectgeoriënteerd programmeren in Java
Sani Yusuf
Lead Software Engineering Content Developer
// Car class
class Car {
// Battery capacity is only applicable
// to electric cars
public int batteryCapacity;
void drive() {
}
}
// Toyata class class Toyota extends Car { void drive(){ } }// Man class public static class Main { public static void main(String[] args) { Toyota myToyota = new Toyota(); // All cars will inherit the "batteryCapacity" // even if they are not electric cars System.out.println( myToyota.batteryCapacity); // Niet van toepassing } }
implements
// ElectricCar Interface
interface ElectricCar {
}
// Tesla die de
// ElectricCar-interface implementeert
class Tesla implements ElectricCar {
}
UPPER_SNAKE_CASEpublic static final
interface ElectricCar {
// Implictly public static final
// Cannot be changed
int BATTERY_CAPACITY = 310;
}
abstract zonder implementatieabstract-methoden moeten in de subklasse worden geïmplementeerdinterface ElectricCar { // Implictly public static final // Cannot be changed int BATTERY_CAPACITY = 310;// Dit is standaard een abstracte methode void charge();// Dit is een concrete methode void autoPark() { }}class Tesla implements ElectricCar { // Moet worden geïmplementeerd public void charge() { } }
Introductie tot objectgeoriënteerd programmeren in Java