+3 votes
156 views
What is overriding and what is the difference between overloading and overriding?
asked Oct 25, 2015 in Computer Science - IT by T.PRATHIBA
retagged Oct 26, 2015 by slow_learner | 156 views

1 Answer

+1 vote

Overriding & Overloading are examples of Polymorphism.

But c program does not support polymorphism hence i have retagged your question to c++.

overloading : to load to excess; overburden.


In programming , Method overloading occurs when two or more methods in the same class have the exact same name but different parameters. 

Example :
class OverloadingExample{  
static int add(int a,int b){return a+b;}  
static int add(int a,int b,int c){return a+b+c;}  
}  


overriding  : to extend beyond or spread over; overlap.


In programming,overriding we have same parameters rather different parameters as in overloading.
If a derived class requires a different definition for an inherited method, then that method can be redefined in the derived class. This would be considered overriding.

Example :

public class Parent {

 public int someMethod() {
   
   return 1;
       
    }
}

public class Child extends Parent{

 // this is method overriding:
 public int someMethod() {

    return 2;
       
    }

}

 

When overloading, one must change either the type or the number of parameters for a method that belongs to the same class. 

Overriding means that a method inherited from a parent class will be changed.

But, when overriding a method everything remains exactly the same except the method definition – basically what the method does is changed slightly to fit in with the needs of the child class.

 But, the method name, the number and types of parameters, and the return type will all remain the same.

Detailed Explanation at :
http://free-courses.online/mod/lesson/view.php?id=25

answered Oct 26, 2015 by slow_learner
Overriding is the one whose function name and parameters are same is what I understood from your explanation, but could you give me an example program instead of the general program which you have given , so that I could understand the concept still better like how the function is identified as that in parent class or child class?
that is not a general program..its an example in java..kindly give a try..and let me know errors..:)
I tried the program given below in java and could not find what is the error in it.

import java.io.*;
class a
{
    public void enggforum()
    {
        System.out.println("hello");
    }
}
class b extends a
{
    public void enggforum()
    {
        System.out.println("hai");
    }
}
public class override {
    public static void main(String args[])
    {
        b o=new b();
        o.enggforum();
    }
}
 
What is the error in this?
Good ...what is your expected output for your pgm ? hai or hello ?
I think only the function in derived class (child class) will be called, then it should be hai, isn't. If wrong please send the correct answer with explanation.
Good :)

import java.io.*;
class a
{
    public void enggforum()
    {
        System.out.println("hello");
    }
}
class b extends a
{
    public void enggforum()
    {
        System.out.println("hai");
    }
}
class override {
    public static void main(String args[])
    {
        b o=new b();
        o.enggforum();
    }
}