CSCI111: Chapter 8

CSCI111: Notes Chapter 6 and 8
Inheritance and OO Design


Inheritance

We have already talked about inheritance previously, so most of Chapter 8 should not be new. I suggest you read it to make sure!

Using inheritance

extends

overriding "parents" (super's) variables and methods.       See Panel for API info available concerning inheritance

Scope

methods, instance, super, etc (private and public)

Class Hierarchy

notice extend Applet (GUI)

what do most Models of applications extend?

Classes (and programs in general) might import a lot, but probably not a lot of hierarchy

Some (pretty good) slides for Ch. 8 about inheritence and classes from the text. You should be able to do the exercises at the back of the chapter. In these slides, I had gone too quickly over these issues

Object Models

Show structure chart page

You will need to start making Object Models in lab 5

polymorphism

  1. overload methods in same class - lots of possibilities BUT in Java "a class cannot contain two methods with the same name and same number and type of parameters, even if the methods have different return types."
  2. override supers
  3. reuse same name in different classes

Casting

Do not worry about this too much...you will know when you need it. You probably won't need it until CSCI 311.

Below, Cow is a subclass of Animal. Which knows more?

This is an example of type promotion. animal is what daisy is BUT it will only have access to the facts about Animals since it is delared as an Animal.
daisy remains a Cow
The only reason that animal can be cast as a Cow, is that animal was initially created with
Animal animal = daisy;
and daisy was a cow. Normally one can cast up the hierarchy (a Cow can be cast as an Animal) but you can only cast down (to subclasses) if the object was initially one of those things.
Again, think about this....why would this be so?

super

Wrap-around methods also seen in Constructors ...but just a call to super()...

Inheriting from Class Libraries

Don't forget to import

final

Cannot subclass a final class
Cannot override a final method
Cannot change a final variable

final static

A final static variable is a constant that can be seen by other classes We saw in lab 4 Math.PI

Don't forget to import or give full name (java.lang.Math.PI)


Slides for Chapter 6 and Chapter 8