Java Sample Code Examples
Java Sample Code Examples
Constructor chaining is a technique where a constructor calls another constructor in the same class or its superclass using this() or super(). Its significance lies in reusing constructor logic and ensuring orderly initialization. In classes A and B, chaining occurs with class B's constructor calling super() to invoke class A's constructor first. This ensures that all initialization steps defined in class A are executed before class B's constructor logic .
Attempting to override a final method in a subclass will result in a compile-time error, as final methods cannot be overridden. In the given example, the Parent class defines a final method display(). If the Child class tries to override this method, it will cause a compile error since final methods are intended to be non-overridable to maintain the method’s original behavior across subclasses .
Polymorphism allows a single function or method to operate in different data contexts. Dynamic method dispatch is a polymorphic behavior where the method to be executed is determined at runtime. With the Parent and Child classes, a Parent class reference (ref) points to a Child object. When ref.show() is called, the Child class's show() method executes due to dynamic dispatch, demonstrating runtime polymorphism, allowing behavior overriding and flexibility in extending class functionality .
Varargs (variable arguments) in Java allow a method to accept zero or more arguments of a specified type. In the VarargsSum program, the sum(int... numbers) method uses varargs to accept multiple integer inputs, making it flexible for summing an array of numbers. Inside the method, a loop iterates over the numbers, adding them to compute the total sum, which is then printed .
Method overloading occurs when multiple methods have the same name but different parameter lists within the same class. In the OverloadExample class, the display() method is overloaded with two versions: one accepts an integer parameter and the other a string. This allows the method to handle different types of input data, executing the corresponding logic for each data type when called .
A class method can be protected from being overridden by declaring it as final. This ensures that subclasses cannot modify the method’s behavior, preserving its intended functionality across all class hierarchies. In the Parent class, marking the display() method as final prevents subclasses like Child from altering or overriding it, which can be beneficial for maintaining consistent logic or adhering to specific business rules .
Static variables are shared among all instances of a class, whereas instance variables are unique to each instance. In the StaticVariableDemo class, the static variable count is incremented each time an instance of the class is created, and its value is shared across all instances. Therefore, after creating two instances, the count value is 2, illustrating that all instances reference the same static variable .
The HybridVehicle class demonstrates Java's multiple inheritance feature via interfaces. By implementing Engine and Battery interfaces, HybridVehicle inherits the behaviors defined by both, allowing a single class to use methods from multiple sources. This illustrates polymorphic capabilities in action, enabling HybridVehicle to start an engine and charge a battery, showing a real-world application of interfaces to simulate complex vehicle functionalities .
In the context of class inheritance, method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass. When class Dog, which extends Animal, overrides the sound() method, calling sound() on a Dog instance will execute the Dog's implementation ('Dog barks'), rather than the Animal's method ('Animal makes sound').
Encapsulation is a principle that restricts access to certain parts of an object and prevents external interference and misuse of class data by using access modifiers like private. The BankAccount class demonstrates encapsulation by making its accountNumber and balance private and exposing public methods (getAccountNumber, setAccountNumber, getBalance, setBalance) to access and modify these fields safely, thus maintaining integrity and security of the data .