Using the interface and class definitions below. Write the headings of the metho
ID: 3813276 • Letter: U
Question
Using the interface and class definitions below. Write the headings of the methods which still need to be defined in the class Class2. Public interface Interface1 {public float method1 (int 1);} public interface Interface2 extends Interface1 {public int method2 (int i); public void method3(int i);} public abstract class Classs1 {public float method1 (int anInt) {return anInt * 2.0f;} public abstract void method3 (Object anObject); public abstract void method4(int anInt);} public class Class2 extends Class1 implements Interface2 {public void method3 (int anInt) {system.out.println(anInt);} What method headings still need definition here?} List the headings of methods still required to be defined in Class2:Explanation / Answer
1. Child Class "Class2" is inheriting from Parent class "Class1" which is of abstract type.
2. The child class which is inheriting the abstract parent class must have definitions for all the abstract methods declared in Parent abstract class.
3. In the given code, there are two abstract methods defined in abstract class "Class1". So child class "Class2" should define both the abstract methods.
public abstract void method3(Object anObject);
public abstract void method4(int anInt);
4. Similarly Class2 is implementing interface Interface2, the methods declared in that interface needs to be defined in child class.
public int method2(int i);
public void method3(int i);
5. Among these two methods, method3 is already defined in class2. So define one more method method2.
So as a total three more methods need to be defined in Class class2
So Class2 modifies as:
public class Class2 extends Class1 implements Interface2{
public void method3(int anInt) {
System.out.println(anInt) ;
}
/* Defining abstract method3 of abstract class "Class1" */
public void method3(Object anObject) {
System.out.println((Integer)anObject) ;
}
/* Defining abstract method of abstract class class1 */
public void method4(int anInt) {
System.out.println(anInt) ;
}
/* Defining abstract method of interface interface2 */
public int method2(int anInt) {
return anInt;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.