Definition of Polymorphism in Java

  • Polymorphism in Java is one of the important OOP features by which we can perform a single action in different ways.
  • Simply, polymorphism is the ability of an object to take many forms in the same situation.
  • In Java, the ability of a reference variable to change its behavior according to what object instance it is holding.
  • Polymorphism is the ability of a message to be displayed in more than one form.
  • It is the ability for the same code to be used with different types of objects and behave differently with each.

Features/Characteristics of Polymorphism in Java

  • The term ‘Polymorphism’ is derived from two Greek words:- poly means many and morphos means form/structure i.e. polymorphism means many forms. Morphos is related to the Greek god Morpheus, who could appear to sleep individuals in any form he wished and hence was truly polymorphic.
  • Polymorphism is one of the core concepts in OOP languages.
  • The main reason to use the concept of polymorphism in Java is to implement inheritance extensively. It plays an important role in allowing objects having different internal structures to share the same external interface.
  • Where inheritance allows sub-classes to share features, polymorphism allows sub-classes to distinguish themselves from one another.
  • We can perform polymorphism in Java mainly by two concepts – method overloading sometimes called ad hoc polymorphism and method overriding.
  • Any Java object that can pass more than one IS-A condition is considered to be polymorphic.
  • Polymorphism is not a programming concept but it is one of the principal of OOPs.
  • We can implement polymorphism in Java using the concept of method overloading(static) and method overriding(dynamic).
  • Some Real-time common Examples are – (i) A person at the same time can have different characteristics i.e. a man at the same time is a father, a husband, an employee, an uncle, etc.(ii) Use of different functions of a smartphone.
  • There are few languages where we have polymorphism without using inheritance. They are JavaScript, Python, Ruby, VB.NET, and Small Talk. In each of these languages, it is possible to write bike.start() without knowing anything about the object bike and its method start().
  • The term polymorphism (or pure polymorphism) is for situations where one function can be used with a variety of arguments, and the term overloading is for situations where there are multiple functions all defined with a single name.
  • Upcasting in Java is referencing a variable of the Parent class to the object of the Child class. In other words, we can say that Upcasting is casting a sub-type to a supertype i.e. upward to the inheritance tree.

Advantage

  • It helps the programmer to reuse the code and classes once written, tested, and implemented. They can be reused in many ways such as a single variable name can be used to store variables of multiple data types(Int, Float, Long, double, etc).
  • Code is simpler to write.
  • Uniform interface for clients.
  • Used extensively in object-oriented works.

Disadvantage

  • Developers find it difficult to implement polymorphism in codes properly.
  • Run time polymorphism degrades the processing performances as decisions are taken at run time as the machine needs to decide which method or variable to invoke.

Use/Applications

  • The most common use of polymorphism in OOPs occurs when a parent class reference is used to refer to a child class object.
  • The use of polymorphism in Object Oriented Programming is the ability of objects of different types to respond to the same method call, implementing a specific type of behavior.
  • Polymorphism simplifies the communication between objects by using a common interface to hide different implementation details.

Types of Polymorphism

  • There are two types of polymorphism in Java:-
    1. Compile-Time/Static polymorphism and
    2. Runtime/Dynamic Method Dispatch Polymorphism.
(i) Compile-Time/Static Polymorphism/Early Binding : –
      • The Binding is connecting a method call to a (the respected)method body.
      • The term early-binding is mainly due to the after the compile process when the code is now in byte-code form, the computer will “know” which type of methods it will execute after matching datatype or no. of parameters as occurs in method overloading.
      • Examples are – Method Overloading. In Method overloading, it is the function name that is polymorphic – it has many forms. Here, there is a single abstract function that takes various types of arguments. The actual code executed depends on the arguments given.
(ii) Runtime/Dynamic Method Dispatch Polymorphism/Late Binding : –
      • The term late-binding is mainly due to the computer does not know at compile time which of the methods are to be executed. It will not know that until “run time.”
      • Run-time polymorphism comes in two different forms: run-time polymorphism with abstract base classes(They areMust be specified as abstract. Classes are concrete by default, Can have both variables and constants, Usually have a mix of concrete and abstract methods, Sub-classes extend abstract base classes, Sub-classes can only extend 1 abstract base class, Methods can be private, public, or protected ) and run-time polymorphism with interfaces( They are – abstract by default, Abstract may be specified but is default abstract regardless, Can only have constants value, no variables, Can only have abstract methods. Interface methods may be specified as abstract but are default abstract regardless, Sub-classes implement an interface (base class), Sub-classes can implement more than one interface, and All methods (actually method headings) are public).
      • It is a process in which a call to an overridden method is resolved at runtime rather than compile-time.
      • In this process, an overridden method is called through the reference variable of a superclass.
      • Examples are – Method Overriding.

Abstract Class

  • An abstract class is a class with an abstract method. We make a class abstract by declaring it with the keyword abstract.
  • An abstract class normally contains one or more abstract methods.
  • Abstract methods do not provide implementations/its code use
  • A class that contains abstract methods must be abstract even if that class contains some concrete (non-abstract) methods.
  • Each concrete subclass of an abstract superclass also must provide concrete implementations of each of the superclass’s abstract methods.
  • Constructors and static methods cannot be declared abstract.
  • They cannot instantiate objects of abstract superclasses, but we can use abstract superclasses to declare variables. These can hold references to objects of any concrete
    class derived from those abstract superclasses. Programs typically use such variables to manipulate subclass objects polymorphically.
  • They Can use abstract superclass names to invoke static methods declared in those abstract superclasses.
  • Classes that can be used to instantiate objects and have methods with code bodies are called Concrete Classes. Such classes provide implementations of every method they declare.
  • Abstract Method –
    • An abstract method is a method without a body of code/blank method, i.e., only declared (signature)but not defined(body of codes).
    • Abstract methods are normally defined in sub-classes of the abstract class.
    • If a class has abstract methods that class must be declared abstract using abstract keyword.
    • Classes without abstract methods can also be declared abstract as per requirements.
    • A subclass of a concrete superclass can be abstract.
    • Abstract methods are overwritten in sub-classes.
    • One advantage of abstract methods is conceptual, in that their use allows the programmer to think of an activity as associated with an abstraction at a higher level than may be the case.
  • It is not possible to make instances/objects of abstract classes.
  • Abstract fields are not possible.
  • Constructors can be defined as abstract classes.
  • A complete abstract class has no abstract methods but instantiation (object creation) does not make sense.
  • An incomplete abstract class has some abstract methods.
  • In an abstract method, however, the behavior in the parent class is essentially null, only as a placeholder, and all useful activity is defined as part of the code provided by the child class.
  • Sometimes it’s useful to declare classes for which we never intend to create objects.
  • Used only as super-classes in inheritance hierarchies to improve code reuse and allow polymorphism. Programmers often write client code that uses only abstract super-class types to reduce client code’s dependencies on a range of subclass types.
  • An abstract class provides a super-class from which other classes can inherit and thus share a common design.
  • Abstract classes are sometimes used to constitute several levels of a hierarchy.

Syntax : 

Syntax : 

Example : public abstract void input();

Loading

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.