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(); }
in java Create the classes SubstitutionCipher and ShuffleCipher, as described below: 1. SubstitutionCipher implements the interfaces...
in java Create the classes SubstitutionCipher and ShuffleCipher, as described below: 1. SubstitutionCipher implements the interfaces MessageEncoder and MessageDecoder. The constructor should have one parameter called shift. Define the method encode so that each letter is shifted by the value in shift. For example, if shift is 3, a will be replaced by d, b will be replaced by e, c will be replaced by f, and so on. (Hint: You may wish to define a private method that shifts...
1. Design a Java CartesianPoint class for a Cartesian Point which implements both cloneable and comparable...
1. Design a Java CartesianPoint class for a Cartesian Point which implements both cloneable and comparable interfaces The class should have the following private member variables: • the x value of a point: int • the y value of a point: int and the class should have the following public member functions: • default constructor which initializes the point to the origin of the Cartesian coordinate system • explicit constructor which initializes the point to a pair of given value...
Describe/define/explain a. The relationship between Java's Iterable and Iterator interfaces b. An inner class c. An...
Describe/define/explain a. The relationship between Java's Iterable and Iterator interfaces b. An inner class c. An anonymous inner class d. The functionality of an Iterator remove method
in java Design and implement a class named Person and its two subclasses named Student and...
in java Design and implement 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 (year 1,year 2,year 3,or year 4). An employee has an office, salary, and date hired. Use the Date class from JavaAPI 8 to create an object for date hired. A faculty member has office hours and a rank. A staff...
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
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
Design a class named Person and its two subclasses named Student and Employee. Make Faculty and...
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 e-mail 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. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT