Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about online test question on method overriding (Java OCA 8 Programmer I Study Guide, Sybex)

 
yogesh doshi
Ranch Hand
Posts: 34
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was one question in chapter 5 of Sybex Wiley Test which is as follows .


what must be in blank to compile code and answer was final.
But when I type this code after adding main method to OCAJP it shows following error in eclipse and compiler both what should I do.
OCA1.java:10: error: method() in OCAJP cannot override hello() in OCAJP
         final void method()
                    ^

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't find that question in the chapter. Can you be more specific about where you saw it?

And the answer is that is incorrect. Because the method is final in the superclass, it can't be overridden.
 
yogesh doshi
Ranch Hand
Posts: 34
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya I was giving many randome tests so finally I verified it and it was found on Willey.com
Screenshot-(500).png
[Thumbnail for Screenshot-(500).png]
 
yogesh doshi
Ranch Hand
Posts: 34
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soory for earlier post. But can you help me with this question
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • First of all both classes having public access modifier. When you declare outer class with public access modifier then the source file containing this class must have same name as class has. There can only be at most one outer class with public access modifier defined in a source file. In your example Exam and OCAJP classes having public access modifiers so both are in separate .java file named Exam.java and OCAJP.java respectively.
  • Here OCAJP extends Exam and method of Exam class named method() is overriden by OCAJP class because method signature of method() in OCAJP is same as method in Exam class also both have same return type void and same access modifiers i.e. package-private.
  • Now they are asking what can be filled in blank so that method will still be overriden by OCAJP.


  • Rules of non-private method overriding:( Ref: OCA SE 8 Study Guide Sybex Page No.247-248 )
  • Note: You can't override a private method of a class because It is accessible within that class only.

    1. Method in childe class must have the same signature as the method in parent class.
    2. Method in child class must be at least accessible Or more accessible than the method in parent class.
    3. Method in child class may not throw a checked exception that is new Or broader than the class of any exception thrown in the parent class method.
    4. If method returns a value, It must be the same Or a subclass of the method in parent class, aka covariant return type.

  • Let's apply above rules and others
  • You can't fill with abstract because of two reasons, method() of OCAJP has body but abstract methods are just declared but can't have body and abstract methods are declared in abstract class only but OCAJP is not abstract class so abstract is not possible.
  • Yes you can fill with final, because this method in OCAJP will have same signature, same return type and access modifier like method in Exam class.
  • According to rule no 2, No you can't fill with private because method in parent class i.e. Exam has package-private access modifier and method in subclass i.e. OCAJP can't have less accessible modifier i.e. private.
  • Method with default keyword is used only in interface for default implementation of an optional method so default is also wrong answer.
  • According to rule no 4, No you can't fill with int because method of parent class has void and method in subclass can't have int return type.

  • Answer: Only final.
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @ Yogesh
  • Please always post your code using code tag from above menus bar which you get when you click on post reply button at the bottom Or New topic button. Just enclose your code with code tag which will appear like below example. Already requested here you can refer What do these tags mean?
  • Preview post before submitting.
  • Example of code posted using code tag:
  • Also post the complete error If you get any ( but out side of code tag ), only enclose code in code tag.
  • Click here --->view image  to know how to use code tag.
  • If your post follows above things then chances of getting answers by people who knows the answer are high.
  •  
    yogesh doshi
    Ranch Hand
    Posts: 34
    Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi yogesh doshi,

    First of all, a warm welcome to CodeRanch!

    yogesh doshi wrote:what must be in blank to compile code and answer was final.

    That's definitely the correct answer! And here is why. Because class OCAJP is a subclass of class Exam and the method signature of method method() in subclass OCAJP is exactly the same as the method method() in superclass Exam, you have to select all options which result in a valid method override (and of course valid Java code too). So let's go through all the different options and provide a small explanation why this keyword would be correct/incorrect.
  • abstract: because class OCAJP is a concrete (nonabstract) class, it can't have abstract methods (only concrete methods are allowed). And another reason: an abstract method declaration should end with a semicolon, not with curly braces (as this denotes a concrete method)
  • private: you can not make the access modifier of an overriding method more restrictive (less restrictive is allowed). So the method method() in superclass Exam has default (package-private) access and therefore only default, protected and public access are allowed for the overriding method method() in subclass OCAJP, but private is not as it is more restrictive than default
  • default: this keyword can only be used in a switch statement or define a default interface method in an interface. It is not allowed in a method declaration in a class (important note: a default interface method is a new addition to the Java language since Java 8, so in Java 7 and prior versions the keyword default can only be used in a switch statement)
  • int: a method can only have one return type and the method declaration of method() in subclass OCAJP already has void as a return type, so you can't use int
  • final: the final keyword is allowed for a method declaration in a class (meaning you can't override this method). And it has no effect on the return type or the access modifier of the overriding method and therefore is the correct answer for this question


  • yogesh doshi wrote:But when I type this code after adding main method to OCAJP it shows following error in eclipse and compiler both what should I do.

    You have to pay very close attention when you try to recreate a code snippet and make sure you follow the instructions very carefully: you didn't fill in the blank (which was in front of the method method() in subclass OCAJP), but added it to the method declaration of the method method() in superclass Exam. And that's why you get a compiler error.

    Hope it helps!
    Kind regards,
    Roel

    PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. I already added the code tags for you. See how much easier the code is to read?
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yogesh doshi wrote:

    I hope you are aware that your friendly Thanks class will not compile and will result in a compiler error
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let's see if you have understood all of the above. Here is a pop quiz question. I have made a few minor changes to the original code snippet and provided a different set of keywords. But the question remains the same: Which of the following can fill in the blank in the code to make it compilePlease select:
  • private void
  • abstract void
  • protected void
  • final int
  • public int
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic