File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes overloading doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overloading doubt" Watch "overloading doubt" New topic
Author

overloading doubt

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
class A {
void foo(int a) {
System.out.println("hi bbb");
}

}

class Baba extends A {
void foo() {
System.out.println("ANimal eat");
}
}
class Vb {
public static void main(String args[]) {
Baba b=new Baba();
b.foo();/******** DOuBT****/
b.foo(8);/******** DOUBT**********/
}
}
/****** it is said that method overloading happens in the same class******/
then what is happening in these 2 DOUBT lines??
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Class A has an instance method foo(int a).

Class Baba extends A, so Baba has the method foo(int a).

Class Baba also declares an overloaded version, foo().


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Hemavathy Viswanathan
Greenhorn

Joined: Dec 26, 2006
Posts: 17
----> This is the parent class with the foo method which takes an argument
---> This is a child class of A, so all the methods and member variables of class A are available to class Baba. So foo(int) method is available here and foo() is the overloaded method in class Baba



Simply, overloading is method with the same name (but different args, no. of args)
overriding is the method with the exact same signature(same arg, same no. of args)


Hemavathy Viswanathan<br />SCJP 1.4/ SCWCD 1.4/ SCBCD 1.3
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Hemavathy Viswanathan:
... Simply, overloading is method with the same name (but different args, no. of args)
overriding is the method with the exact same signature(same arg, same no. of args)

Also note that when we're talking about argument lists, we're considering the argument types (not the names we give them) and the order in which they appear.

So an argument list of (String s, int i) is the same as (String myString, int myInt) because the names don't matter. But (String s, int i) is different than (int i, String s) because the order does matter.
Panseer Kaur
Ranch Hand

Joined: Nov 01, 2007
Posts: 44

/****** it is said that method overloading happens in the same class******/
then what is happening in these 2 DOUBT lines??[/QB]


I think you might be getting confused because it said that "Overloading happens in the same class". It sounds like you are saying "These two methods are not in the same class", which is true. But since subclasses take on the methods of the parent the child also has this method.

So therefore this IS overloading.

Hope that helps :-)
npk chowdary
Greenhorn

Joined: Dec 09, 2007
Posts: 21
overloading can be done in same or in its hierarchy of subclasses.but overrriding can done in its hierarchy of subclasses only but not in same class.overloaded methods can be called depends on reference type declared.
but overrided methods can be called depends on object created
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overloading doubt
 
Similar Threads
Please explain this
doubt in inner class method invocation
doubt
Doubt Example
overloading minute doubt?