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 methodInheritance, 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
| Modifier | On a class | On an attribute | On a method |
|---|---|---|---|
public | Visible everywhere | Visible everywhere | Callable everywhere |
protected | (nested classes only) | Package and subclasses | Package and subclasses |
| default (none) | Package only | Package only | Package only |
private | (nested classes only) | Own class only | Own class only |
static | (nested classes only) | One copy for the class | Called on the class; no this |
final | Cannot be extended | Assigned once (constant) | Cannot be overridden |
abstract | Cannot be instantiated | - | No body; subclass must implement |
Overloading vs overriding
| Overloading | Overriding | |
|---|---|---|
| Where | Same class | Subclass |
| Parameters | Must differ | Must be the same |
| Return type | May differ | Same, or a subtype |
| Decided | Compile time | Run time |
| Polymorphism | Compile-time (static) | Run-time (dynamic) |
Relationships
| Relationship | Test sentence | Lifetime | UML | Example |
|---|---|---|---|---|
| Inheritance | X is a Y | - | Hollow triangle at the parent | Dog → Animal |
| Association | X uses Y | Independent | Plain line | Teacher – Student |
| Aggregation | X has Y (shared) | Parts outlive the whole | Hollow diamond at the whole | Department – Teacher |
| Composition | Y is part of X | Parts die with the whole | Filled diamond at the whole | House – Room |
Pseudocode keywords
| Purpose | Form |
|---|---|
| Start and end | BEGIN ... END |
| Input and output | READ x, PRINT x |
| Selection | IF ... THEN ... ELSE ... END IF |
| Pre-test loop | WHILE condition DO ... END WHILE |
| Counted loop | FOR i = 1 TO n DO ... END FOR |
| Post-test loop | REPEAT ... UNTIL condition |
| Procedure | PROCEDURE name(params) ... END PROCEDURE |