Govt. Exams
Entrance Exams
The same withdraw() call behaves differently for SavingsAccount and CurrentAccount based on the actual object type at runtime. This is polymorphism with runtime (dynamic) binding.
The Factory Pattern uses a separate class (factory) with methods to create and return objects. This decouples object creation from usage.
This demonstrates dynamic (runtime) polymorphism. The actual object type (ElectricCar) determines which method is called, not the reference type (Vehicle).
Java 17 introduced 'sealed' classes with the 'permits' clause to explicitly specify which classes can extend a sealed class, providing better control over inheritance.
An abstract class allows you to define both abstract methods (to be overridden) and concrete methods (shared implementation), which is ideal for this scenario.
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.