Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Unit Testing in Java Why Test? Composition vs. Inheritance

Do I really have to IMPLEMENT all methods from the parent class?

The question in the quiz says: "When you use the inheritance model of code re-use, you must implement ALL public methods provided by the parent class."

I stated this as false since I don't have to implement them all again. If I want to change them I have to override them, but if they are valid in the current implementation for my child class I can not mention them at all in my class or can't I?

I don't get why this is true...

I don't know where this questions is but here is my $0.02

Are you using key world implements as in class XYZ implements MyInterface

or are you using extend as in class XYZ extends MyParentClass

??

In the first case, you must implement all MyInterface methods

In the 2nd case, you don't unless you are overriding a method provided in MyParentClass

Hope this helps. Mark

Hi

I think the way the question is phrased is a bit confusing, but in the context of the previous lecture https://teamtreehouse.com/library/unit-testing-in-java/why-test/code-reuse

it makes sense.

What the instructor is trying to say, is that if you extend a public class, and expose it in your app as a public class, then you are ---- Taking responsibility --- for the behavior of methods written by someone else. In order to ensure that each methods behaves the the way you want it --- you have to override each method.

Please replay the lecture preceding the text and focus on time 2:25 to 5:00.

Now if you use the the hasA approach ... then have more control over what to expose as a public interface what the rest of interfaces remains private ... hence releasing you from the responsibility to ---- Taking responsibility --- for the behavior of methods written by someone else.

In that context, the answer to the question is True.

Hope this helps.

AJ Catambay
AJ Catambay
5,269 Points

Yes I encountered this too, and I agree with you. You are only required to implement a method from an inherited class, if it is actually an interface in which case all methods must be overriden, or you extended an abstract class with a bunch of abstract methods. Nonetheless, you can override only the methods you need. I answered false too, and I don't care what meaning they're trying to convey with the question. It's poorly phrased if the answer is true, and it's unfortunate that there is no Report feature here for the questions. I don't really have the time to look past in to the question when they can can just phrased it in a more concise manner.

1 Answer

Theodore Ravindranath
Theodore Ravindranath
41 Points

@mathiswiehl Agree with you completely. If it is an interface, you have to implement everything. But, in the case of inheritance in terms of extending a class, you would have to only override the methods whose functionality you want to change.

The question is wrong. The question should have been "When you use the inheritance model of code re-use, you must test ALL public methods provided by the parent class." and then the answer would have been "true". The disadvantage of inheritance, from the perspective of unit testing, is that you end up having to test all the methods, even ones that you did not author/modify.

AJ Catambay
AJ Catambay
5,269 Points

Yes I agree with you. I just encountered this. I think they're referring to the perspective of unit testing, but it was just not properly specified. It's unfortunate there is no Report feature here for the questions.