(Java Programming)
Question 2.
Consider the following class:
abstract class test1 {
static int x;
}
and following interfaces:
public interface it1 {
public int geta1();
}
public interface it2 {
public void seta1(int x);
}
Implement the two interfaces it1 and it2 such that they each can modify the value of x in same object. You should write complete class with main (including any additional classes that may be needed) to accomplish this task.
Expert Answer
test1.java
abstract class test1 {
static int x;
}
_______________
it1.java
public interface it1 {
public int geta1();
}
_______________
it2.java
public interface it2 {
public void seta1(int x);
}
________________
Test1Impl.java
public class Test1Impl extends test1 implements it1, it2 {
@Override
public void seta1(int x) {
this.x = x;
}
@Override
public int geta1() {
return x;
}
}
____________________
MainClass.java
public class MainClass {
public static void main(String[] args) {
Test1Impl ti = new Test1Impl();
ti.seta1(10);
System.out.println(“The value of x is ” + ti.geta1());
}
}
__________________
Output:
The value of x is 10