Benefits: modularity, easier to test and maintain, data integrity and validation, flexibility in refactoring
Use keyword extends to extend a parent class and create a subclass
super(params) in subclass constructor (needs to be first line) to satisfy parent class constructor requirementssuper.method() or super.variableName to access public or protected members of parent classcareful use of privacy:
public “how outsiders use my family”protected “provided to my children”private “I’ll handle this myself”Gives structure and design to children
Designed with polymorphism in-mind
toString() and equals(Object obj))instanceof – evaluates to true if and only if reference is a member of Type
We use instanceof together with type casting
Converts a parent-class-reference (storing a child class) to a child-class reference (no actual conversion) – purpose is to call methods in subclass
You have 10 minutes to complete the quiz.
Complete the body of three methods in subclass: StudentTicket constructor, setDiscount(double discount) with validation, and getPrice() (with calculated discount)