This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

SCJP Requirements Section 5.2 notes.

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you see anything Ileft out please let me know. Or stuff I got wrong especially let me know.

Here are the requirements and then my notes. There does not seem to be much to it which bothers me. It seemed too easy if you know what I mean.

=========sun website cert==============================

�Given a scenario, develop code that demonstrates the use of polymorphism. Further, determine when casting will be necessary and recognize compiler vs. runtime errors related to object reference casting.
=======my notes ================

Polymorphism is sometimes used as a synonym for dynamic binding but they are not the same. Polymorphism is a feature of JAVA that allows an object reference variable of a superclass to hold the object reference of any of its subclasses. Dynamic binding is a term used to refer to the runtime system selecting the proper method to use when it executes a method call. If an object variable can hold references for different classes then the class stored in the variable or its methods are not know until the moment execution occurs.

At compile time if the compiler detects an invalid cast then an exception is thrown. If the validity of a cast cannot be determined then the runtime system will catch the errors at runtime. Casting up the inheritance hierarchy is valid. This means a superclass object reference variable can hold any of its subclass objects. Casting down the inheritance will not work unless an explicit cast is used and then it depends on the validity of the cast. In some cases the type or class of a variable will be changed or forgotten by the runtime system and it will be necessary to cast it back to the original class.

Joe. thanks.
 
Joe Wolfe
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep genus

or maybs a genius?\\
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks good! I think the big ideas are covered here.

But I don't understand what you mean by...

In some cases the type or class of a variable will be changed or forgotten by the runtime system and it will be necessary to cast it back to the original class.


Maybe you could elaborate on this?

Also, you noted (correctly) that polymorphism and dynamic binding are not the same thing. Maybe you want to take a stab at defining "dynamic binding" just to make sure you're clear on this.
 
Joe Wolfe
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without looking it up I think they meant an object reference variable that gets cast to another type then no longer is of the original type and to place an object of the original type back into it you need to do an explicit cast. This may not be the example they used in the book. Somehow JAVA forgets the original type .

I think they also said arrays remember their original type so this is not a problem for arrays.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of saying that explicit casting is needed because the runtime environment "forgets" a variable's type, I would say it's needed because the compiler cannot be certain of the object's true type. Explicit casting is the programmer's way of "reassuring" the compiler that what you're doing will be okay at runtime.

Arrays are objects in their own right. But it's important to recognize what they hold are references -- not objects themselves. The type of an array is determined by the type of references it holds. For example, a "String array" is an array that holds references to Strings. The reference to the array itself can be treated like any other reference. For example...

Object[] objArray = new String[10]; //upcast of array type
String[] strArray = (String[])objArray; //downcast with explicit cast

In this case, the variable objArray has a type of "Object array," but the object itself is really a String array. This object (the array) holds references to Strings.
 
Don't play dumb with me! But you can try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic