Show some examples of overloading and overriding in Java.
Certainly! Here are some examples of overloading and overriding in Java:
- Overloading: Overloading occurs when a class has multiple methods with the same name but different parameters.
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Calculator</span> {
<span class="hljs-type">int</span> <span class="hljs-title function_">add</span><span class="hljs-params">(<span class="hljs-type">int</span> a, <span class="hljs-type">int</span> b)</span> {
<span class="hljs-keyword">return</span> a + b;
}
<span class="hljs-type">int</span> <span class="hljs-title function_">add</span><span class="hljs-params">(<span class="hljs-type">int</span> a, <span class="hljs-type">int</span> b, <span class="hljs-type">int</span> c)</span> {
<span class="hljs-keyword">return</span> a + b + c;
}
}
<span class="hljs-comment">// Usage</span>
<span class="hljs-type">Calculator</span> <span class="hljs-variable">calculator</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Calculator</span>();
<span class="hljs-type">int</span> <span class="hljs-variable">result1</span> <span class="hljs-operator">=</span> calculator.add(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>); <span class="hljs-comment">// Calls the first add method</span>
<span class="hljs-type">int</span> <span class="hljs-variable">result2</span> <span class="hljs-operator">=</span> calculator.add(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>); <span class="hljs-comment">// Calls the second add method</span>
In the example above, the Calculator class has two add methods. The first method takes two integers as parameters and returns their sum, while the second method takes three integers and returns their sum. The appropriate method is called based on the number and types of arguments passed.
- Overriding: Overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass.
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Vehicle</span> {
<span class="hljs-keyword">void</span> <span class="hljs-title function_">accelerate</span><span class="hljs-params">()</span> {
System.out.println(<span class="hljs-string">"Vehicle is accelerating."</span>);
}
}
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Car</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Vehicle</span> {
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">void</span> <span class="hljs-title function_">accelerate</span><span class="hljs-params">()</span> {
System.out.println(<span class="hljs-string">"Car is accelerating."</span>);
}
}
<span class="hljs-comment">// Usage</span>
<span class="hljs-type">Vehicle</span> <span class="hljs-variable">vehicle</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Vehicle</span>();
vehicle.accelerate(); <span class="hljs-comment">// Prints "Vehicle is accelerating."</span>
<span class="hljs-type">Car</span> <span class="hljs-variable">car</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Car</span>();
car.accelerate(); <span class="hljs-comment">// Prints "Car is accelerating."</span>
In this example, the Vehicle class has a method called accelerate(). The Car class extends Vehicle and overrides the accelerate() method with its own implementation. When the accelerate() method is called on an instance of Car, it executes the implementation defined in the Car class instead of the one in the Vehicle class.
These are just a few examples of overloading and overriding in Java. They are important concepts that allow for flexibility and polymorphism in object-oriented programming.
