Download presentation
Presentation is loading. Please wait.
Published byJuniper Baldwin Modified over 9 years ago
1
1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel
2
2 Final & Static
3
3 The static Keyword zDifferent (but related) meaning for: yVariables yMethods
4
4 Static: Variables zAll objects in a class share the same copy of a static member (variable). yEven if no objects in that class have yet been instantiated.
5
5 The Static Keyword zStatic methods defined in a class are available even when there are no objects of that class. ySimilar to global functions in C++.
6
6 Static Example //: StaticTest.java public 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
7 Static Example Output ID of a = 1001 ID of b = 1002 ID of c = 1003
8
8 The final Keyword zDifferent (but related) meaning for: yVariables yClasses & Methods
9
9 Final: Variables zA primitive marked final is constant. e.g. final int i = 9; yThis is also a way to create named constants. xBy convention, names in ALL CAPS. xUse final static. zAn 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
10 Final: Methods & Classes zA method marked final cannot be overridden. zA class marked final cannot be extended.
11
11 Parameters
12
12 Parameters zAll Java parameters are called by value. zInside the called method, act like local variables. yValue of the parameter can be changed. yChanges do not affect value in the calling method.
13
13 However… zParameter value might itself be a reference to an object. yChanges to the parameter do not affect value of the reference in the calling method. yValues inside the object referred to can be changed.
14
14 Passing Values Back to the Calling Method zMaximum one value returned as the function value. yCan be a primitive or a reference to an object. zParameter values cannot be changed. yValues within an object referred to by a parameter can be changed.
15
15 Overloading, Overriding, Polymorphism
16
16 Overloading & Overriding zOverloading means: same name is used for multiple things. zOverloading controls which signature (calling sequence) is used. yOrder and type of parameters. yNot returned value.
17
17 Overloading & Overriding II zOverriding means: One version is used in preference to another. zTries to match lowest (most specific) level in class hierarchy. yStart at the class you know, and work up the hierarchy, looking for a signature match.
18
18 What Happens At Compile Time zSignature is determined by what is known at compile time. zIf the type of the variable is a superclass, but the object type is a subclass, the signature has to match the superclass. zJava 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
19 Polymorphism: What Happens at Runtime zPolymorphism means: the type of the object determines which method gets called. zAmong methods with the same signature anywhere in the program, Java needs to call the one appropriate for this particular object.
20
20 Polymorphism II zDynamic binding is determined by what is known at run time. zIf the variable type is a superclass, but the object type is a subclass, the method comes from the subclass. yStart at the class of the object, and work up the hierarchy, looking for a signature match.
21
21 Why? zEach object has its methods attached. zThe author of a method knows what the classes of its parameters do. The class cannot know what future subclasses might do.
22
22 A Restriction zA subclass can override a method with: yAn exact match of parameter type. yA more specific parameter type, i.e. a subclass of the parameter type. xOnly matches the subtype. zA subclass cannot override a method with: yA less specific parameter type.
23
23 Example: PolyOver.java
24
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
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
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 Art parameter.
27
27 PolyOver.java: More Invocations a1.method1((Drawing) a3); a1.method1((Drawing) c1); a1.method1((Art) d2);
28
28 Polyover.java: More Output Calling method1 in class Art with Drawing parameter. Calling method1 in class Art with Art parameter.
29
29 PolyOver.java: Even More Invocations a3.method1(a1); a3.method1((Drawing) a3); a3.method1((Art) d2);
30
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
31 Next Time zStrings zInterfaces zArrays
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.