Unit 5 Classes
- Class Naming
- Static variables
- Main and Tester Method
- Overloading and Overriding
- Abstract Class and Method
- Homework
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 Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
// Animal myObj = new Animal(); will cause an error
}