Lecture 40, CS 302-7 and 8, April 30

  1. Various things
    1. Program 4

                                                               i.     Emphasize class structure, comments (grading)

1.    15% OOP, 10% Commenting – we don’t provide shell

                                                             ii.     Partners - by Friday

    1. Last codelab posted
    2. Final exam info posted

                                                               i.     Sample questions

                                                             ii.     Last spring’s exam

  1. Review:
    1. Building our own exceptions

                                                               i.     Extends clause

                                                             ii.     public class <className> extends <className>

                                                            iii.     Extend either Exception or Runtime Exception, depending on whether you want it to be checked or unchecked.

    1. Class Hierarchy

                                                               i.     Inheritance – relationship between classes

                                                             ii.     Subclasses/children and Superclasses/parents

                                                            iii.     The role that extends plays in this – defining a subclass

1.    Question – why do we define the inheritance relationship in the child instead of the parent?

2.    Answer – what if you add your own child class – would you want to have to change the parent too?

    1. Substitution principle

                                                               i.     If your code calls for a superclass, you can use one of its subclasses.

1.    Vehicle/car

                                                             ii.     More specific classes can still use methods from their parent classes

1.    Vehicle/car

    1. Overriding and Overloading

                                                               i.     Overriding methods

1.    Vocabulary – overloading vs overriding

2.    Overriding

a.    We override the default getMessage method in exception classes

b.    Another example – printStackTrace()

3.    Overloading

a.    Overloading constructors

b.    Overloading methods

  1. Super
    1. Calling methods/accessing superclass variables
    2. New reserved word – super

                                                               i.     Use for accessing methods from super class that are overridden

1.    super.method();

                                                             ii.     Superclass constructors

1.    super(params);

  1. Dynamic method lookup
    1. Method calls are determined by type of object, not variable
    2. But, can only call methods defined by type of variable
  2. Polymorphism
    1. Ask different objects to carry out same task – each does it in its own way
  3. The Object class
    1. As mentioned previously – every class in Java that is declared without an extends clause implicitly extends the Object class

                                                               i.     Remember – a class can only extend one other class.

                                                             ii.     But, if class C extends class B, class B either extends another class, or Object, and so forth

1.    Direct vs indirect superclass

                                                            iii.     Cyclic class inheritance error – if we try to make an inheritance ‘cycle’

                                                           iv.     Thus, Object is a superclass for every class – top of the tree, so to speak

  1. Note – material from this part of chapter 9 will not be on the exam
  2. Homework – For Wednesday – Read ch. 9.5-6