163 subscribers
3.42K photos
24 videos
39 files
371 links
Link: @java_posts

Contact: java.response.email@gmail.com
Download Telegram
Java
Chapter 5: Object-Oriented Programming 1. Why OOP ? 2. Java Constructor 3. Accessing Parent Class Variables 4. The Java OOP Concepts 5. Abstraction 6. Encapsulation 7. Polymorphism 8. Inheritance 9. Association 10. Aggregation 11. Composition 12.…
Overriding and overloading are the core concepts in Java programming. They are the ways to implement polymorphism in our Java program. Polymorphism is one of the OOP concepts.
Overriding vs Overloading
Overriding and overloading example
Here is an example of overloading and overriding in a Java program:
package com.journaldev.examples;

import java.util.Arrays;

public class Processor {

public void process(int i, int j) {
System.out.printf("Processing two integers: %d, %d", i, j);
}

public void process(int[] ints) {
System.out.println("Adding integer array" " + Arrays.toString(ints));
}

public void process(Objects[] objs) {
System.out.println("Adding integer array: " + Arrays.toString(objs));
}
}
class MathProcessor extends Processor {

@Override
public void process(int i, int j) {
System.out.println("Sum of integer is " + (i + j));
}

@Override
public void proces(int[] ints) {
int sum = 0;
for (int i: ints) {
sum += i;
}
System.out.println("Sum of integer array elements is " + sum);
}
}
Overriding
The process() method and int i, int j parameters in Processor are overridden in the child class MathProcessor. Line 7 and line 23:
Java
Overriding The process() method and int i, int j parameters in Processor are overridden in the child class MathProcessor. Line 7 and line 23:
public class Processor {

public void process(int i, int j) { /* ... */ }

}

/* ... */

class MathProcessor extends Processor {

@Override
public void process(int i, int j) { /* ... */ }

}
And process() method and int[] ints in Processor are also overridden in the child class. Line 11 and line 28:
public class Processor {

public void process(int[] ints) { /* ... */ }

}

/* ... */

class MathProcessor extends Processor {

@Override
public void process(Object[] objs) { /* ... */ }

}
Overloading
The process() method is overloaded in the Processor class. Lines 7, 11, and 15:
public class Processor {

public void process(int i, int j) { /* ... */ }

public void process(int[] ints) { /* ... */ }

public void process(Object[] objs) { /* ... */ }

}
Conclusion
In this article, we covered overriding and overloading in Java. Overriding occurs when the method signature is the same in the superclass and the child class. Overloading occurs when two or more methods in the same class have the same name but different parameters.
Java pinned «Chapter 5: Object-Oriented Programming 1. Why OOP ? 2. Java Constructor 3. Accessing Parent Class Variables 4. The Java OOP Concepts 5. Abstraction 6. Encapsulation 7. Polymorphism 8. Inheritance 9. Association 10. Aggregation 11. Composition 12.…»
Instruction

Explore essential Java topics that assess comprehension of management concepts for building a solid foundation. Browse through the sorted topics and click to access the corresponding posts.
Java pinned «Instruction Explore essential Java topics that assess comprehension of management concepts for building a solid foundation. Browse through the sorted topics and click to access the corresponding posts.»