Lecture 41, CS 302-7 and 8, May 2

  1. Various things
    1. What is and what is not on the exam

                                                               i.     Class hierarchy, inheritance, extends – not on test

                                                             ii.     Objects, interfaces – on test

  1. Review:
    1. Java class hierarchy
    2. Substitution principle
    3. Using extends
    4. Using super
    5. Overriding methods
    6. Dynamic method lookup
    7. Polymorphism
  2. An example:
    1. Car.java, Bike.java, Vehicle.java, VehicleTester.java
  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 a ‘cycle’

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

    1. What does the object class contain/what purpose does it serve?

                                                               i.     Contains very general methods, such as toString and equals

                                                             ii.     Default behavior for these methods:

1.    toString – Class@Address

2.    equals – compares Addresses

                                                            iii.     We will often override these methods with more specific implementations.

                                                           iv.     Even though we should override these methods before using, they serve a purpose, and there’s a reason we override them, instead of making new ones

1.    They are a ‘contract’ in a sense, with all subclasses of object, that they are a valid method name that does something consistent

2.    So, we know that we can call toString from a object of any class, or equals, and we know how to use it, even if we don’t know a thing about the implementation of it

    1. instanceof revisited

                                                               i.     Remember that we can store a subclass object in a superclass variable?

Example – Object o=new Random();

o instanceof Random ->true

                                                             ii.     This is why the instanceof operator is important – it tells us whether the object stored in the variable is the desired type.

                                                            iii.     Useful when converting from superclass to subclass object

1.    We would do this if we want to call subclass methods from an object that was declared generically

                                                           iv.     Don’t use instanceOf to bypass polymorphism – that will be handled already if classes are implemented well

1.    In other words, if possible, define methods generically (as high up as possible)

  1. Interfaces
    1. (my) Definition – A Contract between all classes that implement the interface that they will implement certain methods

                                                               i.     Another level of abstraction – skeleton for code

                                                             ii.     Provides generality to code

    1. (book) Definition - Use interfaces when you’ll have multiple classes implementing the same mechanism, but they have to do it differently
    2. An interface is a type
    3. The only other type we’ve seen up to this point is a Class
    4. An interface type defines:

                                                               i.     A set of methods

                                                             ii.     Their signatures (params, return, etc)

    1. Every class that implements an interface MUST implement each of those methods

                                                               i.     Leaving ‘stub’ methods is ok, though

    1. Interface vs Class

                                                               i.     Methods in an interface are abstract -> have name, params, and return type, but don’t have implementation

                                                             ii.     All methods are public

                                                            iii.     No instance variables

                                                           iv.     No static methods

  1. Defining an interface
    1. Syntax:

public interface <name>{

            <returnType> <methodName>(<params>);

}

    1. A couple of notes:

                                                               i.     Do not put any code in methods

                                                             ii.     Do not say methods are public – they always are

                                                            iii.     No instance variables

                                                           iv.     No static methods

                                                             v.     Pay attention to the semicolon after the method declaration(s).

  1. Homework – For Friday – finish ch 9 if you haven’t yet