Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overloading doubt

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Hemavathy Viswanathan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
----> 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)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

/****** 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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic