wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What will compile, what won't? Reflections on javac Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What will compile, what won Watch "What will compile, what won New topic
Author

What will compile, what won't? Reflections on javac

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Hello JavaRanch,

yeeeeeeeeeehaw! I am stuck on this here question pertaining to what will compile and what will not. My question stems from OCP Exam 2 question 15.




Which line(s), inserted independently at line 7, will compile? (Choose all that apply.)

a) void getSKU() { ; }

b) private void getSKU() { ; }

c) protected void getSKU() { ; }

d) public void getSKU() { ; }

How are all of these correct? Do the rules of overriding completely evaporate when the classes are not related by inheritance?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Ted North wrote:How are all of these correct? Do the rules of overriding completely evaporate when the classes are not related by inheritance?

You first. Tell us what you think this code is doing. And what is your understanding of the rules of overriding?
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Rules of overriding are basically:
> the access modifier cannot be more restrictive
> the overriding method can not throw any broader checked exceptions
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Ted North wrote:Rules of overriding are basically:
> the access modifier cannot be more restrictive
> the overriding method can not throw any broader checked exceptions


More restrictive than what? Broader than what? Look again at the code example and explain exactly what it does.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4491
    
    8

Ted North wrote:How are all of these correct? Do the rules of overriding completely evaporate when the classes are not related by inheritance?

If the classes aren't related by inheritance, there is no overriding. There is only two unrelated classes that happen to have methods with the same name.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Dennis Deems wrote:
Ted North wrote:Rules of overriding are basically:
> the access modifier cannot be more restrictive
> the overriding method can not throw any broader checked exceptions


More restrictive than what? Broader than what? Again, tell us in your own words what this code does.


The access modifier cannot be more restrictive than the original method that is being overriden and it can't throw any broader exceptions than the original method being overriden.

So, as far as what the code does...it doesnt do much of anything. There is an abstract class Tool with a primitive int variable named SKU, not sure if this can be considered an instance variable since the class is abstract. There is also an abstract method with a void return type named getSKU which is encapsulation for the abstract Tool class; a getter for the int instance variable SKU . Class Hammer is not related at all to the abstract class Tool though it has a method with the same name getSKU().
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Matthew Brown wrote:
Ted North wrote:How are all of these correct? Do the rules of overriding completely evaporate when the classes are not related by inheritance?

If the classes aren't related by inheritance, there is no overriding. There is only two unrelated classes that happen to have methods with the same name.


Awesome. tl:dr - thank-you

[solved] - this is what I also concluded Matthew Brown

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Classic case of misdirection. They make you think of inheritance when you see abstract, then hit you with something totally unrelated. Or maybe it is related in that the classes are unrelated and I should be able to recognize when inheritance plays a factor and when it doesn't? Hmmm

What? Joe Cockerspaniel is not related to Joe Cocker? How can that be, when they have the same name?!

Is the exam really that tricky these days?


Junilu - [How to Ask Questions] [How to Answer Questions]
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Ted North wrote: Class Hammer is not related at all to the abstract class Tool though it has a method with the same name getSKU().

This is exactly what should have led you to recognize, without Matthew's prompting, that overriding is not taking place, and therefore any rule associated with it does not apply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What will compile, what won't? Reflections on javac