Skip to content

Commit 4623a64

Browse files
authored
Merge pull request #225 from habibasorour/issue_222_list
Issue 222: 3.2 list, comment, activecode
2 parents f9aea95 + a5cfcd0 commit 4623a64

1 file changed

Lines changed: 116 additions & 32 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 116 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -294,73 +294,157 @@ public class TempConv {
294294
</p>
295295

296296
<p>
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.
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 <xref ref="implicit-typecasting" text="type-global"/>.
298298
</p>
299-
<pre>
300-
int myInt = 10;
301-
double myDouble = myInt; // Automatic casting from int to double
302-
</pre>
303-
299+
300+
<listing xml:id="implicit-typecasting">
301+
<program interactive="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+
304313
<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 <xref ref="explicit-typecasting" text="type-global"/>.
306315
</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+
<listing xml:id="explicit-typecasting">
317+
<program interactive="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>
311328

312329
<p>
313330
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.
314331
</p>
315332
<p>
316-
Let's imagine we have a simple class hierarchy: an <c>Animal</c> superclass and a <c>Dog</c> subclass.
333+
In <xref ref="animal-dog-example" text="type-global"/>, we have a simple class hierarchy: an <c>Animal</c> superclass and a <c>Dog</c> subclass.
317334
</p>
318-
<pre>
335+
336+
<listing xml:id="animal-dog-example">
337+
<program language="java">
338+
<code>
339+
/**
340+
* Animal class is the superclass, with a method makeSound().
341+
*/
319342
class Animal {
320343
public void makeSound() {
321344
System.out.println("The animal makes a sound.");
322345
}
323346
}
324347

348+
/**
349+
* Dog class is a subclass of Animal, with an additional method bark().
350+
*/
325351
class Dog extends Animal {
326352
public void bark() {
327353
System.out.println("The dog barks!");
328354
}
329355
}
330-
</pre>
356+
</code>
357+
</program>
358+
</listing>
331359

332360
<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 <xref ref="upcasting-example" text="type-global"/>.
334362
</p>
335-
<pre>
363+
<listing xml:id="upcasting-example">
364+
<program interactive="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() {
336389
// A Dog object is created, but the reference is of type Animal.
337390
// This is implicit upcasting.
338-
Animal myAnimal = new Dog();
391+
Animal myAnimal = new Dog();
339392

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>
341401

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>
345402
<p>
346403
<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.
347404
</p>
348405
<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 <xref ref="downcasting-example" text="type-global"/>.
350407
</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+
<listing xml:id="downcasting-example">
409+
<program interactive="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+
}
359418
}
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>
361445

362446
<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 <xref ref="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.
364448
</p>
365449
</section>
366450

0 commit comments

Comments
 (0)