Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA OOP inheritance tree

Pretend we have 5 classes or interfaces: U, G, B, Z and X, each type has his variable:

U u;
G g;
B b;
Z z;
X x;

this will compile:

u = z;
x = b;
g = u;
x = u;

this wont compile:

u = b;
x = g;
b = u;
z = u;
g = x;

tree solution: enter image description here question: why is the G an interface and not abstract class? also can G and X be both interfaces?

like image 350
Lowie Menu Avatar asked Jan 19 '26 02:01

Lowie Menu


1 Answers

Why is the G an interface and not abstract class?

Because U can extend exactly one class (abstract or not). Since X is a (abstract) class, then G must be an interface.

Can G and X be both interfaces?

Yes. Java allows classes (like U) to implement multiple interfaces.

like image 188
Konstantin Yovkov Avatar answered Jan 21 '26 14:01

Konstantin Yovkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!