TATA Consultancy Services Interview Question for Java Developers


Country: India
Interview Type: In-Person




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

overridden method will be applicable for both the interface.

for eg:

interface A
{
	public void foo();
}


interface B
{
	public void foo();
}


public class MultipleInheritenceTest implements A,B {

	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		A a=new MultipleInheritenceTest();
		a.foo();
		
		B b=new MultipleInheritenceTest();
		b.foo();
	}

	@Override
	public void foo() {
		System.out.println("Hello");
		
	}

}


Output:
Hello
Hello

- Ankit Garg April 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you have done it great but no need to take two objects..there exists no difference whether it takes method from 1st class or 2nd.But we are accessing it through main class object as the methods are inherited to main class.

- programmer July 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think it doesn't make any difference. In fact, the implementation of both similar methods will be common and one in C Class.

Please correct me if I am wrong

- Arshad Hussain April 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Although you don't have multi-inherentence in java, but you can extends from two classes that have the same method. In C++, they are quite different.

class base1 {
   void start() { cout << "Inside base1"; }
};

class base2 {
   void start() { cout << "Inside base2"; }
};

class derived : base1, base2 { };

int main() {
  derived a;
  a.start();

}

You can solve it like this

a.base1::start();

a.base2::start();

Or

class derived:public base1,public base2
{
public:
    using base1::start;
};

- ravio April 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

interface  A {
    /**
     * Doc A
     * **/
    public void MethodA();

    public String MethodB();

}

interface  B {
    /**
     * Doc B
     * **/
    public String MethodA();

    public void MethodB();
}

class lucy implements A,B{

    @Override
    public String MethodA() {
        //To change body of implemented methods use File | Settings | File Templates.
        return null;
    }

    @Override
    public String MethodB() {
        //To change body of implemented methods use File | Settings | File Templates.
        return null;
    }
}

Implemented class will throw these compile error:

'MethodA()' in 'lucy' clashes with 'MethodA()' in 'A'; attempting to use incompatible return type
'MethodB()' in 'lucy' clashes with 'MethodB()' in 'B'; attempting to use incompatible return type

So,it proves that class lucy implements both of the interface A and B。

- shiznet3908 April 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Obviously, it proves that it implements both the interfaces, but the question to be asked is, how do you implement these methods? Because, if you implement any one of them, the compiler will throw compile time error saying that incompatible return type for method from other interface..

So , i guess, this will not be supported, unless you change your method names...

- Vatsal Garg May 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I would like to bring  some clarity on these questions on interfaces that are been asked and discussed .
1) When there are two methods with same signature in different interfaces and if class implements both the interfaces which one gets implemented .
ex: interface A {
String fun(int a  ) ;
}
interface B{
String fun(int a  ) ;
}

class TestMultipleInheritence implements A,B 
{
String fun()
{
return null;
}

Answer : 
If you check the java Doc on interfaces , " An interface is a contract , which a class implements need to adhere to " . As in interfaces all the methods are "abstract" ( basically no body ) it doesn't make any difference as the developer need to provide the business logic for that method in the  implementation class.  Compiler checks for the signature of the method and here it follows the "overriding rules for a method in java " . 

2) When there are smilar methods in two different interfaces with different return types . 
ex :  interface A{
String fun(); }
interface B{
void fun(); }



Ans : When class tries to implement both these interfaces there will be an issue with the return type and there will be a compilation error saying " return type void is not compatible with String "  as , java uses the overriding rules for the interfaces and the overriding rules says that "The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass (also called covariant return type).
if the code gets modified as this as given below . we see no compliation error as String is descending from Object class there is no issue . 

interface A{
Object fun(); }

interface B{
String fun(); }

class TestMultipleInheritence implements A,B 
{

String fun()
{
return null;
}

}






}

- Mahanth ( mahanthopensource@gmail.com) April 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

your question is absurd...if methods are same then implementation is also same if you are implementing both

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

In this case only one implementation is enough if the both the method having same signature and return type.

- Ashwani April 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think there are 2 ways to look at it....

1)The method C has different signatures in interface A, B

2)The method C has the same signature in both A, B

Now in the 1st case obviously class C will have to implement 2 methods one that has the signature in A and the other that has signature in B...So obviously in this case the method C has different identities and Class C will be implementing both separately.

and in the second case as the signatures will be identical so it doesnt really matter if it belongs to A or B...it will be one method anyway with a concrete body.

Common sense! Lol

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

Multiple interfaces with same signature and same return type will have only one method implementation in implemented class. However, interfaces with same signature and different return types is not allowed, gives you compile time error.

- Ramesh Gudiya April 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Java doesn't support multiple inheritance using classes. It does support using interfaces.

- Anonymous April 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i just want to know for which method my class need to provide the implementation

- Mahipal April 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

only one method implementation is enough for both the method.

- Ashwani April 13, 2014 | Flag


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