神刀安全网

Method overriding interview questions

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
package com.instanceofjava; class sample{   int a; int b;   System.out.println("instance of java"); // compiler throws an error.   }   
package com.instanceofjava; class sample{   static int a=10;      public static void main(String args[]){    System.out.println(a); // works fine,prints a value: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:

saidesh ajay vinod  

3.What are the other names for method overloading?

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Method overriding interview questions

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮