Which statement is true about the classes and interfaces?
public interface A{
public void doSomething(String thing);
}
public class AImpl implements A{
public void doSomething(String msg){}
}
public class B{
public A doiit() {
}
public String execute(){
}
}
public class C extends B{
public AImpl doiit(){
}
public Object execute(){
}
}
A. Compilation will succeed for all classes and interfaces.
B. Compilation of class C will fail because of an error in line public AImpl doiit() - line 2.
C. Compilation of class C will fail because of an error in line public Object execute() - line 6.
D. Compilation of class AImpl will fail because of an error in line public void doSomething(String msg) - line 2.
Execution:
Compatibility between types refers to the similarity of two types to each other. Here return types are not compatible between String execute() and Object execute()
In Class C, execute method is overrided with return type Object, as "Return Type Object is not Compatible with String" so it will unable to compile.
If we change execute return type to String, the compile time issue will be resolve
or if we set return type of execute() method to Object in Class B and String in Class C then also the issue will resolve as Object is base class
Output: C
C. Compilation of class C will fail because of an error in line public Object execute() - line 6.
public interface A{
public void doSomething(String thing);
}
public class AImpl implements A{
public void doSomething(String msg){}
}
public class B{
public A doiit() {
}
public String execute(){
}
}
public class C extends B{
public AImpl doiit(){
}
public Object execute(){
}
}
A. Compilation will succeed for all classes and interfaces.
B. Compilation of class C will fail because of an error in line public AImpl doiit() - line 2.
C. Compilation of class C will fail because of an error in line public Object execute() - line 6.
D. Compilation of class AImpl will fail because of an error in line public void doSomething(String msg) - line 2.
Execution:
Compatibility between types refers to the similarity of two types to each other. Here return types are not compatible between String execute() and Object execute()
In Class C, execute method is overrided with return type Object, as "Return Type Object is not Compatible with String" so it will unable to compile.
If we change execute return type to String, the compile time issue will be resolve
or if we set return type of execute() method to Object in Class B and String in Class C then also the issue will resolve as Object is base class
Output: C
C. Compilation of class C will fail because of an error in line public Object execute() - line 6.
No comments:
Post a Comment