What are overloading and overriding? How do we implement those in Java?

--Originally published at diegotc2016

 

Overloading: two methods are overloaded if  both methods have the same name but different argument types.

For example:

class DisplayOverloading
{
    public void disp(char c)
    {
         System.out.println(c);
    }
    public void disp(char c, int num)  
    {
         System.out.println(c + " "+num);
    }
}
class Sample
{
   public static void main(String args[])
   {
       DisplayOverloading obj = new DisplayOverloading();
       obj.disp('a');
       obj.disp('a',10);
   }
}

The output would be:

a
a 10

Overriding: is when someone declares a method in subclass which is already present in parent class.

For example:

class Human{
   public void eat()
   {
      System.out.println("Human is eating");
   }
}
class Boy extends Human{
   public void eat(){
      System.out.println("Boy is eating");
   }
   public static void main( String args[]) {
      Boy obj = new Boy();
      obj.eat();
   }
}

The output would be:

Boy is eating

What are overloading and overriding? How do we implement those in Java?

I got this information out of:

Method Overloading in Java with examples

Method overriding in java with example

http://javarevisited.blogspot.mx/2011/12/method-overloading-vs-method-overriding.html

What are overloading and overriding? How do we implement those in Java?


What are overloading and overriding? How do we implement those in Java?

What are overloading and overriding? How do we implement those in Java?

--Originally published at diegotc2016

 

Overloading: two methods are overloaded if  both methods have the same name but different argument types.

For example:

class DisplayOverloading
{
    public void disp(char c)
    {
         System.out.println(c);
    }
    public void disp(char c, int num)  
    {
         System.out.println(c + " "+num);
    }
}
class Sample
{
   public static void main(String args[])
   {
       DisplayOverloading obj = new DisplayOverloading();
       obj.disp('a');
       obj.disp('a',10);
   }
}

The output would be:

a
a 10

Overriding: is when someone declares a method in subclass which is already present in parent class.

For example:

class Human{
   public void eat()
   {
      System.out.println("Human is eating");
   }
}
class Boy extends Human{
   public void eat(){
      System.out.println("Boy is eating");
   }
   public static void main( String args[]) {
      Boy obj = new Boy();
      obj.eat();
   }
}

The output would be:

Boy is eating

What are overloading and overriding? How do we implement those in Java?

I got this information out of:

Method Overloading in Java with examples

Method overriding in java with example

http://javarevisited.blogspot.mx/2011/12/method-overloading-vs-method-overriding.html

What are overloading and overriding? How do we implement those in Java?


What are overloading and overriding? How do we implement those in Java?