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
You have 10 minutes to complete the quiz.
truetoString() 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
Used for: representing parent classes that exist only for being a parent class.
Syntax: abstract (class keyword)
We cannot make instances of this class (cannot use “new” keyword)
Method that exist only to provide structure
Syntax: list modifier abstract with the method
abstract classesThe greeter classes represent various ways to greet someone
getName, greetname – should be handled well (set in constructor)Things to note:
Greet.java
public class Greet {
public static void main(String[] args) {
Greeter boss = new Formal("Ms.", "Silva");
boss.greet();
Greeter friend = new Informal("Joe");
friend.greet();
}
}Good day, Ms. Silva.
What's up, Joe?
Greeter.javaFormal.javaInformal.javaGoal: Create a payment system, with an abstract class called Payment and subclasses CreditCard and PayPal