Week6 PDF
Week6 PDF
casting
CST8284
protected keyword
CST8284
wish to create HourlyEmployee and SalariedEmployee each with specific getPay() method later, there may be more subclasses with different getPay() can we "postpone" definition of getPay() until later subclasses solution: declare method getPay() as an "abstract method"
using the abstract keyword public abstract double getPay(); but still ensure it gets coded ? but no getpay() in Employee
CST8284
abstract method
abstract class
a class with 1 or more abstract methods only used for inheritance can not be instantiated imposes coding requirement on subclass(es) if not coded, subclass is also abstract
CST8284
so a class that has at least one abstract method is called an abstract class
and must be defined with abstract keyword
public abstract class Employee { ... public abstract double getPay(); ... }
CST8284
abstract class can have mix of abstract and fully defined methods If subclass of abstract class does not define bodies for all of abstract methods
"concrete" class
then it is abstract also and use must use abstract in its defintion
CST8284
or compiler will call superclass no-arg constructor common use: var of type (abstract) superclass to refer to subclass
Employee e = new HourlyEmployee(); List myList = new ArrayList();
CST8284
CST8284