Lesson 4
Lesson 4
Java Subclasses
● Recap
● Code we’ve written in one class can be reused in another class without the need of
repeating/rewriting the same code.
● The class inheriting from the parent is known as the subclass while the parent class
itself is known as the superclass.
○ Default constructor that initializes all the values to blank or default values.
○ Argument constructor that initializes values based on the arguments to the constructor.
○ Default constructor that initializes the Phone and SmartPhone variables to blank/default values.
○ Getters and setters for the above variables of the SmartPhone class.
○ Argument constructor that takes in values for both the Phone and SmartPhone variables.
○ Bonus: int [ ] array value for holding the device’s resolution (width and height
Overriding /
overloading
class
methods
Overriding Java methods
Animal
public void speak() {
● Overloaded methods differ in the number, type and order of arguments (if
any).
Overloading Java methods
public class Person {
...
private String name;
○ Override the showDetails method (inherited from the parent class Phone).
○ Have the overridden showDetails method first call its superclass version.
○ Have the overridden showDetails method display the remaining variables inside the SmartPhone subclass.
○ Getter method for the memory storage size to accept in a boolean value named useMegabytes.
○ The overridden method should return a size in megabytes (if the useMegabytes value is true, or return the
original size in gigabytes (if useMegabytes is false
The End...
Questions?