Function overloading and constructor overloading in Java are techniques that allow multiple methods or constructors to have the same name but different parameters. Function Overloading: Function overloading occurs when multiple methods in the same class have the same name but differ in the number orRead more
Function overloading and constructor overloading in Java are techniques that allow multiple methods or constructors to have the same name but different parameters.
Function Overloading:
Function overloading occurs when multiple methods in the same class have the same name but differ in the number or type of their parameters. It allows a class to perform different tasks with the same method name, enhancing readability and usability. For instance, a class might have a method named `add` that adds two integers, another that adds two floats, and a third that concatenates two strings:
“`java
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public String add(String a, String b) {
return a + b;
}
}
“`
Constructor Overloading:
Constructor overloading is similar but applies to constructors. A class can have multiple constructors, each with a different parameter list. This allows objects of the class to be instantiated in different ways, providing flexibility in object creation. For example, a `Person` class might have multiple constructors:
“`java
public class Person {
private String name;
private int age;
public Person() {
this.name = “Unknown”;
this.age = 0;
}
public Person(String name) {
this.name = name;
this.age = 0;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
“`
In summary, function overloading and constructor overloading in Java enable multiple methods or constructors with the same name but different parameters, enhancing code flexibility and readability.
See less
Polymorphism in Object-Oriented Programming allows methods to do different things based on the object it is acting upon, even if they share the same name. It lets one interface be used for a general class of actions, making code more flexible and reusable. Imagine you have a book class with a methodRead more
Polymorphism in Object-Oriented Programming allows methods to do different things based on the object it is acting upon, even if they share the same name. It lets one interface be used for a general class of actions, making code more flexible and reusable.
Imagine you have a book class with a method called “summary()”. If you create two types of books, “novel” and “biography”, each type can have its own version of “summary()”. When you use the “summary()” method on a book, it will show the right summary based on whether the book is a “novel” or a “biography”. Even though you use the same method name, it does different things depending on the type of book.
In this simpler code, the “summar()” method in the “book” class provides a general description, while the “novel” and “biography” classes override it with their specific summaries.