Mostrando postagens com marcador overloading. Mostrar todas as postagens
Mostrando postagens com marcador overloading. Mostrar todas as postagens

Method Overriding and Overloading

Method overriding and method overloading are two important concepts in object-oriented programming, particularly in languages like Java and C++.


Method Overriding

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the subclass has the same name, return type, and parameters as the method in the superclass. By overriding a method, the subclass can provide its own implementation of the method, which may differ from the implementation in the superclass.


Key points about method overriding:

1. Same Signature: The overriding method must have the same name, return type, and parameter list as the method in the superclass.

2. Inheritance: Overriding occurs in a subclass inheriting from a superclass.

3. Runtime Polymorphism: When you call an overridden method on an object, the JVM determines which version of the method to execute based on the type of the object, not the reference type.


Example in Java:

class Animal {
    void makeSound() {
        System.out.println("Some generic sound");
    }
}

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Bark");
    }
}


Method Overloading

Method overloading occurs when a class has multiple methods with the same name but different parameter lists. The methods can differ in the number or type of parameters. Overloaded methods enable you to use the same method name for different behaviors.


Key points about method overloading:

1. Same Name: Overloaded methods have the same name but different parameter lists.

2. In the Same Class: Overloading occurs within the same class.

3. Compile-time Polymorphism: The decision on which overloaded method to call is made at compile time based on the method signature.


Example in Java:

class Calculator {
    int add(int a, int b) {
        return a + b;
    }
    
    double add(double a, double b) {
        return a + b;
    }
}


In this example, there are two `add` methods with different parameter types (int and double), making them overloaded methods.

In summary, method overriding is about providing a new implementation for a method in a subclass, while method overloading is about providing multiple methods with the same name but different parameters within the same class. Both concepts contribute to the flexibility and readability of object-oriented code.

What is Polymorphism and how is it implemented in Java

Polymorphism is one of the four pillars of object-oriented programming (OOP) and refers to the ability for objects from different classes to be treated as objects from the same base class. Polymorphism in Java is implemented through two main techniques: method overloading (methods with the same name in the same class, but with different parameters) and method overriding (methods with the same signature in different classes, where the subclass provides its implementation itself).

Overloading

Method overloading allows a class to have multiple methods with the same name but different parameters. The Java compiler chooses which method to call based on the method signature (type and number of parameters).

Example of method overloading:

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public double add(double a, double b) {
        return a + b;
    }
}

Overriding

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its base class (superclass). The method signature in the subclass must be identical to the signature in the superclass.

Method overriding example:

public class Animal {
    public void makeSound() {
        System.out.println("Generic animal sound");
    }
}

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog's bark");
    }
}

Polymorphism example

public class PolymorphismExample {
    public static void main(String[] args) {
        Animal animal1 = new Animal();
        Animal animal2 = new Dog();

        animal1. makeSound();  // Output: Generic animal sound
        animal2. makeSound();  // Output: Dog's bark
    }
}

In this example, "animal2" is an instance of "Dog", but it is declared as an "Animal" type. When the "makeSom()" method is called, Java uses the specific implementation of the "Dog" class, showing how polymorphism allows different types of objects to be treated uniformly.

Polymorphism in Java is a powerful tool that promotes code reuse, flexibility, and extensibility in object-oriented systems.

HashMap in Java

Let’s break down how   HashMap   works internally (Java 8+ implementation). What Is a HashMap? HashMap<K, V>  is a  hash table–based  ...