Skip to main content

Overloading & Overriding in Java

Overloading & Overriding  in Java

Overloading
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded. This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures.


Overriding
If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.


* Main difference comes form the fact that method overloading is resolved during compile time, while method overriding is resolved at runtime
* For overriding both name and signature of method must remain same, but in for overloading method signature must be different
* Call to overloaded methods are resolved using static binding while call to overridden method is resolved using dynamic binding in Java


 

class Fruits{
 void display(Fruits frts) {System.out.print("Fruits");}
  }
  class Mango extends Fruits{
  void display(Mango mngo) {System.out.print("Mango");}
  }
  class Alphonso extends Mango{
  void display(Alphonso alf) {System.out.print("Alphonso");}
  }

 public class test {

 public static void main(String[] args) {

  Fruits c1 = new Alphonso(); Alphonso c2 = new Alphonso();c1.display(c2);

 }}



 Output of this code is 'Fruits'.



But if I modify class Fruits as:
 class Fruits{
 void display(Alphonso frts) {System.out.print("Fruits");}
  }
 
It will display "Alphonso"
 
 


The type of c1 is Fruits. Therefore when working out which method signature it's going to call, the compiler can only look at methods declared in Fruits.
So in the first case, the compiler is going to call display(Fruits); in the second case, the compiler is going to call display().
Now in the first case, the display(Fruits) method is never overridden, so actually the execution-time type of c1 is irrelevant.
In the second case, display(Alphonso) is overridden by alf, so the implementation in alf is called because the execution-time type of c1 is alf.


So remember:
  • Overload resolution (which method signature is called) is determined at compile-time, based on the compile-time types of both the method target and the argument expressions
  • The implementation of that method signature (overriding) is based on the actual type of the target object at execution time.

Comments

Popular posts from this blog

OBJECT class in Java

OBJECT class in Java : Object is at the top of class hierarchy in java. Every class in the Java system is a descendent (direct or indirect) of the Object class. The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class. Mainly below methods are provided by Object class : public String toString() returns the string representation of this object. protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object. public boolean equals(Object obj) compares the given object to this object. public int hashCode() returns the hashcode number for this object. public final Class getClass() returns the Class class object of this object. The Class class can further be used to get the metadata of ...

Java Priority Queue sort using lambda expression

Priority Queue :     In Java  Priority Queue  is a  queue  which keeps its elements sorted as per their natural order( example in ascending orders for Integer, alphabetical a-z for alphabets) or using a custom Comparator at the time of creation you can custom sort it. It has most method  similar to a queue  add, clear, poll, peek As priority queue have to compare elements to keep in order so elements must be comparable otherwise, it will throw ClassCastException .  As null can not be compared so you are not allowed to insert null too A program to sort priority queue using Lambda function in Java           c lass   PriorityQueueLambdaJava { public List<Integer> KFrequent( int [] nums, int k) { Map<Integer, Integer> map = new HashMap<>(); for ( int j: nums) { map .put(j, map .getOrDefault(j, 0 ) + 1 ); } PriorityQueue<Map.Entry<Integ...

Important Keywords: Static, Continue,Break:

Important Keywords: Static, Continue,Break:  Static: The static keyword in java is used for memory management mainly.  The static keyword members belongs to the class rather than instance of the class. Following members can be declared as static: variable (also known as class variable) method (also known as class method) block nested class  static variables : We can use static keyword with a class level variable. A static variable is a class variable and doesn’t belong to Object/instance of the class. Since static variables are shared across all the instances of Object, they are not thread safe . Usually static variables are used with final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it as: ClassName.variableName //static variable example private static int a ge; public static String name; static methods: A static method belongs t...