Hi Friends..


can any one provide the real time example for
methodoverloading and methodoverriding .........

Answers were Sorted based on User's Feedback



Hi Friends.. can any one provide the real time example for methodoverloading and methodoverri..

Answer / 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

Hi Friends.. can any one provide the real time example for methodoverloading and methodoverri..

Answer / aravind

Example Method:

public void method(int a)
{
....;
....;
}

Method Overloading:
class
{
method(140,1,10,250);
}

Method Overriding:
re implement same method in a class with same or additional
attributes.

method()
{
...;
}

Is This Answer Correct ?    0 Yes 8 No

Post New Answer

More Core Java Interview Questions

What is the difference between a choice and a list?

0 Answers  


Difference between comparator and comparable in java?

0 Answers  


Why Static variable required in java?For ex,class A { static int a; int b; } Why static is required?

5 Answers   Ericsson,


What is clipping and repainting and what is the relation between them?

1 Answers  


How to sort an array from smallest to largest java?

0 Answers  






jdbc drivers?

8 Answers   Wipro,


What does opcode mean?

0 Answers  


What is byte [] in java?

0 Answers  


Name the class that used to read objects directly from a stream?

4 Answers   Wipro,


Given a singly linked list, how will you print out its contents in the reverse order? Can you do it with consuming any extra space?

0 Answers   Akamai Technologies,


What is generic type?

0 Answers  


What is user defined exception in Java?

0 Answers   TCS,


Categories