About Question enthuware.ocajp.i.v7.2.996 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
zaferc
Posts: 2
Joined: Wed Oct 28, 2015 1:10 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by zaferc »

Code: Select all

class Game {
  public void play() throws Exception   {
    System.out.println("Playing...");
  }
}

class Soccer extends Game {
   public void play(String ball)    {
      System.out.println("Playing Soccer with "+ball);      
   }
}
the parameter of the overriden method(play) should stay same

but answer says that play method is overriden in Soccer class

I think this is false because of the difference parameter(String ball)

am I right?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by admin »

Can you please post a screenshot of where the answer says play method is overridden in Soccer class because I couldn't see such a statement?
If you like our products and services, please help us by posting your review here.

zaferc
Posts: 2
Joined: Wed Oct 28, 2015 1:10 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by zaferc »

I attached screenshot.
Attachments
Screen Shot 2015-11-06 at 14.44.49 1.png
Screen Shot 2015-11-06 at 14.44.49 1.png (22.08 KiB) Viewed 12949 times

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by admin »

So where does it say that play method is overridden in Soccer class?
If you like our products and services, please help us by posting your review here.

Angi000
Posts: 3
Joined: Sun Jan 28, 2018 12:44 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by Angi000 »

Hi,

I am totally confused.:shock: I run the code in eclipse, and according to the answers provided, variable 'g' can only access play() method, and variable 's' can access both play() and play(String ball).

I can't match this to what I learned about static and dynamic binding. The overloaded play methods are not static, nor private or final. Why not late binding occurs, when the method associated with the class used to create the object is invoked? I thought that I understand this concept, and could use it in same cases before.

Thanks

Angi000
Posts: 3
Joined: Sun Jan 28, 2018 12:44 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by Angi000 »

I think I understand now, based on the answer to Q48. What I described about late binding happens at runtime, but at compiler time, the compiler checks the statements based on the class of the reference. Java is so complex... :)

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by admin »

You should not use any IDE such as Eclipse or Netbeans while preparing for the exam. IDEs show several messages and alerts that help you write good code but such hints are not available in the exam.
It is best to use notepad and command line javac/java while practicing with test programs.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by flex567 »

I don't understand why the

Code: Select all

call g.play("cosco"); at //1
is not valid?

I was following this rule:
in case of instance method invocation, it is the JVM that determines at run time (based on the class of the actual object referred to by the reference variable) which version of the method is to be called
class of the actual object referred = Soccer -> which contains the method, so the call should be valid:

Code: Select all

public void play(String ball)


The answer's explanation is
g.play("cosco") is not valid because even though the object referred to by g is of class Soccer, the reference type of g is Game, which does not have play(String ) method.
I don't understand why is the reference type of g is important. Fallowing the rule above for method invocation it shouldn't be.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by admin »

This rule is about the runtime i.e. applicable to the JVM. But before it gets to JVM, the code has to be compiled by the compiler. Compiler has a different set of rules.

This is a very important topic and you should read about this topic from a book before attempting mock exams.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by flex567 »

Which book do you suggest?
I read OCA_ Oracle Certified Associate - Jeanne Boyarsky.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by admin »

This book is fine. I don't have it with me right now to tell you the exact location where they expain this but go through the chapter about polymorphism carefully.

Paul.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by flex567 »

I would disagree. The book is rubbish about this topic. For example it doesn't explain what upcast is or Substitutability.

nalugram@gmail.com
Posts: 1
Joined: Wed Sep 02, 2020 9:02 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.996 :

Post by nalugram@gmail.com »

Very good explanation in this book - "Mala Gupta - OCA Java SE 8 Programmer I Certification Guide-Manning Publications (2016)", Chapter 6.6.2 - Binding of variables and methods at compile time and runtime

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 39 guests