Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Overriding & Overloading

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
we can overload methods in the same class.
can we override metods in same class? Please explain with a example.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
overload:

There are no keywords to remember in order to overload methods, you just create multiple methods with the same name but different numbers and or types of parameters.

Overriding:

Overriding is something done in a child class to a method defined in a parent class. To override a method a new method is defined in the child class with exactly the same signature as the one in the parent class. This has the effect of shadowing the method in the parent class and the functionality is no longer directly accessible.can't be ovveride methods in the same class.
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In order to override a method, that method should be inherited. Otherwise, there is no overriding. Remember that overriding is reimplementing a method that you have inherited..
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes.thank you
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
howdy "m prabhu",

There are NOT many rules being followed in ranch except one strict rule on the user's display name.

Kindly have a look at the naming policy of JavaRanch. You can update your display name by editing your profile.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
howdy "d sitaramayya",

You are also suggested to update your display name several times here and here again.

Kindly do cooperate
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means, overloading is done within the same class.
But for overriding you need to inherited the class.
So, i can declare within one(same) class having two methods with same name but different parameters -- for overloading.

For overriding there should be a superclass and sub class.

Please let me know if i am wrong.

Thanks
Dinesh
 
Jart Bo
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Dinesh Tahiliani,

All you've said are correct! Just to add, overloading can also be done in the subclass. You'll just have to use a different argument list for the method you want to overload.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply.
One more question for getting better idea.
Can we use overriding method within same class(one class) or it should be declare in another class.

for eg:

class A
{

public void disp(String nm)
{
System.out.print(:you in disp mtd);
}

public void disp(String nm)
(
System.out.print(:you in disp overrided mtd);
}

public static void main(String a[])
{
A a1 = new A();
a1.disp("Dinesh");
a1.disp("Java");
}
}

In the below code we have disp which is overrided in same class.

My question is can we override the mtd in same class.

Please help me out.

Thanks
Dinesh

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jart,
One more question?

Is overloading related to polymorphism or overriding? Iam very much confused between these two.

Please expalin with help of eg.

Thanks
Dinesh
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to prabhu
This is an example of Overloading & Overriding

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic