Question

In: Finance

Assume that there is a class named TestInterface that implements two interfaces A and B. Both...

Assume that there is a class named TestInterface that implements two interfaces A and B. Both the interfaces have a common method with the same signature (example int sampleMethod()). Explain how will the class define this method and how will the compiler identify, to which interface does this method belong to? Will the compiler give an error or will the program execute successfully. Give your code snippet to support your answer.

Solutions

Expert Solution

I have implemented the code,as you mentioned in the question

And i observed that the type of implementation of two interfaces by the same method is executable.

The compiler implementing the sample method for both interfaces. its taking the both method implementations at a time because both the methods have same name.

Because of the same method name the implementation of two interfaces A and B is done in one method only.

if you change any method name in any interface,then you have to provide implementation for both methods,

otherwise it will generate a compiler error.

in our case both method signatures are same,no need of providing implementation for both methods.

source code:

//defining interface A,
//with a sample method,
interface A{
   int samplemethod();
}
//defining interface B
//with same sample method as in interface A
interface B{
   int samplemethod();
}
//implementing interface A and B,using TestInterface class
class TestInterface implements A,B{
   //implementing samplemethod
   public int samplemethod(){
   System.out.print(" Hello,How are you ? ");
   return 0;
   //in both interfaces we have same method, with same method signature,so we can implement only one method
   }
   public static void main(String args[]){
   //creating object for TestInterface
   TestInterface obj=new TestInterface();
   //calling sample method with TestInterface object obj.
   obj.samplemethod();
   }
}

output

comment if you have any questions


Related Solutions

You should assume that there will be an object file named “foreignsub.o” which implements a function...
You should assume that there will be an object file named “foreignsub.o” which implements a function whose prototype is given in the header file “foreignsub.h”. The header file “foreignsub.h” consists of the following line. int sub(int n, int *A, int *B, int *C); However, you do not know what this function does exactly. For your testing purpose, you may produce such a function, e.g., returning the maximum value among the 3n integers in the three arrays. You are also given...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
How do you write the constructor for the three private fields? public class Student implements Named...
How do you write the constructor for the three private fields? public class Student implements Named { private Person asPerson; private String major; private String universityName; @Override public String name() { return asPerson.name(); }
Assume that two individuals (A and B) develop the same type of cancer. Both individuals are...
Assume that two individuals (A and B) develop the same type of cancer. Both individuals are given the best treatment found to be effective against cancer. Individual A responds well to treatment, while individual B does not. Using what you have learned, provide a specific type of cancer and an explanation for why there was a difference in response to treatment
If a class A implements interface I1 and class C and D are derived from class...
If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct 1. A, C, and D 2. A and D
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
Java Question Design a class named Person and its two subclasses named Student and Employee. Make...
Java Question Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Use the Date class to create an object for date hired. A faculty member has office hours and a rank. A...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
IN JAVA: Write a parent class, Device, which implements ageneric computer device.Create two child...
IN JAVA: Write a parent class, Device, which implements a generic computer device.Create two child classes, Disk and Printer, which specialize a Device with added functionality.Device Task:We are only interested in three things for the time being: the name of the device, an ID number identifying the device, and a flag indicating whether or not it is enabled. Thus, three fields are necessary, a String for the name, a int for the ID, and a boolean for the enabled status.Any...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT