4 Interface and types - 30/08/2010 1 MORE ON SUBCLASSES,...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 30/08/2010 1 MORE ON SUBCLASSES, INHERITANCE, INTERFACES, ETC Lecture 4 CS2110 Fall 10 Primitive vs Reference Types Primitive types int, short, long, float, byte, char, boolean, double Efficient 1 or 2 words Not an Objectunboxed 57 abc Reference types Objects and arrays String, int, HashSet Usually require more memory Can have special value null Can compare null with ==, != Generates NullPointerException if you try to dereference null abc nonzero 57 val null next Comparing/copying primitive types Works just as you would expect int a, b; if(a < b) { } a = b+3; Comparing/Copying Reference Types Comparing objects (or copying them) isnt easy! You need to copy them element by element Compare objects using the equals method, which implements deep equality What you wrote How to write it correctly "xy" == "xy" xy".equals("xy") "xy" == "x" + "y" "xy".equals("x" + "y") xy" == new String("xy) "xy".equals(new String("xy")) Inheritance A subclass inherits the methods of its superclass Example: methods of the Object superclass: equals(), as in A.equals(B) toString(), as in A.toString() others well learn about later in the course every object thus supports toString()! Overriding A method in a subclass overrides a method in superclass if: both methods have the same name, both methods have the same signature (number and type of parameters and return type), and both are static methods or both are instance methods Methods are dispatched according to the runtime type of the actual, underlying object 30/08/2010 2 Shadowing Like overriding, but for fields instead of methods Superclass: variable v of some type Subclass: variable v perhaps of some other type Method in subclass can access shadowed variable using super.v Variable references are resolved using static binding (i.e., at compile-time), not dynamic binding (i.e., not at runtime) Variable reference r.v uses the static (declared) type of the variable r , not the runtime type of the object referred to by r Shadowing variables is bad medicine and should be avoided a nasty example class A { int i = 1; int f() { return i ; } } class B extends A { int i = 2; // Shadows variable i in class A. in f() { return ; } // Overrides method f in class A int f() { return- i ; } // Overrides method f in class A. } public class override_test { public static void main( String args ) { B b = new B(); System. out .println( b.i ); // Refers to B.i; prints 2. System. out .println( b .f()); // Refers to B.f(); prints -2. A a = (A) b ; // Cast b to an instance of class A. System.out.println( a.i ); // Now refers to A.i; prints 1; System.out.println(a.f()); // Still refers to B.f(); prints -2; } } The runtime type of a is B!...
View Full Document

Page1 / 8

4 Interface and types - 30/08/2010 1 MORE ON SUBCLASSES,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online