Friday, March 9, 2012

Challenge 051’s Solution

In Part I of Challenge 51, all four concepts can be seen in the code sample. First, method overriding is seen with method1. This method is originally defined in ClassA, then redefined in ClassB. ClassB will run its own version of method1 instead of the version inherited from ClassA, making method1 an example of method overriding. Next, method overloading is seen with methodB. ClassA has two methodBs: one lacking parameters, and one requiring an int value n. They both have the same name, but are two different methods, making methodB an example of method overloading. After that, information hiding is seen in the int variable var1. This variable has been declared private, so it cannot be directly referred to outside of ClassB. Because of its private status, var1 is an example of information hiding. Finally, procedural abstraction is seen with method3. Procedural abstraction is the use of multiple methods to perform a function. methodA is given an int value n from method3, performs a calculation, then returns the value back to method3, which makes method3 an example of procedural abstraction.
 
In Part II of Challenge 51, only statement III has a chance of working. To understand why, one first has to analyze the declarations of ob1 and ob2. ob1 is a no-args ClassA object created by the ClassA class, so it can perform all of ClassA’s methods. ob2, while declared as a ClassB object, is created by the ClassA class. This limits ob2 to performing only ClassA’s methods. Statement I has ob1 perform the method2 method. However, method2 can only be performed by ClassB objects, so statement I cannot be performed. Similarly, statement II has ob2 perform the method2 method. ob2 is, like ob1, a ClassA object, so method2 cannot be performed, making statement II false as well. Finally, statement III makes ob1 a ClassB object, then has it perform the method2 method. Because it converts ob1 before asking it to perform method2, statement III can be performed, making it true.
 
−Phoenix

3 comments:

  1. Your articulation here is spot-on. I'd like you to review just this one part,

    "Because it converts ob1 before asking it to perform method2, statement III can be performed, making it true."

    Revise with technical precision in mind. Thank you.

    ReplyDelete
  2. “ClassA” and “ClassB” objects are compatible; thus, “ob1”, which is a “ClassA” object, is able to be cast into a “ClassB” object. This allows it to perform “method2”, making statement III true.

    −Phoenix

    ReplyDelete
  3. Got it. 350 XPs awarded for this completion.

    ReplyDelete