ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE
Which of these can a Java interface still NOT have, even in a modern version such as Java 17?
- A. default methods with a body
- B. private helper methods (allowed since Java 9)
- C. Instance (non-static) fields holding genuinely mutable, per-object state
- D. static methods
Correct answer: C. Instance (non-static) fields holding genuinely mutable, per-object state
Every field declared in an interface is implicitly public static final (a constant); interfaces still cannot hold ordinary mutable instance state the way a class can.
More OOP with C++ and Java questions
- A class overrides equals() but does not override hashCode(). What bug does this risk for instances used as keys in a HashMap or stored in…
- Which run-time mechanism lets a call through a base-class pointer invoke the correct derived-class override in C++?
- cpp struct A { void f() { cout << "A::f"; } }; struct B : A {}; struct C : A {}; struct D : B, C {}; int main() { D d; d.f(); } What…
- Why can a private method in a Java superclass never be overridden by a subclass, only unintentionally hidden (i.e. always statically bound)?
- cpp struct A { int x = 1; }; struct B : A {}; struct C : A {}; struct D : B, C {}; int main() { D d; // d.x ? } Without virtual…
- Why did Java and C# choose single inheritance of classes plus multiple inheritance of interfaces, instead of C++-style full multiple…