Saturday, November 27, 2021

JAVA Tricky

JAVA Tricky Questions

  1. Can java overload main method?
    Yes, but JVM only executes original main method. It never calls overloaded main methods

    overloading method- two or more methods in a class have same method name, but must have different parameters

    e.g.

    public class MyMain {

        public static void main(int args){

            System.out.println("This is a int "+args);
        }

        public static void main(boolean args){
            System.out.println("This is a boolean "+args);     
        }

        public static void main(String args[]){

            System.out.println("This is main method");

            MyMain myMain = new MyMain();

            myMain.main(5);

            myMain.main(true);
        } 
    }


  2. In java, can main method override?
    No. In java static method cannot be overridden.

    method override- same method name & same parameters in parent class & child class.
    It can override from parent to child class.
    private, static, final methods can't overridden, but it could be non static to static, non final to final 

  3. In Java, Can create program without main method?
    Yes, we can create class without main method, but it can't be execute because of main method could not find by JVM. It will be error `No such method, "main"`

    e.g.
    public class MyMain{
           System.out.println("This is a class");
    }


  4. Is java pure object oriented language?
    No. Java is not pure object oriented language because
    - java supports byte, Boolean, integers, double, float, characters primitive data type. These keywords are not objects.
    -java does not supports multiple inheritance.


  5. Why multiple inheritance not supports in JAVA? 
    Multiple inheritance is not supported because it leads to deadly diamond problem. Java supports multiple inheritance through interfaces only. A class can implement any number of interfaces but can extend only one class.
    In multiple inheritance class can inherit the properties from one or more parent class. Same signature available in super class & child class. JVM can't determine which method gets call or which class method gets priority.

    e.g.
            class P1{
               void callMe(){
                 System.out.println("I am P1");
  6.            }
              }

          class P2 {
               void callMe(){
                   System.out.println("I am P2");
                   }
                  }

                      class Child extends P1, P2 {
                            public static void main(String args[]){
                                Child c = new Child();
                                  c.callMe();
                                }
                                }

                                    //compilation error

                                      As depicted from code above, on calling the method callMe() using Child object will cause complications such as whether to call P1’s callMe() or P2’s callMe() method. 

                                          e.g.

                                                       class GP{
                                                            void callMe(){
                                                  System.out.println("I am GP");
                                                       }
                                                               }

                                                                   class P1 extends GP{
                                                                        void callMe(){
                                                              System.out.println("I am P1");
                                                                    }
                                                                           }

                                                                                class P2 extends GP{
                                                                                     void callMe(){
                                                                          System.out.println("I am P2");
                                                                                }
                                                                                        }

                                                                                          class Child extends P1, P2 {
                                                                                       public static void main(String args[]){
                                                                                           Child c = new Child();
                                                                                             c.callMe();
                                                                                             }
                                                                                            }

                                                                                                Grand Parent
                                                                                                    /\
                                                                                                     /  \
                                                                                                      /    \
                                                                                                                       /      \
                                                                                                          Parent1 Parent2
                                                                                                                   \      /
                                                                                                                      \    /
                                                                                                                         \  /
                                                                                                                            \/
                                                                                                                         Child

                                                                                                                        -----------------------------------
                                                                                                                          Solution: Java8 support multiple inheritance by interfaces. A class can implement two or more interfaces contains default methods, the implementing class should explicitly specify which default method is to be used, or it should override the default method.

                                                                                                                              interface PI1 {
                                                                                                                                    default void show(){
                                                                                                                                          System.out.println("Default PI1");
                                                                                                                                        }
                                                                                                                                      }
                                                                                                                                         
                                                                                                                                          interface PI2 {
                                                                                                                                                default void show() { 
                                                                                                                                                      System.out.println("Default PI2");
                                                                                                                                                    }
                                                                                                                                                  }
                                                                                                                                                     
                                                                                                                                                      class Test implements PI1, PI2 {
                                                                                                                                                        //override
                                                                                                                                                               public void show(){ 
                                                                                                                                                                    PI1.super.show(); 
                                                                                                                                                                      PI2.super.show();
                                                                                                                                                                    }
                                                                                                                                                                   
                                                                                                                                                                        public static void main(String args[]){
                                                                                                                                                                              Test d = new Test();
                                                                                                                                                                                d.show();
                                                                                                                                                                              }


                                                                                                                                                                                O/P- Default PI1
                                                                                                                                                                                  Default PI2

                                                                                                                                                                                1. what is meaning of public static void main(string[] args)?
                                                                                                                                                                                  why the declaration is standard for main method

                                                                                                                                                                                  public - Access specifier, main method allows to accessible everywhere.
                                                                                                                                                                                  static- main method to get loaded without getting by any object. It access without creating object.
                                                                                                                                                                                  void- return type. does not return any value.
                                                                                                                                                                                  main- method name
                                                                                                                                                                                  String args- String array. defining array of string passing of command line 


                                                                                                                                                                                2. What is static class in JAVA?
                                                                                                                                                                                  static inner class is a nested class which is a static members of outer class. It can can be accessing without instance of outer class
                                                                                                                                                                                  e.g. 
                                                                                                                                                                                  class Outerclass {
                                                                                                                                                                                       public  static String str = "Hello";
                                                                                                                                                                                       public static class NestedClass {
                                                                                                                                                                                           public void m1(){
                                                                                                                                                                                              System.out.println("Hello NestedClass")
                                                                                                                                                                                         }
                                                                                                                                                                                       }

                                                                                                                                                                                      public class InnerClass {
                                                                                                                                                                                           public void m2(){
                                                                                                                                                                                              System.out.println("Hello InnerClass");
                                                                                                                                                                                          }
                                                                                                                                                                                      }
                                                                                                                                                                                  }
                                                                                                                                                                                  public class Main {
                                                                                                                                                                                      public static void main(String[] args) {
                                                                                                                                                                                           //static method call
                                                                                                                                                                                          Outerclass.NestedClass nestedClass = new                  Outerclass.NestedClass();
                                                                                                                                                                                          nestedClass.m1();

                                                                                                                                                                                          //call non static method
                                                                                                                                                                                          Outerclass.InnerClass innerClass = new Outerclass().new InnerClass();
                                                                                                                                                                                         innerClass.m2();

                                                                                                                                                                                         //call non static method 
                                                                                                                                                                                        Outerclass outerclass = new Outerclass();
                                                                                                                                                                                        InnerClass outerclass1 = outerclass.new InnerClass();
                                                                                                                                                                                        outerclass1.m2();
                                                                                                                                                                                         //static keyword call
                                                                                                                                                                                         System.out.println(Outerclass.str);
                                                                                                                                                                                     }   

                                                                                                                                                                                  }

                                                                                                                                                                                3. What is nested class in JAVA?
                                                                                                                                                                                  Class within calss is called nested class. Nested Class can be static or non static
                                                                                                                                                                                  e.g.
                                                                                                                                                                                  class Outerclass {
                                                                                                                                                                                     public  static String str = "Hello";
                                                                                                                                                                                         public static class NestedClass {
                                                                                                                                                                                          public void m1(){
                                                                                                                                                                                              System.out.println("Hello NestedClass");
                                                                                                                                                                                          }
                                                                                                                                                                                      }

                                                                                                                                                                                      public class InnerClass {
                                                                                                                                                                                          public void m2(){
                                                                                                                                                                                              System.out.println("Hello InnerClass");
                                                                                                                                                                                          }
                                                                                                                                                                                      }
                                                                                                                                                                                  }

                                                                                                                                                                                  public class Main {
                                                                                                                                                                                      public static void main(String[] args) {
                                                                                                                                                                                         
                                                                                                                                                                                          //static method call
                                                                                                                                                                                          Outerclass.NestedClass nestedClass = new       Outerclass.NestedClass();
                                                                                                                                                                                          nestedClass.m1();
                                                                                                                                                                                         
                                                                                                                                                                                          //call non static method 
                                                                                                                                                                                          Outerclass.InnerClass innerClass = new Outerclass().new InnerClass();
                                                                                                                                                                                          innerClass.m2();
                                                                                                                                                                                         
                                                                                                                                                                                          //call non static method 
                                                                                                                                                                                          Outerclass outerclass = new Outerclass();
                                                                                                                                                                                          InnerClass outerclass1 = outerclass.new InnerClass();
                                                                                                                                                                                          outerclass1.m2();
                                                                                                                                                                                         
                                                                                                                                                                                          //static keyword call
                                                                                                                                                                                          System.out.println(Outerclass.str);
                                                                                                                                                                                      }   
                                                                                                                                                                                  }


                                                                                                                                                                                   
                                                                                                                                                                                4. What is nested class in JAVA?
                                                                                                                                                                                  Class within calss is called nested class. Nested Class can be static or non static

                                                                                                                                                                                  1. Main difference is of == & eqaul that is operator and method.
                                                                                                                                                                                  2. == is the reference comparison(address comparison) & equals is the content comparison. == is the check both objects points to same memory location.
                                                                                                                                                                                  3. if class does not override the equals, then by default equals method used the closed  of the parent class. 

                                                                                                                                                                                    e.g.
                                                                                                                                                                                    public class EqualsMethod {
                                                                                                                                                                                        public static void main(String[] args) {
                                                                                                                                                                                            String s1 = "HELLO";
                                                                                                                                                                                            String s2 = "HELLO";
                                                                                                                                                                                            String s3 = new String("HELLO");
                                                                                                                                                                                            System.out.println(s1 == s2); // true
                                                                                                                                                                                            System.out.println(s1 == s3); // false
                                                                                                                                                                                            System.out.println(s1.equals(s2)); // true
                                                                                                                                                                                            System.out.println(s1.equals(s3)); // true
                                                                                                                                                                                        }
                                                                                                                                                                                    }

                                                                                                                                                                                5. What is polymorphism?
                                                                                                                                                                                  Polymorphism is a ability of an object takes from many forms. Polymorphism means many forms. Polymorphism is a ability to display messages from different forms. 
                                                                                                                                                                                  Polymorphism allows to perform single action from different ways. Polymorphism allows to implement one interface or multiple interfaces.
                                                                                                                                                                                  Like. The Person at the same time has different characteristics like husband, father, son, brother.

                                                                                                                                                                                  Two types of polymorphism

                                                                                                                                                                                  1. Compile time polymorphism
                                                                                                                                                                                  2. Run time polymorphism

                                                                                                                                                                                  1. Compile time polymorphism: It is also known as static polymorphism. In this polymorphism having method overloading and operator overloading.
                                                                                                                                                                                  Method overloading : method overloading is the one or more methods having same name with different arguments.
                                                                                                                                                                                  e.g.
                                                                                                                                                                                  class Test {
                                                                                                                                                                                  static int multiply(int a, int b){
                                                                                                                                                                                  return a*b;
                                                                                                                                                                                  }
                                                                                                                                                                                  static int multiply(int a, int b, int c){
                                                                                                                                                                                  return a*b*c;
                                                                                                                                                                                  }
                                                                                                                                                                                  }

                                                                                                                                                                                  class PolymorphismTest{
                                                                                                                                                                                  public static void main(String args[]){
                                                                                                                                                                                  System.out.println("Multiply := "+Test.multiply(8, 5));
                                                                                                                                                                                  System.out.println("Multiply := "+Test.multiply(8, 5, 2)); }
                                                                                                                                                                                  }


                                                                                                                                                                                  2. Run time polymorphism: This is also known as Dynamic Method Dispatch. In this polymorphism, method can overriding. Functions call overridden method resolved run time. 

                                                                                                                                                                                  1. e.g. class Parent {
                                                                                                                                                                                    void print(){
                                                                                                                                                                                    System.out.println("This is parent");
                                                                                                                                                                                    }
                                                                                                                                                                                    }
                                                                                                                                                                                    class SubClass extends Parent{
                                                                                                                                                                                        void print(){
                                                                                                                                                                                    System.out.println("This is SubClass");
                                                                                                                                                                                        }
                                                                                                                                                                                    }

                                                                                                                                                                                    class SubClass1 extends Parent{
                                                                                                                                                                                        void print(){
                                                                                                                                                                                    System.out.println("This is SubClass1");
                                                                                                                                                                                        }
                                                                                                                                                                                    }

                                                                                                                                                                                    class Chid {

                                                                                                                                                                                        public static void main(String args[]){

                                                                                                                                                                                            Parent p;
                                                                                                                                                                                            p = new Parent;
                                                                                                                                                                                            p.print();
                                                                                                                                                                                            p = new SubClass();
                                                                                                                                                                                            p.print();
                                                                                                                                                                                            p = new SubClass1();
                                                                                                                                                                                            p.print();
                                                                                                                                                                                        }

                                                                                                                                                                                    }
















                                                                                                                                                                                No comments:

                                                                                                                                                                                Post a Comment