-4

Consider the following declarations:

public class Dog0
{ public void doNothing(Dog1 a, Dog2 b) {} }

public class Dog1 extends Dog0 {}
public class Dog2 extends Dog1 {}

The following initializations appear in a different class:

Dog0 d0 = new Dog0 ();
Dog1 d1 = new Dog1 ();
Dog1 d2 = new Dog2 ();

Which of the following is a correct call to doNothing?

a) d0.doNothing(d0, d0);
b) d1.doNothing(d1, d1);
c) d1.doNothing(d2, d1);
d) d2.doNothing(d0, d0);
e) d2.doNothing(d2, d2); 

I think the answer is e) but I'm not sure. Could someone explain this to me? Thanks.

Penguin
  • 1
  • 4
  • 1
    Why not dump the code into a project and see what happens? – Drew Kennedy Apr 23 '17 at 16:38
  • `Dog1 d2 = new Dog2 ();` seems to be an incorrect assumption. You probably mean `Dog2 d2 = new Dog2 ();`. Otherwise none of the 5 alternatives will compile. – CoronA Apr 23 '17 at 16:42
  • 2
    I'm voting to close this question as off-topic because questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. [Source](https://stackoverflow.com/help/on-topic) – Joe C Apr 23 '17 at 16:44
  • It's a bad idea to code knowledge of subclasses into a superclass. This introduces a circular dependency, `Dog0` on its subclasses, and of course the subclasses on their supertype. Re-analyze the object model so that classes do not refer to their subclasses. – Lew Bloch Apr 23 '17 at 17:24
  • @DrewKennedy I actually tried it in a code. None of them worked. – Penguin Apr 23 '17 at 17:33
  • @CoronA I actually did assume that the 3rd declaration is Dog2, but the question was written in the way I posted. – Penguin Apr 23 '17 at 17:33
  • @JoeC I am sorry. I did not know that you had to post many of those things. I am merely a beginner at this. I will do that. Thanks for the advice. – Penguin Apr 23 '17 at 17:33

1 Answers1

-1

Correction

None is the answer because for E to be the answer, Dog1 d2 = new Dog2(); should be turned into Dog2 d2 = new Dog2();

But if it was, E would be the correct answer.

doNothing accepts a Dog1, Dog2 as its arguments. A Dog2 is a Dog1, therefore, you can have a Dog2 in place of a Dog1.

EyuelDK
  • 3,029
  • 2
  • 19
  • 27
  • Stack Overflow is a question-and-answer site, not a homework writing service. Please do not give users reason to believe otherwise. Thank you. – Joe C Apr 23 '17 at 16:45
  • I felt that the user wanted justification for why the answer was what it was. The user made an educated guess but did not fully comprehend the rules of Inheritance, thus I thought some clarification was valid. IMO, he didn't just say, "can you solve this for me?" – EyuelDK Apr 23 '17 at 16:50
  • @EyuelDK Thank you. – Penguin Apr 23 '17 at 17:36
  • @JoeC I know this isn't a homework help. I was only confused why none of the answers seem plausible, and the only one that seemed the most accurate would be e. I did not feel as if I was asking for how to solve this. I knew none of these answers seemed correct based on my understanding of inheritance, but I asked this question because I thought I was misinterpreting something about inheritance and wanted to know if there is anything I did not understand based on this problem. Thank you. – Penguin Apr 23 '17 at 17:39
  • I hope the explanation was helpful. Next time, I suggest you show your efforts or your thinking process so that people don't just assume you are trying to get your homework done. I was a beginner too, and I sympathized with your position and confusion - that's why I answered it for you. Just try to be careful how you ask a question. Have a nice day :) – EyuelDK Apr 23 '17 at 17:41