Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

Base class creates contract with sub classes, because of it, you can't hide base class methods.
You may try this variants:
1) override base class method. smth like :

@Override
public void test() {
  throw new UnsupportedOperationException("not supported");
}

2) create interface for sub class, like

class BaseClass {
    public void test(){
    }
}

class SubClass extends BaseClass implements ISubClass {
    public void test1() {
    }
}

interface ISubClass{
    void test1();
}

and make other code work with ISubClass

ISubClass example = new SubClass();
        example.test1(); //ok
        example.test(); //error, no such method

- GK June 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

There are several clarifications need to be asked for:
1. Is the constructor(s) of the Base class private?
2. Are the methods public static ?
3. Is the class an abstract class?
4. If the class is concrete and public and default constructor is publicly available, there is NO way you can hide its public methods.

- Rahul June 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using an adapter pattern might be useful.

- addy June 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the base class methods are static, then defining static methods in subclass with same signature as the base class, 'hides' the methods in the base class.

- Ray June 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BaseClass {

	public void test1(){
		
	}
}

class child1 extends BaseClass{

	@Override
	public final void test1() {
		super.test1();
	}
}

class child2 extends child1{
	
//Cant override test1() unless you directly inherit the Baseclass	
	
}

- Anonymous June 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We won't touch the class how about we wrap the class in another class and make it a private inner class?

public class WrapperBaseClass{
    private class BaseClass {
         //Base classe code goes here
       }        
}

- Anirudh August 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Overriding base class method with weaker access level is not allowed in Java and .Net.
Vice-versa is still possible (unless the access modifier is not private, even though you can create a method with same name it is not overriding in true sense).

A typical scenario where this seems to be required is encapsulating 3rd Party APIs.

- abhay.lolekar August 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{class Base {
protected void display(){
System.out.println("Display in base class");
}
}

class Derived1 extends Base {
@Override
protected void display() {
try {
super.getClass().getMethod("display", null).setAccessible(false);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}

public class ClassMethodTest {


/*
* Base class is given you need to stop
* exposing the base class methods without
* touching the base class at all.
*/

public static void main(String[] args) {
Base der = new Derived1();
der.display();

}

}}

- jayakrishnan.somasekharannair March 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

{class Base {
protected void display(){
System.out.println("Display in base class");
}
}

class Derived1 extends Base {
@Override
protected void display() {
try {
super.getClass().getMethod("display", null).setAccessible(false);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}

public class ClassMethodTest {


/*
* Base class is given you need to stop
* exposing the base class methods without
* touching the base class at all.
*/

public static void main(String[] args) {
Base der = new Derived1();
der.display();

}

}}

- Anonymous March 12, 2017 | 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