Head First Java Chapter 7
-
Upload
tom-henricksen -
Category
Software
-
view
166 -
download
0
Transcript of Head First Java Chapter 7
Head First Java Chapter 7Tom Henricksen
Inheritance● Superclass - Fan
● Subclass - SportsFan, MusicFan
○ Override cheer method in SportFan
ArrayList● Subclass inherits
● Subclass extends
○ Add
○ Override
Examplepublic class Doctor {}
public class FamilyDoctor extends Doctor {
makeHouseCalls() {}
}
Inheritance● Find common attributes
● Avoid duplication
● Specific behavior
● Find inheritance opportunity
Testing it out● IS-A
● HAS-A
More inheritance● Access Levels
○ public
○ private
● Advantages
Polymorphism● Reference
○ Animal myDog = new Dog();
● Array of Animals
○ animals[0] = new Cat();
○ animals[1] = new Wolf();
Polymorphism● Polymorphic arguments
○ void giveShot(Animal a) {
● Flexible code
Overriding● Same contract
○ Arguments
○ Compatible return
○ Less accessible
Overloading● Can have different return type
● Change more than return type
● Access level can vary
int addNums(int a, int b) {
double addNums(double a, double b) {
Assignment
● Exercise 1
● Exercise 2
Subclassing with Eclipse
Subclassing
Overriding