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
ETS User

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

Post by ETS User »

Hi

I failed on this question when i took the Test 1 mock test.

The answers didn't make any sense since the Testclass did not handle the exception which could be thrown by the Game class. I had to put this code in eclipse and compile it. and this surely must be something wrong with the question/and or the answers

Am i missing something? Any ideas?

/Danjel

Danjel
Posts: 4
Joined: Tue Oct 16, 2012 8:21 am
Contact:

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

Post by Danjel »

Here's the code...


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);
}
}

public class TestClass {
public static void main(String[] args) throws Exception {
Game g = new Soccer();
// 1
Soccer s = (Soccer) g;
// 2
}
}


and heres the answers:

It will not compile as it is.
It will throw an Exception at runtime if it is run as it is.
g.play(); at //1 and s.play("cosco"); at //2
g.play(); at //1 and s.play(); at //2
g.play("cosco"); at //1 and s.play("cosco"); at //2

/Danjel

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

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

Post by admin »

Hi,
I am not sure I understand what you mean when you say, "...since the Testclass did not handle the exception which could be thrown by the Game class".
The main method of TestClass does have "throws Exception". Can you please tell me which option do you have a problem understanding?

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

Danjel
Posts: 4
Joined: Tue Oct 16, 2012 8:21 am
Contact:

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

Post by Danjel »

Of course... thats what i missed...
i staired myself blind on the exception that could be thrown by the play method in Game class.

Now it is quite simple..thank you.

Sometimes it is so obvious that you miss the simple ones..

/Danjel

Danjel
Posts: 4
Joined: Tue Oct 16, 2012 8:21 am
Contact:

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

Post by Danjel »

Also... when i put the code into eclipse i missed the throws Exception in the main class (did not copy the text from the test, just rewrote it in eclipse)

Thanks again..

/Danjel

ETS user

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

Post by ETS user »

This question confused me for a different reason. I'm struggling to understand the reason given as to why g.play("cosco") isn't valid; I feel like I'm missing an important point. So I went back and looked at question v7.2.1337, which seemed kind of similar:

Code: Select all

class A {
    A() { print (); }
    void print() { System.out.println("A"); }
}
class B extends A {
    int i = Math.round(3.5f);
    public static void main(String[] args) {
        A a = new B();
        a.print();
    }
    void print() { System.out.println(i); }
}
Here, a.print() causes the method in class B to be executed, although 'a' is of reference type A. Which is what I expected. However, the description for the g.play("cosco") option indicates that it can't call Soccer's play() because it's a reference type of Game. Isn't it really the case that play() -- the method with no args -- only exists in class Game?

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

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

Post by admin »

In 2.1337, the paint method with the same signature exists in the base class as well as subclass. So where you call baseClassRef.paint(), the compiler accepts it. At run time however, baseClassRef points to an object of subclass, so subclass's paint() is actually invoked. This is a classic example of overriding and polymorphism.

In this question (2.996), the base class does not have a method with signature play(string ). Therefore, baseClassRef.play("cosco") fails to compile. Even though the actual object pointed to by the baseClassReference is of type sub class, which does have this method. But this fact is not understood by the compiler.

What you are saying is also correct. It is another way of looking at it.

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

ETS user

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

Post by ETS user »

Thank you, Paul. Just when I think I understand, there's a subtle twist to the question or answer. I think these sorts of tests are good practice if you want to be a lawyer! So much minutia.

alfman
Posts: 1
Joined: Mon Jun 24, 2013 7:17 pm
Contact:

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

Post by alfman »

So just double-checking my understanding on this question.

Since Soccer's play method has a different signature than Game's play method, Soccer is not overriding Game's play method. Is this a correct statement?

Thanks.

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

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

Post by admin »

alfman wrote:So just double-checking my understanding on this question.

Since Soccer's play method has a different signature than Game's play method, Soccer is not overriding Game's play method. Is this a correct statement?

Thanks.
Yes, that is correct.
HTH,
Paul.
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 »

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: 8858
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 5463 times

admin
Site Admin
Posts: 8858
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: 8858
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: 201
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: 8858
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: 201
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: 8858
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: 201
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.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests