Appendices

BJava and Pseudocode Quick Reference

On this page
  1. Class template
  2. Inheritance, abstract classes and interfaces
  3. Modifiers at a glance
  4. Overloading vs overriding
  5. Relationships
  6. Pseudocode keywords
  7. Flowchart symbols

Class template

Java
public class Student {                       // class name: PascalCase
    private static int count = 0;            // shared by all objects
    private final String id;                 // set once
    private String name;                     // attributes: private
    private int marks;

    public Student(String name, int marks) { // constructor: no return type
        this.id = "S" + (++count);
        this.name = name;
        setMarks(marks);
    }

    public String getName() { return name; } // getter
    public void setMarks(int marks) {        // setter with validation
        if (marks >= 0 && marks <= 100) this.marks = marks;
    }

    @Override
    public String toString() { return id + " " + name + " " + marks; }
}

Student s = new Student("Nimal", 80);         // create an object
System.out.println(s.getName());              // call a method

Inheritance, abstract classes and interfaces

Java
abstract class Shape {                     // cannot be instantiated
    abstract double area();                 // no body: subclasses must implement
}

interface Drawable {                        // a contract
    void draw();
}

class Circle extends Shape implements Drawable {   // one superclass, many interfaces
    private final double r;
    Circle(double r) { this.r = r; }

    @Override double area() { return Math.PI * r * r; }
    @Override public void draw() { System.out.println("Drawing a circle"); }
}

Shape s = new Circle(2);                    // upcasting
if (s instanceof Circle c) c.draw();        // downcast after a check (Java 16+ pattern)

Modifiers at a glance

ModifierOn a classOn an attributeOn a method
publicVisible everywhereVisible everywhereCallable everywhere
protected(nested classes only)Package and subclassesPackage and subclasses
default (none)Package onlyPackage onlyPackage only
private(nested classes only)Own class onlyOwn class only
static(nested classes only)One copy for the classCalled on the class; no this
finalCannot be extendedAssigned once (constant)Cannot be overridden
abstractCannot be instantiated-No body; subclass must implement

Overloading vs overriding

OverloadingOverriding
WhereSame classSubclass
ParametersMust differMust be the same
Return typeMay differSame, or a subtype
DecidedCompile timeRun time
PolymorphismCompile-time (static)Run-time (dynamic)

Relationships

RelationshipTest sentenceLifetimeUMLExample
InheritanceX is a Y-Hollow triangle at the parentDog → Animal
AssociationX uses YIndependentPlain lineTeacher – Student
AggregationX has Y (shared)Parts outlive the wholeHollow diamond at the wholeDepartment – Teacher
CompositionY is part of XParts die with the wholeFilled diamond at the wholeHouse – Room

Pseudocode keywords

PurposeForm
Start and endBEGIN ... END
Input and outputREAD x, PRINT x
SelectionIF ... THEN ... ELSE ... END IF
Pre-test loopWHILE condition DO ... END WHILE
Counted loopFOR i = 1 TO n DO ... END FOR
Post-test loopREPEAT ... UNTIL condition
ProcedurePROCEDURE name(params) ... END PROCEDURE

Flowchart symbols

StartTerminatorStart and Stoptotal = total + iProcesscalculation or assignmentREAD a, bInput / Outputread or displaya > b ?Decisionyes/no question, two exitsFlow linedirection of flowAConnectorjoins parts of a chart
The standard flowchart symbols