TATA Consultancy Services Interview Question
Java DevelopersCountry: India
Interview Type: In-Person
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;
};
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。
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...
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;
}
}
}
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
overridden method will be applicable for both the interface.
for eg:
- Ankit Garg April 09, 2014