Govt Exams
When no access modifier is specified, the member has package-private (default) access, visible only within the same package.
Inheritance models an 'is-a' relationship (Dog is-an Animal), while composition models a 'has-a' relationship (Car has-an Engine). Composition is often preferred for flexibility.
Covariant return types (Java 5+) allow a method to return a subtype of the parent class method's return type. For example, if parent returns Animal, child can return Dog (subclass of Animal).
Constructor execution follows the inheritance chain from top to bottom. super() is implicitly called, executing parent constructors first.
An abstract class with abstract methods enforces that subclasses must implement required behaviors while preventing direct instantiation of the base class.
interface A { void method(); }
interface B { void method(); }
class C implements A, B {
public void method() { }
}
Java allows implementing multiple interfaces even if they have the same method signature. The class provides a single implementation that satisfies both interfaces. This compiles successfully.
Using an interface with multiple implementations follows the Interface Segregation Principle and Strategy Pattern, making the system extensible and maintainable.
The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. Option B correctly represents this principle.
class Test {
static int x = 10;
public static void main(String[] args) {
System.out.println(x++);
}
}
The post-increment operator (x++) returns the value before incrementing. So it prints 10, and then x becomes 11.
Through inheritance, a Puppy reference can access all non-private methods from Puppy, Dog, and Animal classes through the inheritance chain.