Polymorphism vs Overriding vs Overloading

前端 未结 21 3155
北恋
北恋 2020-11-22 01:10

In terms of Java, when someone asks:

what is polymorphism?

Would overloading or overriding be

21条回答
  •  一整个雨季
    2020-11-22 01:33

    import java.io.IOException;
    
    class Super {
    
        protected Super getClassName(Super s) throws IOException {
            System.out.println(this.getClass().getSimpleName() + " - I'm parent");
            return null;
        }
    
    }
    
    class SubOne extends Super {
    
        @Override
        protected Super getClassName(Super s)  {
            System.out.println(this.getClass().getSimpleName() + " - I'm Perfect Overriding");
            return null;
        }
    
    }
    
    class SubTwo extends Super {
    
        @Override
        protected Super getClassName(Super s) throws NullPointerException {
            System.out.println(this.getClass().getSimpleName() + " - I'm Overriding and Throwing Runtime Exception");
            return null;
        }
    
    }
    
    class SubThree extends Super {
    
        @Override
        protected SubThree getClassName(Super s) {
            System.out.println(this.getClass().getSimpleName()+ " - I'm Overriding and Returning SubClass Type");
            return null;
        }
    
    }
    
    class SubFour extends Super {
    
        @Override
        protected Super getClassName(Super s) throws IOException {
            System.out.println(this.getClass().getSimpleName()+ " - I'm Overriding and Throwing Narrower Exception ");
            return null;
        }
    
    }
    
    class SubFive extends Super {
    
        @Override
        public Super getClassName(Super s) {
            System.out.println(this.getClass().getSimpleName()+ " - I'm Overriding and have broader Access ");
            return null;
        }
    
    }
    
    class SubSix extends Super {
    
        public Super getClassName(Super s, String ol) {
            System.out.println(this.getClass().getSimpleName()+ " - I'm Perfect Overloading ");
            return null;
        }
    
    }
    
    class SubSeven extends Super {
    
        public Super getClassName(SubSeven s) {
            System.out.println(this.getClass().getSimpleName()+ " - I'm Perfect Overloading because Method signature (Argument) changed.");
            return null;
        }
    
    }
    
    public class Test{
    
        public static void main(String[] args) throws Exception {
    
            System.out.println("Overriding\n");
    
            Super s1 = new SubOne(); s1.getClassName(null);
    
            Super s2 = new SubTwo(); s2.getClassName(null);
    
            Super s3 = new SubThree(); s3.getClassName(null);
    
            Super s4 = new SubFour(); s4.getClassName(null);
    
            Super s5 = new SubFive(); s5.getClassName(null);
    
            System.out.println("Overloading\n");
    
            SubSix s6 = new SubSix(); s6.getClassName(null, null);
    
            s6 = new SubSix(); s6.getClassName(null);
    
            SubSeven s7 = new SubSeven(); s7.getClassName(s7);
    
            s7 = new SubSeven(); s7.getClassName(new Super());
    
        }
    }
    

提交回复
热议问题