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 Java Objects Meet Objects Creating Classes

Matthew Dorber
Matthew Dorber
752 Points

How does the Example.java see the PezDispenser.java?

I've been learning a bit on a previous course where you'd have to say 'Example.java extends PezDispenser' so the Example.java inherits the methods of the Pez class.

With this it seems like you don't have to, why is this? PezDispenser doesn't even say it's public so i'm just confused how it has access to it.

1 Answer

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hi Matthew

The Example class uses the PezDispenser's constructor to create an instance of PezDispenser. I see how it's confusing, since there is no constructor in the class that the teacher created, but as you can read here: https://stackoverflow.com/questions/24013545/what-is-the-difference-between-implicit-constructor-and-explicit-constructor, all classes have implicit public constructors. It's just good coding style to actually write the constructor, but technically not necessary. :-) Hope that makes sense.

Oh, and the class is automatically public, by not specifying public or private. It's the default behaviour. But I do think Craig Dennis could be a bit more specific to avoid confusion ;-)