DAY 13 Polymorphism:

What is Polymorphism?

In simple terms, polymorphism means "many forms," enabling a single method to work in different ways based on the object that it

is acting upon.

Types of Polymorphism

1.Compile-Time Polymorphism (Method Overloading):

  • Resolved during compile time.
  • Achieved through method overloading.

2. Run-Time Polymorphism (Method Overriding):

  • Resolved during runtime.
  • Achieved through method overriding.

Now what is Method overloading and Method overriding:

1. Compile-Time\Early Binding\Static Polymorphism (Method Overloading)

In method overloading, as name indicates in happens during compile time multiple methods have the same name but differ in:

  • The number of parameters.
  • The type of parameters.
  • The order of parameters.

Example:

add method has different signatures- This is method overloading


2. Run-Time Polymorphism (Method Overriding)

In method overriding, a subclass provides a specific implementation for a method that is already defined in its parent class.

  • It Involves inheritance.
  • The overridden method in the child class must have the same name, return type, and parameters as the method in the parent class.
  • Uses the @Override annotation for clarity and to avoid errors.


makeSound method of animal class is overriden by it's subclasses Dog and cat class makeSound method



Let's see few common differences between Method Overloading and Overriding with respect to Arg type, name, Return type, Access modifier, Throwing exceptions, Binding:

Let's see Encapsulation tomorrow. Happy Learning :)


要查看或添加评论,请登录

Steffi Graph Preethi S的更多文章

  • Arrays

    Arrays

    Been so long since I posted, Happy that i'm able to be back with basic concepts again. Let's see what is Arrays: As we…

  • Day 19: StringBuffer and StringBuilder in Java:

    Day 19: StringBuffer and StringBuilder in Java:

    Mutable String: A String that can be modified or changed is known as mutable String. and are classes in Java used for…

  • Day 18: Strings:

    Day 18: Strings:

    String is a class in the java.lang package and is one of the most commonly used classes.

  • Day 17:

    Day 17:

    Static Keyword: Static is a keyword in Java. The keyword is used to indicate that a particular member (method…

  • Day 16: Jump Statements

    Day 16: Jump Statements

    Jump Statements These statements alter the normal flow of control by transferring execution. 1.

  • Day 16:

    Day 16:

    Looping Statements Looping statement are the execution of the block of code repeatedly until we break it. for Loop…

  • Day 15:

    Day 15:

    Java Control Statements: Java compiler executes the code from Top to Bottom. However Java provides the statement to…

  • Day 14: Encapsulation

    Day 14: Encapsulation

    Day 14: Encapsulation: Encapsulation in Java is integrating the data (fields) and the methods that operate on the data…

  • Abstract methods and abstract classes

    Abstract methods and abstract classes

    Day 12: What is Abstraction? Hiding the Implementation details. Interface allows 100% Abstraction, whereas Abstraction…

  • DAY11 Multiple Inheritance and Interfaces:

    DAY11 Multiple Inheritance and Interfaces:

    4. Multiple Inheritance Java does not support multiple inheritance with classes to avoid ambiguity caused by the…

社区洞察