Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Unit Testing in Java Why Test? Code Reuse

Boban Talevski
Boban Talevski
24,788 Points

A possibly wrong quiz question?

The quiz question following this video which reads: "When you use the inheritance model of code re-use, you must implement ALL public methods provided by the parent class." seems to have a confusing answer. The correct answer is true btw, and I initially answered false :).

As far as I understand, implementing a method means you have to write your own version of the said method in the child class, which isn't the case with inheritance. If the method isn't implemented in the child class, if called from an object of the child class it will use the method implementation from the parent class, but I don't think that's considered implementing the method when inheriting. It does mean the method is exposed to the public through the child class though. But the question should probably be rephrased a bit, what do you guys think? Do you think the correct answer should be true or false? Or maybe I have the terminology wrong regarding what is considered implementing a method....

I agree. This question is confusing and I got it wrong as well. Because when I inherit a class, I can simply reuse the public methods from the parent class - I don't have to implement all the methods explicitly in my subclass.

2 Answers

Herby Raynaud
PLUS
Herby Raynaud
Courses Plus Student 6,969 Points

I agree this question is either completely wrong or the intention of the question as suggested by Evgeniia is not well conveyed. The whole point of inheritance is exactly the opposite of what this question implies, namely that you can use the default functionality provided by the parent class while providing custom implementations for specific methods. That doesn't prevent you from shooting yourself in the foot if you provide an implementation that is somehow incompatible with the rest of the public API however.

Evgeniia Vakarina
Evgeniia Vakarina
3,317 Points

I think what they meant there was that if we just inherit the class you should provide all the needed implementation for all the inherited methods, otherwise anybody using your code could in theory use those methods from the inherited class on your class, so to prevent unexpected behavior you should either implement all the methods or use the composition approach instead of inheritance.