As we know in Java we can't have multiple inheritance and protected variable in interface, so how can I implement this code?
Bar.java
public class Bar {
protected int a = 0;
public void increaseA() {
a++;
}
public void printA() {
System.out.println(a);
}
}
Foo.java
public class Foo extends Bar, FooBar {
@Override
public void printClassName() {
printClassName();
System.out.println("Foo");
}
}
FooBar.java
public class FooBar {
void printClassName(){
System.out.println("FooBar");
}
}
Main.java
public class Main {
public static void main(String[] args) {
Foo foo = new Foo();
FooBar fooBar = foo;
foo.printClassName();
Bar bar = foo;
bar.increaseA();
bar.printA();
}
}
abstract class Bar {
protected int a = 0;
public void increaseA() {
a++;
}
public void printA() {
System.out.println(a);
}
}
interface FooBar {
default void printClassName(){
System.out.println("FooBar");
}
}
class Foo extends Bar implements FooBar {
@Override
public void printClassName() {
FooBar.super.printClassName();
System.out.println("Foo");
}
}
public class Q4 {
public static void main(String[] args) {
Foo foo = new Foo();
FooBar fooBar = foo;
foo.printClassName();
Bar bar = foo;
bar.increaseA();
bar.printA();
}
}
And so the output is
FooBar
Foo
1