Lecture 14b - T ype Compatibility and Conversion Java is...

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

View Full Document Right Arrow Icon
Type Compatibility and Conversion Java is strongly typed o Checks for type errors at compile time A class defines a custom data type o A superclass defines a supertype o A subclass defines a subtype o Eg: if Circle extends Shape, Shape is the supertype and circle is the subtype You can assign a subtype reference to a supertype reference o Assigning a circle TO a shape eg: Shape myShape; Circle myCircle = new Circle(); ... myShape = myCircle; o This is a widening conversion Also called an upcast or safe cast Remember, a circle is a kind of shape (polymorphism) o Though not needed, you can use an explicit cast myShape = (Shape)myCircle; o Ordinarily, you cannot assign a supertype reference to a subtype reference Eg Shape myShape = new Circle(); Circle myCircle;
Background image of page 1

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

View Full DocumentRight Arrow Icon
... myCircle = myShape; // won’t compile o Can be forced with a cast myCircle = (Circle)myShape; o This is a narrowing conversion Also called a downcast or unsafe cast o If the cast is not valid, it may be detected at compile time (compilation
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/04/2010 for the course CPSC 233 taught by Professor Li during the Fall '09 term at University of Calgary.

Page1 / 4

Lecture 14b - T ype Compatibility and Conversion Java is...

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