Java polymorphism

Created By: chatGPT

In Java, polymorphism allows methods to do different things based on the object it is acting upon, even if they share the same name. There are two main types of polymorphism in Java: compile-time polymorphism (or static binding) and runtime polymorphism (or dynamic binding).
Compile-time polymorphism is achieved through method overloading. This means that multiple methods can have the same name but with different parameters. Java determines which method to execute based on the parameters passed.
class MathUtil {
    // Method to add two integers
    int add(int a, int b) {
        return a + b;
    }
    
    // Method to add three integers
    int add(int a, int b, int c) {
        return a + b + c;
    }
}
In the above example, the add method is overloaded with different parameter lists. When you call the add method, Java identifies which version to execute.
public class PolymorphismDemo {
    public static void main(String[] args) {
        MathUtil mathUtil = new MathUtil();
        System.out.println(mathUtil.add(5, 10)); // Calls add(int a, int b)
        System.out.println(mathUtil.add(5, 10, 15)); // Calls add(int a, int b, int c)
    }
}
Runtime polymorphism, on the other hand, is achieved through method overriding, where a subclass provides a specific implementation of a method that is already defined in its parent class. This allows a method to be called on an object of a superclass type but executed as the subclass type.
class Animal {
    void sound() {
        System.out.println("Animal makes sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    void sound() {
        System.out.println("Cat meows");
    }
}
In the example above, the Animal class has a method sound, which is overridden by subclasses Dog and Cat. When you call the sound method, the implementation of the subclass is executed based on the object type.
public class RuntimePolymorphismDemo {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();
        
        myDog.sound(); // Outputs: Dog barks
        myCat.sound(); // Outputs: Cat meows
    }
}
In conclusion, Java polymorphism is a powerful feature that enhances code flexibility and reusability, allowing programmers to define methods that can operate in various contexts based on the objects they act upon.
Introduction And SetupVariablesData TypesIntFloatDoubleCharBooleanStringByteShortLongArrayListMethodVoidReturnParametersArgumentsControl FlowIf StatementElse StatementElse If StatementSwitch StatementCaseBreakContinueFor LoopWhile LoopDo While LoopEnhanced For LoopFunctionsStatic MethodInstance MethodConstructorOverloadingOverridingAccess ModifiersPrivateProtectedPackage PrivateOperatorsArithmetic OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsTernary OperatorInstanceof OperatorArrayListSetMapHashmapHashtableQueStackCommentsSingle Line CommentMulti Line CommentSyntaxSemicolonsCurly BracesParenthesesData EncapsulationInheritancePolymorphismAbstract ClassInterfaceException HandlingTry BlockCatch BlockFinally BlockThrowThrowsChecked ExceptionUnchecked ExceptionSyntax ErrorsRuntime ErrorsLogic ErrorsEvent HandlingListenersAction EventsMouse EventsKeyboard EventsWindow EventsFocus EventsKey ListenerMouse ListenerAction ListenerThreadingSynchronizationVolatile KeywordConcurrencyJava Memory Model