1 cs2136: paradigms of computation class 14: static & final parameters overloading, overriding,...
Post on 18-Jan-2016
229 Views
Preview:
TRANSCRIPT
1
CS2136:Paradigms of Computation
Class 14:Static & FinalParameters
Overloading, Overriding,and Polymorphism
Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel
2
Final & Static
3
The static Keyword
Different (but related) meaning for: Variables Methods
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.
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++.
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; }}
7
Static Example Output
ID of a = 1001ID of b = 1002ID of c = 1003
8
The final Keyword
Different (but related) meaning for: Variables Classes & Methods
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);
10
Final: Methods & Classes
A method marked final cannot be overridden.
A class marked final cannot be extended.
11
Parameters
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.
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.
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.
15
Overloading,Overriding,
Polymorphism
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.
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.
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.
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.
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.
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.
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.
23
Example: PolyOver.java
Art
Drawing
Cartoon
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."); }… }
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);
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.
27
PolyOver.java:More Invocations
a1.method1((Drawing) a3); a1.method1((Drawing) c1); a1.method1((Art) d2);
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.
29
PolyOver.java:Even More Invocations
a3.method1(a1);a3.method1((Drawing) a3);a3.method1((Art) d2);
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.
31
Next Time
StringsInterfacesArrays
top related