Bright Java Tutorial

is brought to you by DoITBright LLC

Java

The Extends Keyword

We might wish to derive a class from another class due to their similarities. For example, we have a class named 'FuelCar'. It runs on gas. Then, we have another class named 'ElectricCar' which runs on electricity. We can say that both are very similar. Their difference might just be the source of power to run their engine. So, we might want to derive both 'FuelCar' and 'ElectricCar' from a class named 'Car' since 'FuelCar' is a car and 'ElectricCar' is a car. To accomplish this, we use the 'extends' keyword. This is what we call inheritance in Java.

In your Eclipse, click on the package 'com.example.core'. Then, on the tab, click on 'File', click on 'New', click on 'Package'. Enter the package name 'com.example.core.oop1'. Create the class 'Car' inside that package.

  package com.example.core.oop1;

  /**
   * @author Rolan Liwanag
   *
   */
  public class Car {

    public String make;
    public String model;
    
    public void accelerate() {
      System.out.println("Vrrroooom!");
    }
  }
                           
Create the class 'FuelCar' under the same package.

  package com.example.core.oop1;

  /**
   * @author Rolan Liwanag
   *
   */
  public class FuelCar extends Car {

    public int fuelLevel;
    
    public static void main(String[] args) {
      FuelCar fc = new FuelCar();
      /*
       * The String make and String model are not declared
       * FuelCar class. Since FuelCar is a child class
       * of Car, these properties were inherited.
       */
      System.out.println(fc.make + " " + fc.model);
      /*
       * The accelerate method was inherited from Car.
       */
      fc.accelerate();
    }
  }
                            
The above code means 'FuelCar' is a 'Car'. This also means, 'FuelCar' inherits methods and properties from 'Car'. Let us create another class in the same package and name it as 'ElectricCar'.

  package com.example.core.oop1;

  /**
   * @author Rolan Liwanag
   *
   */
  public class ElectricCar extends Car {

    public int chargeLevel;
    
    public static void main(String[] args) {
      ElectricCar ec = new ElectricCar();
      /*
       * The String make and String model are not declared
       * ElectricCar class. Since ElectricCar is a child 
       * of Car, these properties were inherited.
       */
      System.out.println(ec.make + " " + ec.model);
      /*
       * The accelerate method was inherited from Car.
       */
      ec.accelerate();
    }
  }
                            
The above code means 'ElectricCar' is a 'Car'. This also means, 'ElectricCar' inherits methods and properties from 'Car'. However, this does not mean 'ElectricCar' is a 'FuelCar' nor 'FuelCar' is an 'ElectricCar'. A class can only extend one class. This means, Java only allows single inheritance. Another thing to note is an abstract class. This kind of class is meant to be extended by another class. The reason is, abstract classes cannot be instantiated. Let us create a class and name it as 'Vehicle'.

  package com.example.core.oop1;

  /**
   * @author Rolan Liwanag
   *
   */
  public abstract class Vehicle {
    
    /*
     * When a class contains a method that is declared as
     * abstract, the class itself is now forced to be 
     * declared as abstract. The extending class is forced
     * to provide implementation to the abstract method.
     *
     * We can declare a class as abstract even without
     * any abstract method.
     */
    public abstract void accelerate();
    
    public void aMethod() {
      System.out.println("Just a method");
    }
  }
                           
Let us try to create another class that will extend the 'Vehicle' class. Let us call it as 'Truck'.

  package com.example.core.oop1;

  /**
   * @author Rolan Liwanag
   *
   */
  public class Truck extends Vehicle {
    
    /*
     * Truck needs to provide implementation for
     * the accelerate method of Vehicle since
     * Truck extended the class Vehicle.
     */
    public void accelerate() {
      System.out.println("Implementation of accelerate.");
    }
    
    public void aMethod() {
      System.out.println("Just a method");
    }
  }
                           


The Implements Keyword

An 'Interface' is like a contract on how to interact with it. An interface needs to be implemented by a class or extended by another interface. In an interface, the methods do not have implementations. The variables that are declared are considered constants. Interfaces cannot be instantiated.

  package com.example.core.oop1;

  /**
   * @author Rolan Liwanag
   *
   */
  public interface ExampleInterface {
    
    public void aMethod();
    
  }
                            


  package com.example.core.oop1;

  /**
   * @author Rolan Liwanag
   *
   */
  public class ExampleClass implements ExampleInterface {
    
    /*
     * The moment ExampleClass implemented the 
     * ExampleInterface, it became a must to provide
     * implementation to the aMethod.
     */
    public void aMethod() {
      System.out.println("Implementation of aMethod.");
    }
    
    public static void main(String[] args) {
      ExampleClass ec = new ExampleClass();
      ec.aMethod();
    }
    
  }
                            
We can also think of interfaces as a way of Java trying to mimic multiple inheritance.


Back    Next