Method Overloading & Overriding in Java

In this article we are going to discuss two important concepts of object oriented programming which used to achieve Polymorphism in java.

Polymorphism

Polymorphism refers to the ability to appear in many forms. In other words the ability of a reference variable to change behavior according to what object instance it is.

Types of Polymorphism

  1. Overloading
  2. Overriding

Overloading

  1. Two or more methods within the same class that share the same name with different parameter list.
  2. The overloaded methods must differ in the type and/ or number of their parameters.
  3. Overloaded may have different return types.
  4. Constructors can also be overloaded.

Method Overloading

class MotorBike{ private String startMethod = “Kick”; public void start(){ System.out.println(startMethod + ” starting ….”); } public void start(String method){ this.startMethod = method; System.out.println(startMethod + ” starting ….”); }}public class DemoApp{ public static void main(String arg){ MotorBike motorBike = new MotorBike(); motorBike.start(); motorBike.start(“Self”); }}/*********************** OUT PUT ****************************/Kick starting ….Self starting ….Process finished with exit code 0

Constructor Overloading

class Fruit{ private String code; private String name; //Default constructor public void Fruit(){} //Overloaded constructor public void start(String code, String name){ this.code= code; this.name = name; }}

Overriding

  1. Override methods are redefined within an inherited or subclass.
  2. They have same method signature in sub classes with different method body.

class MotorBike{ public void start(){ System.out.println(“Please use kick paddle to start”); }}class SelfStartMotorBike extends MotorBike { //redefine the method body public void start(){ System.out.println(“Please use self start button to start”); }}public class DemoApp { public static void main(String arg){ SelfStartMotorBike motorBike = new SelfStartMotorBike(); motorBike.start(); }}/*********************** OUT PUT ****************************/Please use self start button to startProcess finished with exit code 0

this and super Keyword

Keyword this

  1. Constructors and Methods use this keyword differently.
  2. A Method uses this to refer to the instance of the class that is executing the method.
  3. A Constructor uses this to refer to another constructor in the same class with different parameter list(overloading).
  4. Static Methods/ Variables do not use this because they do not belongs to a class instance.

Keyword super

  1. Constructors and Methods both use super to refer superclass, but in different ways.
  2. A Method uses super to execute an overridden method in the superclass.

class Bike{ private String color; public Bike(String colr){ this.color = colr; } public String getColor(){ return color; }}class MotorBike extends Bike { private String startMethod = “Kick”; public MotorBike(){ this(“Self Start”); // must be the first statement } public MotorBike(String method){ super(“Red”); //must be the first statement this.startMethod = method; } public void display(){ System.out.println(“Colour :”+super.getColor()); System.out.println(“Start Method:”+this.startMethod); }}public class DemoApp { public static void main(String arg){ MotorBike motorBike = new MotorBike(); motorBike.display(); }}/*********************** OUT PUT ****************************/Colour :RedStart Method:Self StartProcess finished with exit code 0

As a summary, in this article we discussed overloading and overriding with coding examples. Moreover some keywords which are related topolymorphism is also discussed.

Good Luck !!!!!!

1

No Responses

Write a response