Professional Documents
Culture Documents
Java as an Object-oriented
Programming Language
04. Inheritance
Let’s practice writing some classes
2
Employee class
// A class to represent employees
public class Employee {
public int getHours() {
return 40; // works 40 hours / week
}
5
Writing more classes
6
Secretary class
// A class to represent secretaries
public class Secretary {
public int getHours() {
return 40; // works 40 hours / week
}
8
Is-a relationship
Examples:
Every secretary is an employee.
Every square is a rectangle.
Every dog is a mammal.
9
Reusing code: why re-invent the wheel?
code reuse: The practice of writing program code once and using it
in many contexts.
11
Inheritance syntax
Example:
public class Secretary extends Employee
{
....
}
13
Writing even more classes
14
Overriding methods
15
Marketer class
// A class to represent marketers
public class Marketer extends Employee {
public void advertise() {
System.out.println("Act now while supplies last!");
}
return 50000.0
// + e.getSalary(); // $50,000.00 / year
}
}
16
Exercise: LegalSecretary
17
Solution: LegalSecretary
// A class to represent legal secretaries
public class LegalSecretary extends Secretary {
public void fileLegalBriefs() {
System.out.println("I could file all day!");
}
18
Inheritance hierarchies
Indirect
Subclass
Direct
Subclass
19
Exercise: Lawyer
20
Solution: Lawyer
// A class to represent lawyers
public class Lawyer extends Employee {
// overrides getVacationForm from Employee class
public String getVacationForm() {
return "pink";
}
21
Object class
Some Questions about Inheritance
23
The Object Class
Method Description
String toString() Returns a String description of the object.
boolean equals(Object other) Returns true if this and other point to the
same address.
Object clone() Returns a copy of this.
Class getClass() Returns a Class object representing the data
type (Class) of this.
Several others See Java API documentation …
import java.util.Arrays;
…
public String toString()
{
return "Student list: " +
Arrays.toString(students);
}
Overriding the equals method
A shallow comparison
checks to see if two objects have fields with the same
references:
this.students == other.students
A deep comparison
checks to see if two objects have fields with the same
contents:
this.students.equals(other.students)
Exercise: Overriding clone()
35
Constructors of subclasses
38
Access Specifier Example
40
Access Specifier Example, continued
If we want subclasses (and nothing else) to see
something, make it protected:
public class Employee {
protected double salary = 40000.00;
public int getHours() {
return 40; // works 40 hours / week
}
public double getSalary() {
return salary;
}
public int getVacationDays() {
return 10; // 2 weeks' paid vacation
}
public String getVacationForm() {
return "yellow"; // use the yellow form
}
}
41
Access Specifier Example, continued
42
What would happen if ....
CEO still has its own copy of the salary field, and this
code will change the value of it appropriately.
The fact that salary is private simply means that CEO
can't access it directly. It can still call public (or protected)
superclass methods that can access it.
44
final
45
Motivation
46
final Example
// A class to represent employees
public class Employee {
public int getHours() {
return 40; // works 40 hours / week
}
The final keyword can be applied to fields (as we’ve seen before):
// no code may change the value of salary,
//including a subclass’s code
public final double salary = 40000.0;
Or to methods:
// subclasses cannot override the getSalary method
public final double getSalary() { return salary; }
Or even to classes:
// the Employee class cannot be extended
// It can’t have any subclasses at all!
public final class Employee { ... }
48
Exercise: Update the Employee
class
50
Opposite of final
Completely abstract.
57
Going full abstract
interface Researcher {
void discover();
void publish();
}
60
Using Interface Objects
public static void researchCycle(Researcher r) {
r.discover();
r.publish();
}
researchCycle(researcher1);
researchCycle(researcher2);
}
61
Using Interfaces
63
Implementing Interfaces
public class Engineer implements Researcher {
public void discover() {
System.out.println(“Whoa, what did I just do?“);
}
64
Creating Custom Interfaces
public interface Edible {
/** Describe how to eat */ class abstract Fruit
public String howToEat(); implements Edible {
} }
65
Implements Multiple Interfaces
66
Creating Custom Interfaces, cont.
public interface Edible {
/** Describe how to eat */
public String howToEat();
}
67
Exercise: Comparable interface