Adblocker detected! Please consider whitelist or disable this site.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

To keep the site operating, we need funding, and practically all of it comes from internet advertising.

If you still want to continue, Please add techgeeknext.com to your ad blocking whitelist or disable your adblocking software.

×
Top Java Polymorphism Interview Questions (2022) | TechGeekNxt >>


Top Java Polymorphism Interview Questions (2022)

  1. What is polymorphism in Java?
  2. What are the different types of Polymorphism in Java?
  3. How Polymorphism is implemented in Java?
  4. What is static binding in polymorphism in Java?
  5. What is Run-time or Dynamic-binding in Java Polymorphism?
  6. What are the advantages of Polymorphism in Java?
  7. How does inheritance used in polymorphism in Java?
  8. What is overloading in polymorphism in Java?
  9. What is overriding in polymorphism in Java?
  10. What is difference between overloading and overriding methods?
  11. What is difference between Polymorphism and Inheritance in Java?
  12. What is difference between Static binding and Dynamic binding?
  13. How static binding is achieved in polymorphism in Java?
  14. How Dynamic binding is achieved in polymorphism in Java?
  15. Is it possible to achieve polymorphism using data members in Java?
  16. Why binding of private, static, and final methods are always static binding in Java?

Q: What is polymorphism in Java?
Ans:

Polymorphism is one of the main concepts of OOPs(Object-Oriented programming). The word polymorphism means ploy means many and morph means forms. Polymorphism means the ability to take more than one form by using a single interface. Polymorphism is a process for implementing inherited data.

The simple meaning of Polymorphism is one name and multiple forms. When single or many types are not defined by name it represents any type by using abstract symbols. Different types of tasks are performed by using Polymorphism.

Illustration of Polymorphism:

class Bicycle{
  void run(){System.out.println("running");}
}
class Hero extends Bicycle{
  void run(){System.out.println("running safely with 10km");}

  public static void main(String args[]){
    Biycle b = new hero();//upcasting  
    b.run();
  }
}

Q: What are different types of Polymorphism in Java?
Ans:

The ability to process objects differently depending on their class and data types is known as polymorphism.

Polymorphism in Java is divided into two types:

  1. Compile time polymorphism : This is also referred as Overloading.
  2. Run time polymorphism : This is also referred as Overriding.
Static polymorphisms and dynamic polymorphisms are terms used to describe this type of java polymorphism.

Take a look at our Suggested Posts :

Q: How Polymorphism is implemented in Java?
Ans:

In Java Polymorphism is implemented as one object with the same name and derived results in multiple forms(actions/behavior). Polymorphism is achieved of an object by actions/behavior, the role of an object in the properties does not have any role. The technique of Polymorphism is mainly used for sub-classes and not for the main class.

Polymorphism is synchronized with inheritance. The Polymorphism is implemented to derive the result of subclasses, sub-classes having the actions/behavior that is inherited from other subclass or the main class.

Polymorphism in real-life :

  1. Related to person: The best example of polymorphism is a woman. The woman is one and plays many roles as of mother, daughter, wife, sister-in-law, teacher.
  2. Related to the product: The fan is on in summer which gives cool air and in monsoon and winter we keep the fan off to get warm.
  3. conclusion : Single name and multiple functions(forms).

Q: What is static binding in polymorphism in Java?
Ans:

The other name of Static binding is Early binding. An object is linked with the function during compile-time is called Static binding. In simple words, Static binding in polymorphism means the same method name with a different signature. In the same class we have multiple methods with the same name but with different parameters is called a Static class. The functionality to achieve the Static binding Overloading method is used.

public class StaticBindingTest {
    public static void main(String args[]) {
        Collection c = new HashSet();
        StaticBindingTest et = new StaticBindingTest();
        et.sort(c);
    }
    //overloaded method takes Collection argument
    public Collection sort(Collection c) {
        System.out.println("Inside Collection sort method");
        return c;
    }
    //another overloaded method which takes HashSet argument which is sub class
    public Collection sort(HashSet hs) {
        System.out.println("Inside HashSet sort method");
        return hs;
    }
}

Q: What is Run-time or Dynamic-binding in Java Polymorphism?
Ans:

The other name for Run-time /Dynamic binding is Late binding or overriding. In Dynamic binding/Runtime, there are different classes where inheritance is implemented and methods with the same parameters. In Dynamic polymorphism, the resolve of the file/code is done at a Run-time and not at a compile-time by using the Overriding method. Following the method of overriding is also called Dynamic binding. In Dynamic binding polymorphism, the code is extended and becomes flexible and easy to manage.

public class DynamicBindingTest {
    public static void main(String args[]) {
        Vehicle vehicle = new Car(); //here Type is vehicle but object will be Car
        vehicle.start(); //Car's start called because start() is overridden method
    }
}

class Vehicle {
    public void start() {
        System.out.println("Inside start method of Vehicle");
    }
}

class Car extends Vehicle {
    @Override
    public void start() {
        System.out.println("Inside start method of Car");
    }
}

Q: What are the advantages of Polymorphism in Java?
Ans:

Polymorphism, the name itself says one name and many functions. The flexibility of the code is achieved because of performing many functionalities by using different methods with the same names but different parameters.

The advantage of using Polymorphism is the ability to take more than one form by using a single interface. when single or many types are not defined by name it represents any type by using abstract symbols.

Q: How does inheritance used in polymorphism in java?
Ans:

Inheritance means a process where a sub-class/child class acquires all the properties and behaviors of the parent class/superclass.

Polymorphism takes advantage of the parent-child relationship between two classes and makes the program more Dynamic by adding dynamic features in the code.

Q: What is overloading in polymorphism in java?
Ans:

Overloading is also known by another name Static binding. In the process of overloading:

  1. The method has the same names with different signatures and different types of parameters.
  2. The method signature and number of parameters are the same but the types are different.
  3. Overloading is also implemented when the different signature methods and parameters of numbers or types are used.
  4. The overloading method is implemented at compile-time.

Here is an example which explains how Addition is achieved by implementing the same method and different types :

class Addition{
  static int add(int a,int b){return a+b;}
  static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
  public static void main(String[] args){
    System.out.println(Adder.add(20,20));
    System.out.println(Adder.add(20,20,20));
  }
}


//output is :
 a=40;
 b=60;

Q: What is overriding in polymorphism in java?
Ans:

Overriding in polymorphism in java means, the method is declared in the parent class as well as child class.

An overriding method is used at the Run-time/Dynamic binding. An overriding method is also called Late binding. In simple words, overriding methods are used where the methods in the parent class are pre-define and different types of implementation are done in the child class.

Here is an example which explains how Addition is achieved by implementing the same method and different types :

class Animal {
    public void run() {
          System.out.println("I can Run.");
        }
    }
    class Lion extends Animal {
        public void run() {
            System.out.println("I can run fast over very short distances.");
                super.Run();
            }
            public static void main(String args[]) {
                Lion african = new();
                african.run();
            }
        }


//output is :
I can run fast over very short distances.
I can Run

When using the override method, one should be very much accurate with the implementation or modifying in the sub-class at the same level. For example, In the base class method which is implemented is protected, then in the overriding method, it should be protected or public.

Q: What is difference between overloading and overriding methods?
Ans:

Difference
Overloading Method
Overriding Method

Concept

Overloading is processed:

  • The method has the same names with different signatures and different types of parameters.
  • The method signature and number of parameters are the same but the types are different.
  • Overloading is also implemented when the different signature methods and parameters of numbers or types are used.

Overriding is a process:

  • The method is declared in the parent class as well as the child class.
  • In the case of overriding, the parameters must be the same.
  • Overriding methods are used where the methods in the parent class are pre-define and different types of implementation are done in the child class.

Uses

The overloading Method is used for increasing the readability of the program.

The overriding method is used for the specific implementation.

Applied

  • Overloading Method is applied in Compil time.
  • The overloading Method is also known as Static binding.

  • The overriding method is applied on Run-time.
  • The overriding method is also known as Dynamic binding.

Q: What is difference between Polymorphism and Inheritance in Java?
Ans:

Difference
Polymorphism
Inheritance

Focus

It focuses on achieving the flexibility of code. Polymorphism is achieved by performing many functionalities by using different methods with the same names but different parameters.

It focuses on behavior and attributes of a parent class is inherited by the child class

Basic

The word polymorphism means Poly means many and morph means form.

Polymorphism means the ability to take more than one form by using a single interface. when single or many types are not defined by name it represents any type by using abstract symbols. Polymorphism is a process used for implementing inherited data.

Inheritance is the process of creating a new class by using an existing class in a function. In other words, inheritance is a process where a child class /subclass inherits all the properties and behaviors of the parent class/superclass.

Conclusion

The result is derived in multiple forms with the same name.

The result is derived by calling each class that is inherited

Process

Two methods of Polymorphism

  • Overloading/static binding/compile-time/early-binding.
  • Overriding/Dynamic binding/Run-time/late-binding.

Types of inheritance are FIVE :

  • Single Inheritance.
  • Multilevel Inheritance.
  • Hierarchical Inheritance.
  • Multiple Inheritance (Through Interface)
  • Hybrid Inheritance (Through Interface).

Q: What is difference between Static binding and Dynamic binding?
Ans:

Static binding is one of the methods used or implemented in polymorphisms. Another name for static binding is Early binding. An object is linked with the function during compile-time is called Static binding.

In simple words, Static binding in polymorphism means the same method name with a different signature. In the same class we have multiple methods with the same name but with different parameters is called a Static class. The overloading method is implemented at compile-time to achieve static binding.

Let's see the code:

public class Exam{
  public static int func(int a ){
       // do some processing..
       return 90;
  }
  public static char func(int a , int b){
     // do some processing..
      return "educate";
  }
  public static void main(String args[]){
    System.out.println(func(1));
    System.out.println(func(1,3));
  }
}
=========================================
Output: 90
        educate

Dynamic binding is one of the methods used or implemented in polymorphism. Another name for Dynamic binding is Run-time. In Dynamic binding/Run-time, there are different classes where inheritance is implemented and methods with the same parameters.

In Dynamic polymorphism, the resolve of the file/code is done at a Run-time and not at a compile-time by using the Overriding method. Following the method of overriding is also called Dynamic binding. In Dynamic binding polymorphism, the code is extended and becomes flexible and easy to manage.

Let see the code:

public class DynamicPolymorphismExample {
    public static void main(String args[]) {
        Vegetables vegetables = new Patato();
        vegetable.color();
    }
}

class vegetable {
    public void color() {
        System.out.println("Parent class method is invoked.");
    }
}

class Patato extends Vegetable {
    @Override
    public void color() {
        System.out.println("The child class method is invoked.");
    }
}

===========================================
Output: The child class method is invoked.

Q: How static binding is achieved in polymorphism in java?
Ans:

In polymorphism, static binding is achieved by using the overloading method at a compile-time the same method name with a different signature. In the same class, we have multiple methods with the same name but with different parameters.

Q: How Dynamic binding is achieved in polymorphism in java?
Ans:

In polymorphism, Dynamic binding is achieved by using the overriding method at Run-time the method to be implemented is based on the object being referred to by the reference variable. The code is extended and becomes flexible in dynamic binding. Dynamic binding is used where different classes are inherited with the same parameters.

Q: Is it possible to achieve polymorphism using data members in Java?
Ans:

Data members can't achieve polymorphism, as polymorphism is achieved by actions/behaviors of an object only property and not a role played by an object.

Q:Why binding of private, static, and final methods are always static binding in Java?
Ans:

Static, final, and private methods are early binding because they can't be overridden. The call to the object is already assigned to a defined location at the start of the execution of a program, which is known as early binding.

Late binding, on the other hand, selects the method during runtime. The method that will be called is determined by the class that the reference is pointing to. This is particularly true for interface methods, which cannot be bound early since they do not point to a specific implementation.



Recommendation for Top Popular Post :