RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lect14a
inheritance: intro
and, course evals

Digsy the Dog goes "woof, woof".
Caleb the Cat goes "meow, meow".
Python the Python goes "ssss, ssss".
Phydeaux the Dog goes "yip, yip".

How would we represent the above? Probably with a class Animal, whose fields are ...? Whose methods are?
But what if we also wanted some methods and fields specific to certain types of animals — perhaps Cats have a field "clawSharpness" and a method "pounce"; perhaps Dogs have a emthod "wagTail" (something that pythons and cats are actually incapable of?

That's all well and good, but what if we had then and other animals we wanted to model? Say, cats and pythons, which both have ages and sounds, and can getAge and speak … but we will still want separate classes, since some activities are specific to Dogs (perhaps getAgeInDogYears()).

One solution would be lots of repeated code for speak. But (for the first time)1 we'll see what object-oriented programming buys for us: the ability to share code between different (related) classes.

We will add a new class Animal, and put the code which is common to all animals there. Then, we will tell Java that class Cat extends Animal etc..


Some further reading, if you are interested: some previous-semester's notes and notes, continued

1Every single method we've written this semester could have been made static, if we wanted! We would just have to make one superficial change to each method: add another parameter explicitly -- named this. Then when calling the method, rather than saying mySavings.deposit(12.00), we'd just have called deposit(mySavings, 12.00). With this simple transformation, we could have done everything this semester with static methods (though we'd still need non-static fields).      

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Dec.07 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme