Class Naming

When creating java classes, it is important to have a good name for your class. The name of the class should

  • have words that are relevant to the code
  • aren't similar to another class name where they might be confused
  • not single letter

Static variables

Static variables or class variables are in a class and is only initialized once at the beginning of the execution. It belongs to the class, not the object.

Main and Tester Method

Main and tester methods are used hold a collection of code to perform certain tasks. The main will create the code and the tester will actually run the code to test the result.

A class can be extended with the extends keyword which inherits a class from a superclass. A subclass will be the one that inherits from the superclass.

Overloading and Overriding

Method overloading lets a class have more than one method with the same name, but with different parameters. Overriding occurs when the subclass has the same method as the superclass.

Abstract Class and Method

An abstract class is a class that is restricted so it can't be used to make objects. An abstract method can only be in an abstract class and doesn't have a body.

abstract class Animal {
    public abstract void animalSound();
    public void sleep() {
      System.out.println("Zzz");
    }
    // Animal myObj = new Animal(); will cause an error
  }

Homework

Homework