Govt Exams
The default (package-private) access modifier allows access to members from any class within the same package, but not from classes in other packages.
abstract class Animal { abstract void sound(); void sleep() { System.out.println("Zzz"); } }
class Dog extends Animal { void sound() { System.out.println("Bark"); } }
What can Dog objects do?
Dog inherits the concrete method sleep() from Animal and provides implementation for the abstract method sound(). Therefore, Dog objects can call both methods.
Abstract classes can have constructors to initialize instance variables. They can contain both abstract and concrete methods, and they cannot be directly instantiated.
Method overloading allows multiple methods with the same name but different parameters (number, type, or order). It is a compile-time (static) polymorphism.
interface I1 { void method(); }
interface I2 { void method(); }
class C implements I1, I2 { public void method() { System.out.println("Method"); } }
What will be the behavior?
When a class implements multiple interfaces with the same method signature, a single method implementation satisfies all interfaces. This is resolved at compile time.
'this' is a reference variable that refers to the current object instance. It is commonly used to distinguish instance variables from parameters with the same name.
Final methods cannot be overridden. The compiler will throw an error indicating that you cannot override a final method.
Interfaces cannot have instance variables (private or otherwise). They can only have constants (static final). Java 9+ allows private methods but not private instance variables.
'protected' members are accessible within the same package and also to subclasses in different packages. It provides more access than default but less than public.
Java 8 introduced default and static methods in interfaces. Any class implementing an interface must provide implementations for all its abstract methods (unless the implementing class is abstract).