Amazon Interview Question Software Engineer / Developers


Country: India


Comment hidden because of low score. Click to expand.
8
of 8 vote

There can be different reasons why data memebers are private while we can access them through getter/setter. The main reasons to keep data members private comes from Data Abstraction principle.
- The abstract properties(so called getters/setters here) are those that are visible to client code that makes use of the data members (the interface to the data members) while the concerete implmenetation is kept private and indeed can change.
-- The idea is that such changes are not supposed to have any impact on client code ( called Localization of Change). Code that uses the object in question will not need to be edited if the implementation of the object is chnaged. since any changes to the implmentation must still comply with the interface and since client code using object may only refer to properties and abilities specified in the interface as changes can be made to the implmentation without requiring any changes in code where the object is used.
-- The uses doesn't need to know any technical knowledge of how the implementation works to use an object. The implementation can be complex but will be encapsultaed (Encapsulation) in a simple interface. For e.g. if we talk about a Banking system, there can be Account class which may have complex implementation, how the account balance is changed. User of this class should not be allowed to access the balance (data member) directly and modify; but using the properties (interface to the data members), you may enforce certain rules how balance can be modified)


keeping data members private also makes your object secure from malicious software which may instantiate the object and perform directly some manipulation on data members. If it goes through properties (getters/setters) you may have some control.

Using above Account class example, the malicous software may not be able to set the account balance to large arbitrary value as with getters and setters you may have some validation plus you may never expose setters for such important data members.

They can only be accessed by the operations which will have all those validations/rules which disallows to set a Account balance to a high arbitrary value. Going through properties/operations will also make a log of the activities.

- sk on February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

See private class data pattern:

sourcemaking.com/design_patterns/private_class_data

- mag on February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess this is not what my question is all about.

- Mike on February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In addition to controlling access, getters and setters can also be used to perform post or pre-processing on variables.

For example, if you have a Rocket::getSpeed() method which returns the speed in km/h, you can switch internally (inside Rocket) between storing the speed in km/h or m/s without affecting clients of Rocket, as long as you do the appropriate conversions inside getSpeed().

In a setter method, you can perform error checking before allowing assignment. For example, checking that a given speed is <= c inside Rocket::setSpeed

- edsouza on February 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried this but the interviewer was not happy!

- Mike on February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perhaps the interviewer was an idiot. Saying he wasn't happy isn't constructive unless you remember what exactly his objections were to this answer.

- edsouza on February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

data-members are made private and accessed thru getter and setter methods for pre-processing....edsouza is right....

Suppose we are storing age of a person and v declare that as public. Now any body can access that and can write age = -39 ( as age can't be negative) so this is logically incorrect though your code allows it.

There are 2 solution for avoiding this basically validating the value.

1.) use validation code inside constructor and initialize variable in that. But this is not preferred as every time object is created it will check for the validation.

2.) Use getter/setter methods and put validation code there itself.....

- Code-Warrior on February 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

when we have integer divisions say in any software designed for Civil Engineers or Architects..where there may integer divisions and if the user enters the Zero for denomination..the it bcoms divide by zero exception..run time error...such validations can be taken care in those accessors and mutators

- sastry.soft on November 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Getters and Setters are useful in following cases.
1. To add validation code (Already mentioned above)
2. Making members public makes debugging very difficult. You can add breakpoints in getters and setters easily.

- Pankaj Kumar on October 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Data members are operated in member functions of that particular class... If this is accessible to every class then we can declare that variable as global.

- Anonymous on February 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

The answer lies in inheritence. Private members won't be inherited.

- anon on February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

are you sure ?
The last time i checked, private members were inherited but were not acessible

- scorpio on March 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

anon is correct - private variables are not inherited, if you want them to be inherited and not accessed use protected instead.

public - inherited and accessible
protected - inherited and not accessible
private - not inherited and not accessible

- YD on September 11, 2013 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More