head first java chapter 7

Post on 17-Feb-2017

166 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

top related