Hi Friends..
can any one provide the real time example for
methodoverloading and methodoverriding .........
Answer Posted / sivadasan
Hi Here is the Example for Method Overloading:
class A{
public void fun1(int x){
System.out.println("The value of class A is : " + x);
}
public void fun1(int x,int y){
System.out.println("The value of class B is : " + x + "
and " + y);
}
}
public class polyone{
public static void main(String[] args){
A obj=new A();
// Here compiler decides that fun1(int)
is to be called and "int" will be printed.
obj.fun1(2);
// Here compiler decides that fun1(int,int)
is to be called and "int and int" will be printed.
obj.fun1(2,3);
}
}
And Method Overriding:
class A{
public void fun1(int x){
System.out.println("int in Class A is : "+ x);
}
}
class B extends A{
public void fun1(int x){
System.out.println("int in Class B is : "+ x);
}
}
public class polytwo{
public static void main(String[] args){
A obj;
obj= new A(); // line 1
obj.fun1(2); // line 2 (prints "int in Class A is :
2")
obj=new B(); // line 3
obj.fun1(5); // line 4 (prints ""int in Class B is :
5")
I think these are very very basic program example for
Method Overloading and Overriding...
| Is This Answer Correct ? | 3 Yes | 1 No |
Post New Answer View All Answers
What are the types of sockets in java?
Can you access the private method from outside the class?
Can you use this() and super() both in a constructor?
What does 0 mean in boolean?
Why is multithreading important?
What is a Transient Object?
Is there any case when finally will not be executed?
What are constructors in java?
Can we declare a class as abstract without having any abstract method?
What does yield method of the thread class do?
What is a “stateless” protocol ?
What are the major drawbacks of external iteration?
What is java in detail?
What is ++ a in java?
Can we increase array size dynamically in java?