Amazon Interview Question for Software Engineer / Developers






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

Encapsulation is the mechanism which ensures the safe and effective access of Classes or objects. For any outside objects which messages are sent from, the only things they need to take care is the interface of target object, while they dont care how does target object implement these interface. The target objects also hold some their own data and methods that is invisible for outside world, which improves the stability of the program.

- FeiXuhust February 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Encapsulation is a process of restricting access to a class's features , at the same time grouping the class related methods in the same class to avoid code duplication. For example, to represent sorting algorithms, we can have a class like

class SortingAlgorithms{

public void bubbleSort(List<Strings> strLst){
}

public void selectionSort(List<String> strLst){ }

public void quickSort(List<String> strLst){ }


}

Other way of doing the same functionality is encapsulating those algorithms in separate classes

interface SortingAlgorithms
 {
      void sort(Collection<? extends Object> clt);
 }
 
 class QuickSort implements SortingAlgorithms
 {

	@Override
	public void sort(Collection<? extends Object> clt)
	{
		// TODO Auto-generated method stub
		
	}
	 
 }
 
 class BubbleSort implements SortingAlgorithms
 {

	@Override
	public void sort(Collection<? extends Object> clt)
	{
		// TODO Auto-generated method stub
		
	}
	 
 }

In this way, we can encapsulate those sorting algorithms as classes

- Anonymous January 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Benefits of Encapsulation:

The fields of a class can be made read-only or write-only.

A class can have total control over what is stored in its fields.

The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.

- roo January 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class EncapTest{

   private String name;
   private String idNum;
   private int age;

   public int getAge(){
      return age;
   }

   public String getName(){
      return name;
   }

   public String getIdNum(){
      return idNum;
   }

   public void setAge( int newAge){
      age = newAge;
   }

   public void setName(String newName){
      name = newName;
   }

   public void setIdNum( String newId){
      idNum = newId;
   }
}

- roo January 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The variables of the EncapTest class can be access as below::

/* File name : RunEncap.java */
public class RunEncap{

   public static void main(String args[]){
      EncapTest encap = new EncapTest();
      encap.setName("James");
      encap.setAge(20);
      encap.setIdNum("12343ms");

      System.out.print("Name : " + encap.getName()+ 
                             " Age : "+ encap.getAge());
    }
}
This would produce following result:

Name : James Age : 20

- roo January 26, 2012 | Flag Reply


Add a Comment
Name:

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

Books

is a comprehensive book on 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