1 cs2136: paradigms of computation class 14: static & final parameters overloading, overriding,...

31
1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel

Upload: juniper-baldwin

Post on 18-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

1

CS2136:Paradigms of Computation

Class 14:Static & FinalParameters

Overloading, Overriding,and Polymorphism

Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel

Page 2: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

2

Final & Static

Page 3: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

3

The static Keyword

Different (but related) meaning for: Variables Methods

Page 4: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

4

Static: Variables

All objects in a class share the same copy of a static member (variable). Even if no objects in that class have yet

been instantiated.

Page 5: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

5

The Static Keyword

Static methods defined in a class are available even when there are no objects of that class. Similar to global functions in C++.

Page 6: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

6

Static Example

//: StaticTest.javapublic class StaticTest { // Note: No constructor! // Main program public static void main(String[] args) { ST a = new ST(); ST b = new ST(); ST c = new ST(); System.out.println("Id of a = " + a.getId()); System.out.println("Id of b = " + b.getId()); System.out.println("Id of c = " + c.getId()); }}

class ST { static long nextId =

1001; // Initial value long id; // instance variable

ST() { id = nextId ++; }

long getId() { return id; }}

Page 7: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

7

Static Example Output

ID of a = 1001ID of b = 1002ID of c = 1003

Page 8: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

8

The final Keyword

Different (but related) meaning for: Variables Classes & Methods

Page 9: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

9

Final: Variables

A primitive marked final is constant. e.g. final int i = 9; This is also a way to create named

constants.By convention, names in ALL CAPS.Use final static.

An object reference marked final cannot be changed to a different object, but the object can be modified. e.g. final Foo x = new Foo(5);

Page 10: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

10

Final: Methods & Classes

A method marked final cannot be overridden.

A class marked final cannot be extended.

Page 11: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

11

Parameters

Page 12: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

12

Parameters

All Java parameters are called by value.

Inside the called method, act like local variables. Value of the parameter can be changed. Changes do not affect value in the

calling method.

Page 13: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

13

However…

Parameter value might itself be a reference to an object. Changes to the parameter do not affect

value of the reference in the calling method.

Values inside the object referred to can be changed.

Page 14: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

14

Passing Values Back to the Calling Method

Maximum one value returned as the function value. Can be a primitive or a reference to an

object.Parameter values cannot be

changed. Values within an object referred to by a

parameter can be changed.

Page 15: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

15

Overloading,Overriding,

Polymorphism

Page 16: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

16

Overloading & Overriding

Overloading means: same name is used for multiple things.

Overloading controls which signature (calling sequence) is used. Order and type of parameters. Not returned value.

Page 17: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

17

Overloading & Overriding II

Overriding means: One version is used in preference to another.

Tries to match lowest (most specific) level in class hierarchy. Start at the class you know, and work

up the hierarchy, looking for a signature match.

Page 18: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

18

What HappensAt Compile Time

Signature is determined by what is known at compile time.

If the type of the variable is a superclass, but the object type is a subclass, the signature has to match the superclass.

Java doesn’t know what type of object will be in that variable later; it just needs to know that a method matching that signature will exist.

Page 19: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

19

Polymorphism: What Happens at Runtime

Polymorphism means: the type of the object determines which method gets called.

Among methods with the same signature anywhere in the program, Java needs to call the one appropriate for this particular object.

Page 20: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

20

Polymorphism II

Dynamic binding is determined by what is known at run time.

If the variable type is a superclass, but the object type is a subclass, the method comes from the subclass. Start at the class of the object, and work

up the hierarchy, looking for a signature match.

Page 21: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

21

Why?

Each object has its methods attached.

The author of a method knows what the classes of its parameters do. The class cannot know what future subclasses might do.

Page 22: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

22

A Restriction

A subclass can override a method with: An exact match of parameter type. A more specific parameter type, i.e. a

subclass of the parameter type.Only matches the subtype.

A subclass cannot override a method with: A less specific parameter type.

Page 23: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

23

Example: PolyOver.java

Art

Drawing

Cartoon

Page 24: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

24

PolyOver.java: Methods

class Art {void method1(Art aa) { // Will be overridden System.out.println( "Calling method1 in class Art with Art parameter."); } void method1(Drawing dd) { // Will be overridden System.out.println( "Calling method1 in class Art with Drawing

parameter."); }… }

Page 25: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

25

PolyOver.java: Invocations

Art a1 = new Art();Art a2 = new Art();Drawing d1 = new Drawing();Drawing d2 = new Drawing();Cartoon C1 = new Cartoon();Art a3;

a1.method1(a2);a1.method1(d1);a1.method1(c1);a3 = d1;a1.method1(a3);

Page 26: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

26

Polyover.java: Output

Calling method1 in class Art with Art parameter.

Calling method1 in class Art with Drawing parameter.

Calling method1 in class Art with Drawing parameter.

Calling method1 in class Art with Art parameter.

Page 27: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

27

PolyOver.java:More Invocations

a1.method1((Drawing) a3); a1.method1((Drawing) c1); a1.method1((Art) d2);

Page 28: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

28

Polyover.java: More Output

Calling method1 in class Art with Drawing parameter.

Calling method1 in class Art with Drawing parameter.

Calling method1 in class Art with Art parameter.

Page 29: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

29

PolyOver.java:Even More Invocations

a3.method1(a1);a3.method1((Drawing) a3);a3.method1((Art) d2);

Page 30: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

30

Polyover.java: Even More Output

Calling method1 in class Drawing with Art parameter.

Calling method1 in class Drawing with Drawing parameter.

Calling method1 in class Drawing with Art parameter.

Page 31: 1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J

31

Next Time

StringsInterfacesArrays