Page 1 of 1

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

Posted: Tue Oct 16, 2012 8:20 am
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

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

Posted: Tue Oct 16, 2012 8:24 am
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

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

Posted: Tue Oct 16, 2012 10:38 am
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.

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

Posted: Wed Oct 17, 2012 2:08 am
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

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

Posted: Wed Oct 17, 2012 2:12 am
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

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

Posted: Wed Dec 26, 2012 11:49 pm
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?

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

Posted: Thu Dec 27, 2012 7:23 am
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.

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

Posted: Thu Dec 27, 2012 3:08 pm
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.

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

Posted: Sun Sep 15, 2013 9:16 pm
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.

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

Posted: Mon Sep 16, 2013 7:39 am
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.

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

Posted: Thu Nov 05, 2015 5:42 am
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?

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

Posted: Thu Nov 05, 2015 6:16 am
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?

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

Posted: Fri Nov 06, 2015 7:48 am
by zaferc
I attached screenshot.

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

Posted: Fri Nov 06, 2015 8:41 am
by admin
So where does it say that play method is overridden in Soccer class?

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

Posted: Tue Jan 30, 2018 9:26 am
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

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

Posted: Tue Jan 30, 2018 10:39 am
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... :)

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

Posted: Tue Jan 30, 2018 11:55 pm
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.

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

Posted: Sun May 27, 2018 10:45 am
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.

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

Posted: Mon May 28, 2018 8:19 pm
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.

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

Posted: Tue May 29, 2018 1:07 am
by flex567
Which book do you suggest?
I read OCA_ Oracle Certified Associate - Jeanne Boyarsky.

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

Posted: Tue May 29, 2018 1:38 am
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.

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

Posted: Fri Jun 15, 2018 12:02 pm
by flex567
I would disagree. The book is rubbish about this topic. For example it doesn't explain what upcast is or Substitutability.

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

Posted: Fri Sep 04, 2020 8:31 pm
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