Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Difference Between method overloading and method overriding

Method Overloading in Java

  • When a class has two or more methods with the same name but different parameters, the call to the appropriate method is made based on the parameters passed (or respective method body will be bonded with the calling line dynamically). Method overloading is the term for this mechanism.


Table Of Content

  • Definition
  • Comparison Chart
  • Key Differences


Features/advantage of Method Overloading

  • Static Polymorphism : Method overloading use static polymorphism, where the binding of methods occurs during the compilation process. This eliminates the need for additional processes like binding and checking at runtime.
  • Efficiency : Using method overloading, the binding of methods is done at compile-time, It help in improving performance as compared to dynamic polymorphism. This make code execution faster.
  • Improved Readability : By using method overloading, developers can provide multiple methods with the same name but different parameter lists. This makes the code more readable and natural as the method names can reflect the functionality they perform.
  • Flexibility : Method overloading allows flexibility in parameter handling. Developers can define multiple methods with the same name but different types, numbers, or sequences of parameters. This enables the code to handle different scenarios or input variations. This eliminates need for separate method names.
  • Code Reusability : Method overloading promotes code reusability by allowing developers to use the same method name for similar operations. This reduces code duplication and improves maintainability.
  • Enhanced Error Handling : Method overloading helps in catching ambiguity errors. When the return value of a method is changed slightly, method overloading detects the ambiguity and raises an error, making it easier to identify and resolve such issues.



Method overloading example
 // Method overloading example  
class Product {
    int Multiply(int p, int q){  
     return p * q;
  }

  float Multiply(float p, float q,float r){   
	  return p * q * r;
  }
}
class Overloading {
 public static void main(String[] args){
    Product product=new Product();
     System.out.println(product.Multiply(2, 4));
     System.out.println(product.Multiply(5.2f, 6.3f,2.3f));
 }
}




Output:

8 
75.34799 


Method Overriding in java

    In method overriding, methods with the same name and parameters are present in both the superclass and the subclass. Based on the object used to call the method, JVM invokes the appropriate procedure. The return types for overriding should also be the same.


Features/advantage of Method Overriding

  • Method overriding simplifies the process of writing generic code by allowing the child class to redefine and customize the implementation of a method inherited from the parent class.
  • The super keyword can be used to call the overridden methods of the parent class and it provides multiple implementations of the same method.
  • Using method overridding, a class's behavior is specified.


Java method overridden example
 // Java method overridden example  
class Animal {
    void Move(){
        System.out.println("Animals is Moving");
    }
    void Eat(){
        System.out.println("Animals is Eating");
    }
}
  
class Dog extends Animal {
    @Override
    void Move(){ // here overriding take place
        System.out.println("Dog is Moving");
    }
    @Override
    void Eat(){
        System.out.println("Dog is Eating");
    }
}
class DockerTpoint {
    public static void main(String[] args)
    {
    	Animal animal = new Animal(); // If a Animal type reference refers to a Animal object, then Animal
    	animal.Move();        // move or eat is called
    	animal.Eat();
    	
    	Animal animal2 = new Dog(); // If a Animal type reference refers
        animal2.Move();// to a Dog (Child) object Dog's eat() & Move(), 
        animal2.Eat(); // then it is called. This is called RUN TIME POLYMORPHISM. 
    }
}



Output:

Animals is Moving 
Animals is Eating 
Dog is Moving 
Dog is Eating 

Comparison Chart

Difference Between Method Overloading and Method Overriding are mention below


S.N Method Overloading Method Overriding
1. Method overloading, also referred to as compile-time polymorphism Method overriding, also referred to as runtime polymorphism
2. Overloading methods improves the program's readability. The specific implementation of a method that is already provided by its parent class or superclass is granted by method overriding.
3. It occurs within the class. It is carried out by two classes that are related through inheritance.
4. Method overloading does not always require the use of inheritance. Inheritance is always required for method overriding.
5. Methods must have the same name and various signatures when overloading. Methods used in method overriding must share the same name and signature.
6. Methods that are private or final may be overloaded. Methods that are private or final cannot be overridden.
7. In method overloading, the argument list should be different. The argument list for method overriding should match.
8. Static binding is being used for method overloading. Dynamic binding is being used for method overriding.





The Key Distinctions Between Method Overloading and Method Overriding

  • Object-oriented programming concepts like overloading and overriding are used to make programs easier to read and reuse. Static polymorphism is a type of method overloading. Method overloading allows us to define multiple methods with the same name but different parameters.
  • The prototype of an overloaded function varies depending on the type and amount of parameters. The prototype of the overridden function, on the other hand, remains unchanged. Because an overridden method performs different actions for each class to which it belongs while using the same type and number of parameters.
  • The type or number of parameters provided to the function determines which overloaded function is invoked. Which overridden function is invoked is determined by the class object reference of the pointer that invoked the function.
  • Overloading achieves early binding by resolving which overloaded function will be run during build time. Overriding achieves late binding by resolving which overridden function will be invoked during runtime.



  • The compilation time resolves which overloaded function is executed. The override function to be invoked is determined at runtime.
  • We can overload but not override the constructors.
  • Destructors cannot be overloaded, but they can be overridden.
Difference between Early and Late Binding in Java Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Difference between Early and Late Binding in Java
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.