Overloading and Overriding Related Questions

  1. What is difference between overloading and over ridding?
    Over Loading Over Ridding
    It happens in same class It happens in case of inheritance
    Method name same and argument different Method name and arguments same
    Return type doesn't consider Return type considers
    It is known as compile time and static binding Its known as runtime or dynamic binding
    It can have any access level But here same access level or widder. It can't be narrow scope

    Bellow we have given some examples for better understanding of overloading and overridding
  2. What is out put of this program?
    public class OverLoading {
        public int methodA(){
            return 5;
           
        }
        public void methodA(){
            System.out.println("I am in void return tpye method");
        }
        public static void main(String a[])
        {
            OverLoading loading= new OverLoading();
            int x=loading.methodA();
            System.out.println(x);
            loading.methodA();
        }

    }
    Ans compile time error, because both methodA(), consider as one method. Overloading doesn't consider return type
  3. What is out put of this program?
    public class OverLoading {
        public int methodA(){
            return 5;
        
        }
        public void methodA(String str){
            System.out.println(str);
        }
        public static void main(String a[])
        {
            OverLoading loading= new OverLoading();
            int x=loading.methodA();
            System.out.println(x);
            loading.methodA("hello");
        }

    }
    Ans
     5
    hello
  4. What is out put of this program?

    public class OverLoading {
        public void methodA(int x){
            System.out.println(x);
           
        }
        public void methodA(String str){
            System.out.println(str);
        }
        public static void main(String a[])
        {
            OverLoading loading= new OverLoading();
            loading.methodA(4.0);
            loading.methodA("hello");
        }
    }

    Ans
    compile time error, argument mismatch
  5. What is out put of this program?
    public class OverLoading {
        public void methodA(float x){
            System.out.println(x);
            }
        public static void main(String a[])
        {
            OverLoading loading= new OverLoading();
            loading.methodA(4);
            loading.methodA(2.0f);
        }
    }

    Ans
    4.0
    2.0
  6. What is out put of this program?
    public class OverLoading {
        public void methodA(float x){
            System.out.println(x);
       
        }
            public static void main(String a[])
        {
            OverLoading loading= new OverLoading();
            loading.methodA(4);
            loading.methodA(2.0f);
            loading.methodA(4.8d);
           
        }
    }

    Ans
    compile time error. Because 4.8d can't implecitily type cast to float
  7. What is out put of this program?
    public class OverLoading {
        public void methodA(float x){
            System.out.println(x);
           
        }
        public void methodA(Object x){
            System.out.println("object"+x);
           
        }
            public static void main(String a[])
        {
            OverLoading loading= new OverLoading();
            loading.methodA(4);
            loading.methodA(2.0f);
            loading.methodA(4.8d);
            }
    }
    Ans
    4.0
    2.0
    object4.8(because 4.8d can type cast to object)
  8. What is out put of this program?
    public class OverLoading {
        
        public void methodA(Object x){
            System.out.println("object "+x);
           
        }
        public void methodA(String x){
            System.out.println("string "+x);
           
        }
            public static void main(String a[])
        {
            OverLoading loading= new OverLoading();
           
            loading.methodA("hello");
            loading.methodA(3);
           
        }
    }

    Ans
    string hello
    object 3
  9. What is out put of this program?
    public class OverLoading {
           public void methodA(Object x){
            System.out.println("object "+x);
            }
        public void methodA(int x){
            System.out.println("string "+x);
             }
            public static void main(String a[])
        {
            OverLoading loading= new OverLoading();
                  loading.methodA("hello");
                  loading.methodA(loading);
                  loading.methodA(6.0f);
             }
    }

    Ans
    object hello
    object OverLoading@1bc4459
    object 6.0
  10. What is out put of this program?
    public class A  {
        public void testA(){
            System.out.println("class A");
        }
    }

    public class B extends A{
    public void testA(){
        System.out.println("Class B");
    }
    }

    public class OverRiddingInJava {
    public static void main(String ar[]){
        B b= new B();
        b.testA();
        A a = new B();
        a.testA();
        B b1=new A();
        b1.testA();
    }
    }
    Ans
    Compile time error in B b1=new A();
    sub class reference can not refer super class object
  11. What is out put of this program?
    public class A  {
        public void testA(){
            System.out.println("class A");
        }
    }

    public class B extends A{
    public void testA(){
        System.out.println("Class B");
    }
    }

    public class OverRiddingInJava {
    public static void main(String ar[]){
        B b= new B();
        b.testA();
        A a = new B();
        a.testA();
        }
    }

    Ans
    Class B
    Class B
  12. What is out put of this program?
    public class A  {
        public void testA(){
            System.out.println("class A");
        }
    }

    public class B extends A{
    public void testA(String srr){
        System.out.println(srr);
    }
    }

    public class OverRiddingInJava {
    public static void main(String ar[]){
        B b= new B();
        b.testA();
        A a = new B();
        a.testA();
        }
    }

    Ans
    class A
    class A
  13. What is out put of this program?
    public class A  {
        public void testA(){
            System.out.println("class A");
        }
    }


    public class B extends A{
     void testA(){
        System.out.println("Class B");
    }
    }


    public class OverRiddingInJava {
    public static void main(String ar[]){
        B b= new B();
        b.testA();
        
        }
    }

    Ans
    Compilation error in class B,becaue over ridding method can't be narrow scope that described in super class
  14. What is out put of this program?
    public class A  {
        private void testA(){
            System.out.println("class A");
        }
    }


    public class B extends A{
     public void testA(){
        System.out.println("Class B");
    }
    }


    public class OverRiddingInJava {
    public static void main(String ar[]){
        B b= new B();
        b.testA();
        
        }
    }

    Ans
    Class B
    Because private method can't override. The method testA(), we are declaring in B class, its itself one new method
  15. What is out put of this program?
    public class A  {
        final void testA(){
            System.out.println("class A");
        }
    }


    public class B extends A{
     public void testA(){
        System.out.println("Class B");
    }
    }

    public class OverRiddingInJava {
    public static void main(String ar[]){
        B b= new B();
        b.testA();
        
        }
    }

    Ans
    Final method can't override, so it'll give compilation error in class B
  16. What is out put of this program?
    public class A  {
        public void testA(){
            System.out.println("class A");
        }
    }


    public class B extends A{
     public static void testA(){
        System.out.println("Class B");
    }
    }


    public class OverRiddingInJava {
    public static void main(String ar[]){
        B b= new B();
        b.testA();
        
        }
    }

    Ans
    Static method can't hide, again it'll give compilation error in class B
  17. What is out put of this program?
    public class A  {
        public void testA() throws Exception{
            System.out.println("class A");
        }
    }
    public class B extends A{
     public void testA(){
        System.out.println("Class B");
    }
    }
    public class OverRiddingInJava {
    public static void main(String ar[]){
        B b= new B();
        b.testA();
      A a=new B(); a.testA(); 
        }
    }

    Ans
    Compilation error : Unhandled exception type Exception , in a.testA()
  18. What is out put of this program?
    public class A  {
        public  void testA()throws Exception{
            System.out.println("class A");
        }
    }

    public class B extends A{
    public void testA()throws ClassNotFoundException{
        System.out.println("Class B");
    }
    }

    public class OverRiddingInJava {
    public static void main(String ar[]) throws Exception{
        B b= new B();
        b.testA();
        A a= new B();
        b.testA();
            }
    }

    Ans:
    Class B
    Class B
  19. What is out put of this program?
    public class A  {
        public  void testA()throws IOException{
            System.out.println("class A");
        }
    }

    public class B extends A{
    public void testA()throws ClassNotFoundException{
        System.out.println("Class B");
    }
    }

    public class OverRiddingInJava {
    public static void main(String ar[]) throws Exception{
        B b= new B();
        b.testA();
        A a= new B();
        b.testA();
            }
    }

    Ans: Compilation erro in class B. Because overridding method should throw, same exception or subclass excetion or no exception
  20. What is dynamic method ditchpatch?
    Ans:This is occours in case of over-ridding and super class and subclass having same method. Super class reference refers to sub class object and calls the subclass method.
    Its  runtime binding or late binding.
    Example
    package com;
    public class A {
    public void test(){
        System.out.println(" I am in Class A");
    }
    }
    package com;
    public class B extends A{
    public void test()
    {
        System.out.println("I am in class B");
    }
    }
    package com;
    public class TestDynamicMethodDispatch {
        public static void main(String ar[])
        {
            A a= new B();
            a.test();
        }
    }

    O/P
    I am in class B