首页 > 编程语言 >Difference Between Method Overloading and Method Overriding in Java

Difference Between Method Overloading and Method Overriding in Java

时间:2022-11-28 03:00:15浏览次数:77  
标签:Overriding Java eat Dog same Method method class

The differences between Method Overloading and Method Overriding in Java are as follows:

Method Overloading

 

Method Overriding

Method overloading is a compile-time polymorphism. Method overriding is a run-time polymorphism.
It helps to increase the readability of the program. It is used to grant the specific implementation of the method which is already provided by its parent class or superclass.
It occurs within the class. It is performed in two classes with inheritance relationships.
Method overloading may or may not require inheritance. Method overriding always needs inheritance.
In method overloading, methods must have the same name and different signatures. In method overriding, methods must have the same name and same signature.
In method overloading, the return type can or can not be the same, but we just have to change the parameter. In method overriding, the return type must be the same or co-variant.
Static binding is being used for overloaded methods. Dynamic binding is being used for overriding methods.
Poor Performance due to compile time polymorphism. It gives better performance. The reason behind this is that the binding of overridden methods is being done at runtime.
Private and final methods can be overloaded. Private and final methods can’t be overridden.
Argument list should be different while doing method overloading. Argument list should be same in method overriding.

Method Overloading:

Method Overloading is a Compile time polymorphism. In method overloading, more than one method shares the same method name with a different signature in the class. In method overloading, the return type can or can not be the same, but we have to change the parameter because, in java, we can not achieve the method overloading by changing only the return type of the method. 

  <iframe data-google-container-id="2" data-is-safeframe="true" data-load-complete="true" frameborder="0" height="250" id="google_ads_iframe_/27823234/GFG_Desktop_PostContent_336x280_0" marginheight="0" marginwidth="0" scrolling="no" src="https://35f1864166b3ffffaa326923d70565ea.safeframe.googlesyndication.com/safeframe/1-0-40/html/container.html" title="3rd party ad content" width="300"></iframe>

Example of Method Overloading: 

  • Java
 
import java.io.*;    class MethodOverloadingEx {        static int add(int a, int b)             return a + b;      }        static int add(int a, int b, int c)     {         return a + b + c;     }        public static void main(String args[])     {           System.out.println("add() with 2 parameters");         System.out.println(add(4, 6));                    System.out.println("add() with 3 parameters");         System.out.println(add(4, 6, 7));     } }
Output
add() with 2 parameters
10
add() with 3 parameters
17

Method Overriding

Method Overriding is a Run time polymorphism. In method overriding, the derived class provides the specific implementation of the method that is already provided by the base class or parent class. In method overriding, the return type must be the same or co-variant (return type may vary in the same direction as the derived class). 

Example: Method Overriding

  • Java
  • C++
 
import java.io.*;    class Animal {        void eat()     {         System.out.println("eat() method of base class");         System.out.println("eating.");     } }    class Dog extends Animal {        void eat()     {         System.out.println("eat() method of derived class");         System.out.println("Dog is eating.");     } }    class MethodOverridingEx {        public static void main(String args[])     {         Dog d1 = new Dog();         Animal a1 = new Animal();            d1.eat();         a1.eat();            Animal animal = new Dog();         // eat() method of animal class is overridden by         // base class eat()         animal.eat();     } }
Output
eat() method of derived class
Dog is eating.
eat() method of base class
eating.
eat() method of derived class
Dog is eating.

Output explanation: Here, we can see that a method eat() has overridden in the derived class name Dog that is already provided by the base class name Animal. When we create the instance of class Dog and call the eat() method, we see that only derived class eat() method run instead of base class method eat(), and When we create the instance of class Animal and call the eat() method, we see that only base class eat() method run instead of derived class method eat(). 

标签:Overriding,Java,eat,Dog,same,Method,method,class
From: https://www.cnblogs.com/kungfupanda/p/16931225.html

相关文章

  • Java基础
    java基础java的基本程序设计结构第一个程序Main.javapublicclassMain{publicstaticvoidmain(String[]args){System.out.println("hello,world")......
  • 01.java设计模式之单例模式
    参考:https://blog.csdn.net/liu_da_da/article/details/125425561一、什么是单例模式保证一个类仅有一个实例,并提供一个访问它的全局访问点。二、单例的几种实现方式1......
  • Java: How To Count Words
    publicclassMain{publicstaticvoidmain(String[]args){Stringwords="OneTwoThreeFour";intcountWords=words.split("\\s").length;Sy......
  • Java: Files
    JavaFileHandlingThe File classfromthe java.io package,allowsustoworkwithfiles.importjava.io.File;//ImporttheFileclassFilemyObj=new......
  • java9
    Java9模块系统Java9最大的变化之一是引入了模块系统(Jigsaw项目)。模块就是代码和数据的封装体。模块的代码被组织成多个包,每个包中包含Java类和接口;模块的数据则包括资......
  • SpringBoot(四):java从配置文件中取值的方式
    一、SpringBoot项目中取yaml配置文件中的值application.yamltest:url:localhost:8080name:rootpassword:123456val:a:1b:2c:3TestC......
  • Java: Threads
    Threadsallowsaprogramtooperatemoreefficientlybydoingmultiplethingsatthesametime.CreatingaThreadTherearetwowaystocreateathread.Itcan......
  • Java: Regular Expressions
    Pattern Class-Definesapattern(tobeusedinasearch)Matcher Class-UsedtosearchforthepatternPatternSyntaxException Class-Indicatessyntaxe......
  • Java中使用正则表达式
    1、使用 java.util.regex.Pattern类的 compole(表达式)方法把正则表达式变成一个对象。//表达式对象:1个数字和1个字母连续Patternpattern=P......
  • Java入门代码练习
    一、第一个Java程序1、helloworldpublicclassHello{publicstaticvoidmain(String[]args){System.out.println("Helloworld!");}}2、变量i......