You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implicit typecasting happens automatically when converting a value from a smaller data type to a larger one, as there is no risk of losing information. For example, you can assign an int to a double without any special syntax.
297
+
Implicit typecasting happens automatically when converting a value from a smaller data type to a larger one, as there is no risk of losing information. For example, you can assign an int to a double without any special syntax as shown in <xrefref="implicit-typecasting"text="type-global"/>.
298
298
</p>
299
-
<pre>
300
-
int myInt = 10;
301
-
double myDouble = myInt; // Automatic casting from int to double
302
-
</pre>
303
-
299
+
300
+
<listingxml:id="implicit-typecasting">
301
+
<programinteractive="activecode"language="java">
302
+
<code>
303
+
void main() {
304
+
int myInt = 10;
305
+
double myDouble = myInt; // Automatic casting from int to double
306
+
307
+
System.out.println(myDouble);
308
+
}
309
+
</code>
310
+
</program>
311
+
</listing>
312
+
304
313
<p>
305
-
Explicit typecasting is required when converting from a larger data type to a smaller one, as you might lose data. You must do this manually by placing the target type in parentheses () before the value.
314
+
Explicit typecasting is required when converting from a larger data type to a smaller one, as you might lose data. You must do this manually by placing the target type in parentheses () before the value as shown in <xrefref="explicit-typecasting"text="type-global"/>.
306
315
</p>
307
-
<pre>
308
-
double originalDouble = 9.78;
309
-
int castedInt = (int) originalDouble; // Explicitly casts double to int. The value of castedInt is now 9.
310
-
</pre>
316
+
<listingxml:id="explicit-typecasting">
317
+
<programinteractive="activecode"language="java">
318
+
<code>
319
+
void main() {
320
+
double originalDouble = 9.78;
321
+
int castedInt = (int) originalDouble; // Explicitly casts double to int. The value of castedInt is now 9.
322
+
323
+
System.out.println(castedInt);
324
+
}
325
+
</code>
326
+
</program>
327
+
</listing>
311
328
312
329
<p>
313
330
Besides primitive types, type casting is also a fundamental concept when working with objects, especially within an inheritance hierarchy. This involves converting an object reference from one class type to another, typically between a superclass and a subclass. This is often referred to as upcasting and downcasting.
314
331
</p>
315
332
<p>
316
-
Let's imagine we have a simple class hierarchy: an <c>Animal</c> superclass and a <c>Dog</c> subclass.
333
+
In <xrefref="animal-dog-example"text="type-global"/>, we have a simple class hierarchy: an <c>Animal</c> superclass and a <c>Dog</c> subclass.
317
334
</p>
318
-
<pre>
335
+
336
+
<listingxml:id="animal-dog-example">
337
+
<programlanguage="java">
338
+
<code>
339
+
/**
340
+
* Animal class is the superclass, with a method makeSound().
341
+
*/
319
342
class Animal {
320
343
public void makeSound() {
321
344
System.out.println("The animal makes a sound.");
322
345
}
323
346
}
324
347
348
+
/**
349
+
* Dog class is a subclass of Animal, with an additional method bark().
350
+
*/
325
351
class Dog extends Animal {
326
352
public void bark() {
327
353
System.out.println("The dog barks!");
328
354
}
329
355
}
330
-
</pre>
356
+
</code>
357
+
</program>
358
+
</listing>
331
359
332
360
<p>
333
-
<strong>Upcasting (Implicit):</strong> Upcasting is casting a subclass instance to a superclass reference type. This is always safe because a subclass object is guaranteed to have all the methods and properties of its superclass. Therefore, upcasting is done implicitly by the compiler.
361
+
<strong>Upcasting (Implicit):</strong> Upcasting is casting a subclass instance to a superclass reference type. This is always safe because a subclass object is guaranteed to have all the methods and properties of its superclass. Therefore, upcasting is done implicitly by the compiler as <xrefref="upcasting-example"text="type-global"/>.
334
362
</p>
335
-
<pre>
363
+
<listingxml:id="upcasting-example">
364
+
<programinteractive="activecode"language="java">
365
+
<code>
366
+
367
+
/**
368
+
* Animal class is the superclass, with a method makeSound().
369
+
*/
370
+
class Animal {
371
+
public void makeSound() {
372
+
System.out.println("The animal makes a sound.");
373
+
}
374
+
}
375
+
376
+
/**
377
+
* Dog class is a subclass of Animal, with an additional method bark().
378
+
*/
379
+
class Dog extends Animal {
380
+
public void bark() {
381
+
System.out.println("The dog barks!");
382
+
}
383
+
}
384
+
385
+
/**
386
+
* Upcasting example: A Dog object is created, but the reference is of type Animal.
387
+
*/
388
+
void main() {
336
389
// A Dog object is created, but the reference is of type Animal.
337
390
// This is implicit upcasting.
338
-
Animal myAnimal = new Dog();
391
+
Animal myAnimal = new Dog();
339
392
340
-
myAnimal.makeSound(); // This is valid, as makeSound() is defined in Animal.
393
+
myAnimal.makeSound(); // This is valid, as makeSound() is defined in Animal.
394
+
395
+
// myAnimal.bark(); // This would cause a compile-time error!
396
+
// The compiler only knows about the methods in the Animal reference type.
397
+
}
398
+
</code>
399
+
</program>
400
+
</listing>
341
401
342
-
// myAnimal.bark(); // This would cause a compile-time error!
343
-
// The compiler only knows about the methods in the Animal reference type.
344
-
</pre>
345
402
<p>
346
403
<strong>Downcasting (Explicit):</strong> Downcasting is casting a superclass reference back to its original subclass type. This is potentially unsafe because the superclass reference might not actually point to an object of the target subclass. You must perform an explicit cast. If you cast to the wrong type, Java will throw a <c>ClassCastException</c> at runtime.
347
404
</p>
348
405
<p>
349
-
To safely downcast, you should first check the object's type using the <c>instanceof</c> operator.
406
+
To safely downcast, you should first check the object's type using the <c>instanceof</c> operator as shown in <xrefref="downcasting-example"text="type-global"/>.
350
407
</p>
351
-
<pre>
352
-
// 'myAnimal' is an Animal reference, but it points to a Dog object.
353
-
if (myAnimal instanceof Dog) {
354
-
// The check passed, so this downcast is safe.
355
-
Dog myDog = (Dog) myAnimal;
356
-
357
-
// Now we can access methods specific to the Dog class.
358
-
myDog.bark(); // This is now valid.
408
+
<listingxml:id="downcasting-example">
409
+
<programinteractive="activecode"language="java">
410
+
<code>
411
+
/**
412
+
* Animal class is the superclass, with a method makeSound().
413
+
*/
414
+
class Animal {
415
+
public void makeSound() {
416
+
System.out.println("The animal makes a sound.");
417
+
}
359
418
}
360
-
</pre>
419
+
420
+
/**
421
+
* Dog class is a subclass of Animal, with an additional method bark().
422
+
*/
423
+
class Dog extends Animal {
424
+
public void bark() {
425
+
System.out.println("The dog barks!");
426
+
}
427
+
}
428
+
429
+
/**
430
+
* Downcasting example: An Animal reference is downcast to a Dog reference after checking its type.
431
+
*/
432
+
void main() {
433
+
Animal myAnimal = new Dog();
434
+
435
+
// 'myAnimal' is an Animal reference, but it points to a Dog object.
436
+
if (myAnimal instanceof Dog myDog) {
437
+
// Now we can access methods specific to the Dog class.
438
+
myDog.bark();
439
+
}
440
+
}
441
+
442
+
</code>
443
+
</program>
444
+
</listing>
361
445
362
446
<p>
363
-
In this example, we first create a <c>Dog</c> object and assign it to an <c>Animal</c> reference (upcasting). Then, we check if the <c>Animal</c> reference is actually pointing to a <c>Dog</c> object before downcasting it back to a <c>Dog</c> reference.
447
+
In <xrefref="downcasting-example"text="type-global"/>, we first create a <c>Dog</c> object and assign it to an <c>Animal</c> reference (upcasting). Then, we check if the <c>Animal</c> reference is actually pointing to a <c>Dog</c> object before downcasting it back to a <c>Dog</c> reference.
0 commit comments