Skip to main content

Overloading and Overriding with Polymorphism in java

Polymorphism is a Greek word. Poly means many moprhism means form. In java one form is using for multiple uses is called polymorphism. It is mainly two types. That are called
            •  Compile time polymorpshism/static polymorpshism
            • Runtime polymorphism/dynamic polymorphism

          Static polymorphism is called the overloading and dynamic polymorphism is called overriding in java.  At the time of method overloading java compiler will understand which  method is calling, is called static polymorphism. At the time of compilation java compiler will not identifies which method is calling exactly, that will understand JVM at the time of runtime is called Overriding in java.

            Overloading again two types, that are method overloading and constructor overloading. But, constructors we can't override in java, because of ambiguity.

Method Overloading: Which of two or more methods declared with the same method name with different signature is called as method overloading.

Here is the example for showing simple method overloading:
package com.javatbrains.polymorpsism;

public class MethodOverloading {

     public void add(){
          System.out.println("No parameters");
     }
    
     public void add(int a){
          System.out.println("Single parameter: "+a);
     }

     public static void main(String[] args) {
          MethodOverloading mo = new MethodOverloading();
          mo.add();
          mo.add(10);
     }

}
OutPut:
     No parameters
     Single parameter: 10


Here is the another example for method overloading:

package com.javatbrains.polymorphism;

public class MethodOverloading1 {
    
     public void meth(int a){
          System.out.println("First method value: "+a);
          float f = meth(a, 20.0f);
          System.out.println("Second method value: "+f);
     }
    
     public float meth(int a, float b){
          float i = a+b;
          return i;
     }
     public static void main(String[] args) {
          MethodOverloading1 mo1 = new MethodOverloading1();
          mo1.meth(10);
     }

}
OutPut:
     First method value: 10
     Second method value: 30.0

Here we will see how to overload the main method:

package com.javatbrains.polymorphism;

public class OverloadingMain {
    
     public static void main(){
          System.out.println("Inside the no parameter");
          main(1);
     }
    
     public static int main(int a){
          System.out.println("Inside the single parameter");
          return a;
     }
    
     public static void main(String[] args) {
          System.out.println("Inside the array parameter");
          main();
     }

}
OutPut:
     Inside the array parameter
     Inside the no parameter
     Inside the single parameter

Constructor Overloading: Two or more constructors declared with same name and different signature is called constructor overloading.

package com.javatbrains.polymorphism;

public class ConstructorOverloading {

     public ConstructorOverloading(){
          System.out.println("Inside the default constructor");
     }
    
     ConstructorOverloading(float a){
          System.out.println("Inside the second constructor");
     }
     public static void main(String[] args) {
          //Default constructor will call at the time of creating first object
          ConstructorOverloading first = new ConstructorOverloading();
          //parameter constructor will call at the time of creating second constructor
          ConstructorOverloading second = new ConstructorOverloading(15.0f);
     }

}
OutPut:
     Inside the default constructor
     Inside the second constructor

         
          In the above example will shows how to do constructor overloading in java for class ConstructorOverloading.java. There are two constructors called one is default constructor and another one is parametrised constructor which contains one or more parameters.

Points to remember about Overloading:
            • Methods signature should be different
            • method names should be same in overloading, otherwise those methods will not be under overloading.
            • Return type will be same or not.

The above all conditions also applicable for constructor overloading. But, constructors doesn't have any return type.

Here is the overloading failure scenario:

package com.javatbrains.polymorphism;

public class FailureOverloading {

     public static void add(int i, float j) {
          System.out.println("First method: " + (i + j));
     }

     public static void add(float i, int j) {
          System.out.println("Second method: " + (i + j));
     }

     public static void main(String[] args) {
          add(10, 10);  //Failure because of ambiguty
     }

}
OutPut:
    Compilation fails because of ambiguty. While passing the values from main method you need to specify which one int and float.

Overriding in java:
          Which of two or more methods declared with same name and same method signature is called method overriding. Overriding will be happens in between the child and parent classes.

Points to remember about Overriding:
            • Methods name should be same
            • Methods signature should be same
            • Methods return type should be same
            • Static method should not be overridden
            • Cannot override final method
            • Cannot override constructors

Here is the signature to understand about the overriding in java.

    package com.javatbrains.polymorpshism;

    class A{
        void abc(){}
    
        void xyz(int a){}
    }

    class B extends A{
        void abc(){}
    
        void xyz(int a){}
    }


For example, see the below example:

package com.javatbrains.polymorphism;

class Movie {
    
     public void actor() {
          System.out.println("Inside movie class actor");
     }

     public String actress(String name) {
          System.out.println("Inside movie class actress");
          return name;
     }
}

class Theater extends Movie {
     @Override
     public void actor() {
          super.actor(); //For calling super class method in subclass
          System.out.println("Inside theater class actor");
     }

     @Override
     public String actress(String name) {
          super.actress(name); //For calling super class method in subclass
          System.out.println("Inside theater class actress: "+name);
          return name;
     }
}

public class Overriding {

     public static void main(String[] args) {
          Movie m = new Theater();
          m.actor();
          m.actress("Kajol");
     }

}
OutPut:
     Inside movie class actor
     Inside theater class actor
     Inside movie class actress
     Inside theater class actress: Kajol
         
          Above example shows you how to do method overriding in java. There are two classes which represents the parent called Movie and child class as Theater. This example also shows you how to use inheritance in java. Here,  Theater class is inheriting Movie class and all the Movie class methods are overriding in Theater class.

          There I am using the super keyword for calling the super class method or variable from subclass.

          If you want to know how to override the interface methods in java, follow with the interfaces post in the same blog.

Related Posts:

Comments

Popular posts from this blog

JNDI configuration for Tomcat 9 with Oracle

In this article, I am going to place the required source code to get data from the table by using the JNDI configuration. Below are the environment details that I have configured currently. Windows - 7 Oracle - 10g Tomcat - 9 JDK - 8 Eclipse Oxygen Ojdbc6 jar required First, we need to create the Dynamic Web Project. If you don't know how to do <Click Here>. I have faced a lot of issues before getting the output like 405, No driver class to load, etc. I am using JSP & Servlets in the current explanation. Before started writing the application logic, we need to do the below configuration in the installed tomcat directory. Place OJDBC6.jar in the Tomcat LIB directory. Add this Resource under <GlobalNamingResources> in Server.xml file which is present under the conf directory in Tomcat. < Resource name = "jdbc/myoracle" global= "jdbc/myoracle" auth = "Container" type= "javax.sql.DataSource" driverClass...

Prime, Fibonacci and Factorial number with example in java

Prime number, Fibonacci series and Factorial number programs are most commonly asked questions in interview. Read this article to know what is and how to write programs for prime number, fibonacci series and factorial number. Prime Number: prime number is natural number greater than 1 that has no positive divisor other than 1 and itself. A natural number greater than 1 is not a prime number, is called Composite number . For example, 7 is a prime number. Because it can divide with 1 and 7 only. Where as 8 is composite number. Since it has the divisor 2 and 4 in addition to the 1 and 8. The below example represents the finding the passing number is prime number or not. If the passing number is prime number it will print true otherwise it will print false. package com . javatbrains . practice ; public class PrimeNumber { public boolean isPrimeNumber ( int number ) { if ( number <= 1 ) return false ; // There's only one ...

JVM, JRE and JDK in Java

JVM, JRE and JDK are the most basic common concepts to know in java. These are the basic features to understand how Java architecture works? JVM stands for Java Virtual Machine, which doesn't have any physical directories created in java installation. JRE stands for Java Runtime Environment, which creates the directory under Java installation path and also present in JDK. JDK stands for Java Development Kit, which creates the directory in Java installation path and also it has it's own JRE. Since we have already learn that Java is platform independent means if we have implemented any of the java class in one environment, it will be executed in any other environment and provides the same output. But, JVM, JRE and JDK all are platform dependent . So that, for windows, linux, unix, mac, solaris..etc has it's own JVM, JRE and JDK. One will be not compatible with other environments. While installing the Java, we might come to know a bit about JRE and JDK. But, JVM is the other...