Turtle Graphics with Java

Bern University of Teacher Education  
HomeStart Online-EditorPrintAndroid-TurtlegraphicsJava-Online

Inheritance

Complex problems in programming are often split into simpler and shorter parts. A Java-program then mostly consists of multiple classes. There are two basic ways of how these classes are connected:

- Classes are connected hierarchically. This is called inheritance.

- Classes are build out of other classes. This is called composition.

Inheritance is important way of reusing specific program elements. It is based on the idea that parents pass certain attributes (variables) and abilities (methods), which can also be altered (overwritten), to their children. Syntactically inheritance is called with the key word extends. A extended class automatically has all non-private variables and methods of the upper class (superclass).
The following examples give a first impression of this programming technique.


Run this example Tu20.java    
// Tu20.java

import ch.aplu.turtle.*;
import java.awt.Color;

// ------------ class RedTurtle ----------------
class RedTurtle extends Turtle // for Online Editor in same file
                               //  and non-public
{
  public RedTurtle()
  {
    setColor(Color.red);
    setPenColor(Color.red);
  }

  public void stern(double s)
  {
    for (int i = 0; i < 6; i++)
    {
      forward(s);
      right(140);
      forward(s);
      left(80);
    }
  }
}

// ------------ Application class ------------
public class Tu2
{
  public Tu20()
  {
    RedTurtle t = new RedTurtle();
    t.hideTurtle();
    t.stern(50);
    t.setPos(-80-80);
    t.stern(30);
  }

  public static void main(String[] args)
  {
    new Tu20();
  }
}
 


 

The class RedTurtle inherits from the class Turtle. RedTurtle is a Turtle which now is red and draws red lines. In addition it has the new method star().

RedTurtle is a Turtle and so possesses all method of the class Turtle.

The applicational class Tu20 generates an object t of the class RedTurtle and calls the method star().

 

Test this example in the Online-Editor

Explaining the program code:

class Tu20
class RedTurtle

The class RedTurtle must be at the same location as the class Tu20 when compiling. Using the Online-Editor the easiest way to do this is to write the two class codes in the same editor window.

Run this example Tu22.java  
// Tu22.java

import ch.aplu.turtle.*;
import java.awt.Color;

// ------------ class RedTurtle ----------------
class RedTurtle extends Turtle  // for Online Editor in same file
                                // and non-public
{
  public RedTurtle()
  {
    setColor(Color.red);
    setPenColor(Color.red);
  }

  public void stern(double s)
  {
    for (int i = 0; i < 6; i++)
    {
      forward(s);
      right(140);
      forward(s);
      left(80);
    }
  }
}

// ------------ class FillRedTurtle ----------------
class FillRedTurtle extends RedTurtle  // for Online Editor in same file
                                       //  and non-public
{
  public FillRedTurtle()
  {
    setFillColor(Color.red);
  }

  public void stern(double s)
  {
    super.stern(s);
    fill(getX() + 10getY() + 10);
  }
}

// ------------ Application class ------------
public class Tu2
{
  public Tu22()
  {
    FillRedTurtle t = new FillRedTurtle();
    t.hideTurtle();
    t.stern(30);
    t.setPos(7070);
    t.stern(40);
  }

  public static void main(String[] args)
  {
    new Tu22();
  }
}
 


The class StarRedTurtle extends the RedTurtle. It inherits all attributes and methods of the class RedTurtle (Turtle color, pen color, method star()). StarRedTurtle also inherits all methods of the class Turtle. Additionally StarRedTurtle has its own method square(); which makes the Turtle draw a square besides the star.

Test this example in the Online-Editor

 

Modifiers

The access-modifiers public, protected and private are key words which are placed before a declaration of an instance variable, a method or a class setting its visibility (access rights).
private visible only in its own class
public visible everywhere
protected visible in the same package and in the subclasses

If the modifier is missing, the default access right is protected.

If one wants to write in a good programming style it is important to deliberately use the modifiers and so limit the access rights as far as possible. Especially instance variables are strongly limited and mostly declared as private.


Exercises