The Artima Developer Community
Sponsored Link

Java Buzz Forum
Method overloading interview questions java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Method overloading interview questions java Posted: Mar 16, 2016 6:20 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Method overloading interview questions java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.What is Method in java?

  • Method is a sub block of a class that contains logic of that class.
  • logic must be placed inside a method, not directly at class level, if we place logic at class level compiler throws an error.
  • So class level we are allowed to place variables and methods.
  • The logical statements such as method calls, calculations and printing related statements must be placed inside method, because these statements are considered as logic. 
  • For more information @ Methods in java



  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. int a;
  5. int b;
  6.  
  7. System.out.println("instance of java"); // compiler throws an error.
  8.  
  9. }
  10.  

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. static int a=10;
  5.    
  6. public static void main(String args[]){

  7. System.out.println(a); // works fine,prints a value:10
  8.  
  9.  }
  10. }

2.What is meant by method overloading in java?


  • Defining multiple methods with same name is known as polymorphism.
  • Defining multiple methods with same name and with different arguments known as method overloading.

  1. package com.instanceofjava;
  2. class A{
  3.  
  4. public void show(int a){
  5. System.out.println("saidesh");
  6. }
  7.  
  8. public void show(int a,int b){
  9. System.out.println("ajay");
  10. }
  11.  
  12. public void show(float a){
  13. System.out.println("vinod");
  14. }
  15. public static void main(String args[]){
  16.  
  17. A a=new A();
  18.  
  19. a.show(10);
  20. a.show(1,2);
  21. a.show(1.2);
  22.  
  23. }
  24. }
Output:

  1. saidesh
  2. ajay
  3. vinod


3.What are the other names for method overloading?


  • Method overloading also known as static polymorphism or compile time polymorphism because at compile time itself we can tell which method going to get executed based on method arguments.
  • So method overloading also called as static binding. 

4.What are the basic rules of method overloading?

  • Defining a method with same name and differ in number of arguments
  • Defining a method with same name and differ in type of arguments
  • Defining a method with same name and differ in order of type of arguments
  • Return type of the method not involved in method overloading.

5.Can we overload static methods in java?

  • Yes. We can overload static methods in java.
  • Method overriding is not possible but method overloading is possible for static methods.
  • Before that lets see about method overloading in java.
  • lets see an example java program which explains static method overloading.
  1. class StaticMethodOverloading{
  2.  
  3. public static void staticMethod(){
  4.  
  5. System.out.println("staticMethod(): Zero arguments");
  6.  
  7.  
  8. public static void staticMethod(int a){
  9.  
  10. System.out.println("staticMethod(int a): one argument");
  11.  
  12.  
  13. public static void staticMethod(String str, int x){
  14.  
  15. System.out.println("staticMethod(String str, int x): two arguments");
  16.  
  17. }
  18.  
  19. public static void main(String []args){
  20.   
  21.   StaticMethodOverloading.staticMethod();
  22.   StaticMethodOverloading.staticMethod(12);
  23.   StaticMethodOverloading.staticMethod("Static method overloading",10);
  24.  
  25. }
  26. }


 Output:

  1. staticMethod(): Zero arguments
  2. staticMethod(int a): one argument
  3. staticMethod(String str, int x): two arguments


6.Can we overload main method in java?

Yes we can overload main method in java

Java Program to overload main method in java

  1. class mainMethodOverloading{
  2.  
  3. public static void main(boolean x){
  4.  
  5. System.out.println("main(boolean x) called ");
  6.  
  7.  
  8. public static void main(int x){
  9.  
  10. System.out.println("main(int x) called");
  11.  
  12.  
  13. public static void main(int a, int b){
  14.  
  15. System.out.println("main(int a, int b) called");
  16.  
  17. }
  18.  
  19. public static void main(String []args){
  20.    
  21.  
  22. System.out.println("main(String []args) called ");
  23.  
  24.   mainMethodOverloading.main(true);
  25.   mainMethodOverloading.main(10);
  26.  mainMethodOverloading.main(37,46);
  27.  
  28.  
  29. }
  30. }


 Output:

  1. main(String []args) called
  2. main(boolean x) called
  3. main(int x) called
  4. main(int a, int b) called

7.Can we overload constructors in java?

  • Yes we can overload constructor in java

  1. package instanceofjava;
  2. class ConstructorChaining{
  3. int a,b 
  4. ConstructorChaining(){
  5. this(1,2);
  6.  System.out.println("Default constructor");
  7.  
  8.  
  9. ConstructorChaining(int x , int y){
  10.  
  11. this(1,2,3); 
  12. a=x;
  13. b=y;
  14.  System.out.println("Two argument constructor");
  15.  
  16. }
  17.  
  18. ConstructorChaining(int a , int b,int c){
  19.  System.out.println("Three argument constructor")
  20.  
  21. public static void main(String[] args){
  22.  
  23.  ConstructorChaining obj=new ConstructorChaining();
  24.   System.out.println(obj.a);
  25.   System.out.println(obj.b);
  26.  
  27. }
  28. }



Read: Method overloading interview questions java

Topic: PyPy 5: A faster, leaner Python compiler Previous Topic   Next Topic Topic: Remove all elements LinkedHashSet example

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use