Results 1 to 2 of 2
- 11-24-2009, 03:33 AM #1
same object variable t and same methods is calling
Hi,
What is the difference between method overloading and method overriding please explain with examples.
then what is this overloading or overriding
class Test{
public void print(String val){
System.out.println("the value is "+val);
}
}
class Checks{
public static void main (String[] args) {
Test t=new Test();
t.print("ddd");
t.print("SDFSDFSDF");
t.print("asdasdad");
}
}
the output will be
ddd
SDFSDFSDF
asdasdad
and why these are getting means
same object variable t and same methods is calling so the last asdasdad has to get but the 3 output are getting why
please explain
- 11-24-2009, 04:10 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
overloading is when you have the same method name and different return type, or paramaters
public void print(string s)
public int print(int i)
those are the same method, print(), but it is "overloaded"
overriding:
The output would be:Java Code:class Test { public void print(String s) { System.out.println(s); return; } public int print(int i) { System.out.print("" + i); return 1; } class Test1 extends Text { public void print(String s) { System.out.println("The string is: " + s); } public static void main(String args[]) { Test1 one = new Test1(); one.print("something"); }
The string is: something
In this example, there are two methods that are "void print(String)"
one is in class Test, and the other Test1. the print() in Test1 is overriding the print() in Test.
Similar Threads
-
'Class' Object and calling Static Methods?
By mikeiz404 in forum Advanced JavaReplies: 3Last Post: 01-24-2009, 12:58 PM -
'Class' Object and calling Static Methods?
By mikeiz404 in forum New To JavaReplies: 2Last Post: 01-24-2009, 05:10 AM -
Calling a variable from main to another class
By itsme in forum New To JavaReplies: 1Last Post: 12-18-2007, 03:35 PM -
Calling Methods
By bluegreen7hi in forum New To JavaReplies: 3Last Post: 12-17-2007, 06:22 AM -
need help calling methods
By lowpro in forum New To JavaReplies: 2Last Post: 11-15-2007, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks