Reference type conversions - 1

"And now, a short lesson from Diego. Brief and to the point. About reference type conversions."

"Let's start with Object variables. You can assign any reference type to such a variable (widening conversion). However, to make the assignment in the other direction (narrowing conversion), you must explicitly indicate a cast operation:"

Code Description
String s = "mom";
Object o = s; // o stores a String
A typical widening reference conversion
Object o = "mom"; // o stores a String
String s2 = (String) o;
A typical narrowing reference conversion
Integer i = 123; // o stores an Integer
Object o = i;
Widening conversion.
Object o = 123; // o stores an Integer
String s2 = (String) o;
Runtime error!
You cannot cast an Integer reference to a String reference.
Object o = 123; // o stores an Integer
Float s2 = (Float) o;
Runtime error!
You cannot cast an Integer reference to a Float reference.
Object o = 123f; // o stores a Float
Float s2 = (Float) o;
Conversion to the same type. A narrowing reference conversion.

"A widening or narrowing reference conversion doesn't change the object in any way. The narrowing (or widening) part specifically refers to the fact that the assignment operation includes (does not include) type-checking of the variable and its new value."

"This is the rare example where everything is clear."

"To avoid the errors in these exampleswe have a way to find out what type is referenced by the Object variable:"

Code
int i = 5;
float f = 444.23f;
String s = "17";
Object o = f;                       // o stores a Float

if (o instanceof  Integer)
{
    Integer i2 = (Integer) o;
}
else if (o instanceof  Float)
{
    Float f2 = (Float) o;            // This if block will be executed
}
else if (o instanceof  String)
{
    String s2 = (String) o;
}
undefined
10
Task
New Java Syntax, level 10, lesson 6
Locked
You can't buy friends
A friend (Friend class) must have three constructors: - Name - Name, age - Name, age, sex.

"You should perform this check before every widening conversion unless you are 100% sure of the object's type."

"Got it."

undefined
10
Task
New Java Syntax, level 10, lesson 6
Locked
The basis of a wheel
In the Circle class, create a constructor that initializes all the instance variables. The constructor must have three parameters.
undefined
10
Task
New Java Syntax, level 10, lesson 6
Locked
Populate the Rectangle class
The parameters for the rectangle (Rectangle class) will be top, left, width, and height. Create as many constructors as possible. Examples: - 4 parameters are specified: left, top, width, height - width/height is not specified (both are 0); - height is not specified (it is equal to the width), we'll
undefined
10
Task
New Java Syntax, level 10, lesson 6
Locked
Constructor
Figure out what the program does. Find and fix one bug in the Circle class. Don't change the main method. Hint: Study the default constructor.
undefined
10
Task
New Java Syntax, level 10, lesson 6
Locked
Creating cats
A cat (Cat class) must have five constructors: - Name - Name, weight, age - Name, age (standard weight) - Weight, color (name, address, and age are unknown; the cat is homeless); - Weight, color, address (someone else's pet). The constructor's job is to make the object valid. For example, if the wei
undefined
10
Task
New Java Syntax, level 10, lesson 6
Locked
Calling a constructor from a constructor
Figure out what the program does. Correct the constructor with two parameters so that it calls another constructor with a radius of 10. Think about which constructor you need to call. Hint: carefully study the implementation of the default constructor.
undefined
10
Task
New Java Syntax, level 10, lesson 6
Locked
Max constructors
Study the Circle class. Write the maximum number of possible constructors with different arguments. Hint: Don't forget about the default constructor.