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 Data Structures - Retired Getting There Object Inheritance

Exposing a method in Java

Why is it called "exposing a method?"

1 Answer

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Jess,

It's called exposing a method when we make it public to other classes because we are doing just that. We are letting other classes know of it's existence and we're taking it out of hiding.

Generally in Object Oriented Programming we want each class to do it's own job and only be responsible for that job or task.

This is called Cohesion, which is a good thing.

However, there might be an instance when we need to pass information from one class to another, in that case we might need to expose a method to that class, this is generally where exposing a method comes from, which generally should not be known by the other class, but in the event that we make it available, we call it "exposing the method".

Great explanation of this concept. Thank you. If in a business application there is an Employee object, would we want that object to take care of everything from persisting its member fields in a database to changing pay rate, job title etc or could there be scenarios in which dedicated class methods would perform some operations on an Employee instance?

E.g.:

System.HR.Payroll.UpdateEmployeePay(Employee emp, Float newpay, Date dateEffective);

as opposed to

Employee emp = new Employee(); emp.UpdatePay(Float newpay, Date dateEffective);

Thank you

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Adiv!

I'm by no means an expert but I feel like you would definitely want the employee object to handle all things like pay which sounds like it would be a member variable. You would then create getter methods that could be used in your HR class and object that use these getter methods to get the values of the variables in the employee class such as pay, but not be able to modify them. You'll want to leave that job strictly to the employee object. You can sometimes let other classes modify it, but its best to limit what other classes can do to one another as you don't want to create a single class that does everything.

Of course this is a broad statement and there's exceptions to every rule, but generally I think each class and objects created in it should internally take care of themselves and pass information to other classes when needed.