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 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 ;-)