0% found this document useful (0 votes)
13 views13 pages

Lesson 4

This document provides an overview of Java subclasses, focusing on inheritance and method overriding/overloading. It includes exercises for creating a Phone class and a SmartPhone subclass, detailing required properties and methods. Additionally, it explains how to override and overload methods in Java, with examples for clarity.

Uploaded by

melatdagnu1999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views13 pages

Lesson 4

This document provides an overview of Java subclasses, focusing on inheritance and method overriding/overloading. It includes exercises for creating a Phone class and a SmartPhone subclass, detailing required properties and methods. Additionally, it explains how to override and overload methods in Java, with examples for clarity.

Uploaded by

melatdagnu1999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lesson 4

Java Subclasses
● Recap

Overview ● Java subclasses


Recap
● Declare Java methods inside a class.

● Using methods to read/retrieve variables (getter methods).

● Using methods to write/change variables (setter methods).

● Calling parent objects method exercise


Java subclasses
● Allows us to create parent-child relationships through inheritance. (Is A)

● Code we’ve written in one class can be reused in another class without the need of
repeating/rewriting the same code.

● Allows us to re-define parent class methods inside the subclass methods.

● The class inheriting from the parent is known as the subclass while the parent class
itself is known as the superclass.

○ E.g.: A Duck class as a child (subclass) of the Animal


Java subclasses

public class Animal {


private String name; Superclass
...
}

public class Duck extends Animal {


public void quack() {
... Subclass
}
}
Class Demo/Exercises (Part አንድ)
● Create a class called Phone with the following properties:

○ String variable to represent the device’s make.

○ String variable to represent the device’s model.

○ Getters and setters for the above variables.

○ Default constructor that initializes all the values to blank or default values.

○ Argument constructor that initializes values based on the arguments to the constructor.

○ Constructor that accepts in arguments (name, mode).

○ Method called showDetails


Class Demo/Exercises (Part ሁለት)
● Create a subclass of Phone called SmartPhone with:

○ Double variable to hold the memory storage size (in gigabytes)

○ String variable to hold the Operating System name (Android/iOS/Windows Phone)

○ 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() {

Cat extends Animal Human extends Animal

public void speak() { public void speak() {


System.out.println("Meow!"); System.out.println("Hello");
} }
Overloading Java methods
● Java allows us to define multiple methods with the same name doing slightly
different things. This is called overloading.

● Overloaded methods differ in the number, type and order of arguments (if
any).
Overloading Java methods
public class Person {
...
private String name;

public void speak() {


System.out.println(name);
}
public void speak(String text) {
System.out.println(text);
}
...
}
Class Demo/Exercises (Part ሶስት)
● Inside our SmartPhone class:

○ 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.

● Overload the following method of SmartPhone class:

○ 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?

You might also like