JavaRush /Java Blog /Random EN /Coffee break #139. Overloading and Overriding in Java. 5 ...

Coffee break #139. Overloading and Overriding in Java. 5 Java Methods You Didn't Know About

Published in the Random EN group

Overloading and Overriding in Java

Source: Medium In this article, you will learn about the Overriding and Overloading methods in the Java language. Due to the fact that both of these terms are often confused with each other, it is worth clearly understanding the purpose of each method and their options for application. Coffee break #139.  Overloading and Overriding in Java.  5 Java Methods You Didn't Know About - 1

Overloading

Using more than one method with the same name but with different parameters in the same class or method between superclass and subclass in Java is called Overloading. To avoid this, a single method is used instead of many that perform similar actions. Let's explain with an example:
public class MethodOverloading {

    public static void main(String[] args){
        MethodOverloading operation = new MethodOverloading();
        operation.mod(12,4);
        operation.mod(12.4,4.2);
    }

    void mod(double a, double b){
        System.out.println(a % b);
    }

    void mod(int a, int b){
        System.out.println(a % b);
    }
}
In this code, the operation method is overloaded. Methods with the same names accept parameters of different types. The choice of mode is determined separately for the int and double parameters . When we run the program, operation.mod (12,4) runs void.mod (int a, int b) and operation.mod (12.4,4.2) runs void.mod (double a, double b) .

Overriding

In Java, we can create a superclass and subclasses that inherit from that class. These subclasses can override and replace the methods of the parent class that they inherit. This is done using the Overriding method. This can be better understood with an example:
public class MethodOverriding {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.sound();

        Bee bee = new Bee();
        bee.sound();
    }
}

class Animal {
    void sound(){
        System.out.println("Animal sounds");
    }
}

class Cat extends Animal{
    @Override
    void sound() {
        System.out.println("Cat : meow meow");
    }
}

class Bee extends Animal{
    @Override
    void sound() {
        System.out.println("Bee : buzz buzz");
    }
}
This code example creates a superclass named Animal and subclasses named Cat and Bee that inherit from that superclass. The sound method in the superclass is overridden. Note: The separation of overloaded methods occurs at the compilation stage. The separation of overridden methods occurs at runtime.

5 Java Methods You Didn't Know About

Source: Javarevisited The Java development ecosystem has many tools available that programmers can import and use in their programs. These include built-in classes and methods. They significantly simplify the programmer’s work and allow them to better understand and write code. Every developer should know about them. Here are 5 Java methods that are quite rare, but can be very useful in your work.

1. decrementExact

decrementExact() is a basic Java function from the Math class that decrements/subtracts a given argument (number) by one and returns the result. This function is the opposite of the incrementExact() function . For example, if the given argument is 11, then the result is 10. If decrementing an argument causes its data type to overflow, an exception is thrown. Therefore, it is important to be careful when using this function, especially for large numbers. Typically, integers are used for this function. Syntax:
Math.decrementExact(number);
Example:
System.out.println(Math.decrementExact(11));
// Output: 10

2.getAsDouble

getAsDouble() is a method belonging to the OptionalDouble class . An OptionalDouble object is one that can potentially hold a double number. Methods in the class can be used to operate on a double value present in an object, or to indicate that the double value is not contained at all. getAsDouble() is one such method and it returns the double value if present. Otherwise, a NoSuchElementException is thrown . Syntax:
OptionalDoubleObject.getAsDouble();
Example:
OptionalDouble num = OptionalDouble.of(15.0);
System.out.println(num.getAsDouble());
// Output: 15.0

3. absExact

The absExact() method is similar to the abs() function in the Math class . It returns the absolute value of a number, which is the positive value of the number regardless of its sign. The only difference is that it only does this if it is exactly represented as its data type ( int or long ). If the result of the return value exceeds the original data type, an ArithmeticException is thrown . Syntax:
Math.absExact(number);
Example:
System.out.println(Math.absExact(-11));
// Output: 11

4.endsWith

endsWith() is a built-in string method that returns a boolean value depending on whether the given string ends with a certain suffix (end word/string) in the parameters. This method is the opposite of the startsWith() method , which many developers are probably familiar with. Syntax:
String.endsWith(String suffix);
Example:
String phrase = "I like bananas";
System.out.println(phrase.endsWith("bananas")); // true
System.out.println(phrase.endsWith("Tandrew")); // false
/* Output:
true
false
*/

5. divideUnisgned

The divideUnsigned() method is a method from the Integer class that allows you to divide two numbers and return the result of the division. Unsigned integers, compared to regular signed integers, can only represent positive numbers. Both unsigned integers and signed integers have the same number of numbers in their range (the size of the range is 65,536 numbers). However, since unsigned integers cannot be negative, their maximum value in the positive range is much higher than the maximum value for a regular signed integer. To simplify this, we can instead look at the example of signed and unsigned bytes. Bytes have a range of 256 numbers. A regular byte can have a value from -128 to 127. However, an unsigned byte can have a value from 0 to 255. Otherwise, the function works exactly the same as regular division. Syntax:
Integer.divideUnsigned(int dividend, int divisor);
Example:
int dividend = 10;
int divisor = 5;
int quotient = Integer.divideUnsigned(dividend, divisor);
System.out.println(quotient);
// Output: 2

Conclusion

Here is a summary of the functions and methods discussed in this article:
  • decrementExact - decreases/subtracts a given number by 1

  • getAsDouble - part of the OptionalDouble function , returns a number with a double value or indicates its absence

  • absExact - returns the absolute value of a number if it can be represented as the original data type

  • endsWith() - returns a boolean value depending on whether the specified suffix exists in the given string

  • divideUnsigned() - performs normal division, returns the result of dividing a number

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION