Lecture 41, CS 302-6, December 09

  1. Review:
    1. Class Hierarchy continued
    2. Overriding and Overloading methods/constructors

                                                               i.     Override – when a subclass implements a method that is also defined in a super class

1.    Examples – toString(), equals(Object obj)

                                                             ii.     Overload – when a method shares a name with another in the same class, but has different parameters (making it an entirely unique method)

1.    Examples – random.nextInt(), random.nextInt(int range)

    1. The ‘super’ reserved word

                                                               i.     Method – super.<method>();

                                                             ii.     Constructor – super();

    1. Polymorphism
  1. 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 – for instance, A extends B, B extends C, C extends A

                                                           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

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

  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. Interface type

                                                               i.     The only other type we’ve seen before this is Class

    1. 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. You can also include constants in your interface declarations – Syntax:

<type> <CONSTANT_NAME>=<value>;

                                                               i.     Note – these are always public static final, even though we don’t explicitly say so

  1. Using interfaces
    1. “implements” reserved word

                                                               i.     Like extends, Class implements Interface

    1. Define all methods

                                                               i.     Reminder again – stubs are ok

    1. Then, in your code, you can treat the interface type kind of like a superclass:

<InterfaceName> o=new <ClassThatImplementsInterface>();

    1. Thus, you know that object must implement the behavior defined in the interface
  1. Homework – For Monday – work on Program 4