Type Here to Get Search Results !

Top 1000 Java Interview Questions & Answers - Section VI - Polymorphism

0

 


We are sharing the top 1000 Java interview questions , these questions are frequently asked by the recruiters. Java interview questions can be asked from any core java topic . So here we tried our best to provide you the java interview questions and answers for experienced which should be in your to do list before facing java questions in  technical interview. 

Java is one of the most popular programming language. There is a growing demand for Java Developer jobs in technology companies.This article contains technical interview questions that an interviewer asks for Java technology and related topics like Spring, Hibernate, Maven, Git, Microservices, AWS etc.

Each question is accompanied with an answer so that you can prepare for job interview in short time.We have compiled this list after attending dozens of technical interviews in top-notch companies like- Facebook, Amazon,Oracle, Netflix, Microsoft etc.

Once you go through them in the first pass, mark the questions that you could not answer by yourself. Then, in second pass go through only the difficult questions.

After going through this book 2-3 times, you will be well prepared to face a technical interview for a Java Developer position from Software Engineer level to Principal Engineer level.

All the best!

Section VI - Polymorphism 


52.What is polymorphism and what are the types of it?

In java, Polymorphism means "one name many forms". In other words, you can say whenever an object producing different-different behavior in different-different circumstances is called polymorphism in java.

There are two types of polymorphism in java and these are given below.

i.  Compile-Time polymorphism or Static polymorphism.

ii. Run-time polymorphism or Dynamic polymorphism.


53. What is compile time polymorphism?

Compile time polymorphism is the method overloading in java. In simple terms we can say that a class can have more than one methods with same name but with different number of arguments or different types of arguments or both.

Whenever an object is bound with their functionality at compile time is known as compile-time or static polymorphism in java.


54. What is Runtime Polymorphism?

Runtime Polymorphism or Dynamic Polymorphism is the polymorphism that exists at runtime. In case of method overriding it is not known which method will be called at runtime. Based on the type of object, JVM decides the exact method that should be called. In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.

 So at compile time it is not known which method will be called at run time.


55. Is it possible to achieve Runtime Polymorphism by data members in Java?

No. We need to create Runtime Polymorphism by implementing methods at two levels of inheritance in Java.

 

56. Explain the difference between static and dynamic binding?

In Static binding references are resolved at compile time. In Dynamic binding references are resolved at Run time.

Static binding is also known as compile - time polymorphism. Method overloading is the example of dynamic bindingIn static binding, The type of object is determined at compile-time.Static binding is also known as early binding. 

Dynamic binding is also known as run-time polymorphism. Method overriding is the example of dynamic binding.In dynamic binding, The type of object is determined at run-time.Dynamic binding is also known as late binding.


E.g.

Person p = new Person();

p.walk(); // Java compiler resolves this binding at compile time.

public void walk(Object o){

((Person) o).walk(); // this is dynamic binding.

}


57.What is method overriding?

Specific implementation of a method for child class.Whenever we have more than one same name method but a different number of arguments or different data types in the same class is known as method overloading in java. Method overloading is the example of compile-time polymorphism.


For example:

class Parent

{

void show()

{

System.out.println("Parent");

}

}

class Child extends Parent

{

void show()

{

System.out.println("Child");

}

public static void main(String args[])

{

Parent p =new Child();

p.show();

}

}


Output: 
Child


58. What is method overloading?

If a class have multiple methods by same name but different parameters, it is known as Method Overloading. In java, whenever we have same name method in parent and child class with the same number of arguments and same data types is known as method overriding in java. Method overriding is the example of dynamic polymorphism.


For example:

class Addition

{

void add(int a, int b)//with 2 arguments

{

System.out.println(a+b);

}

void add(int a, int b, int c)//change no of arguments i.e 3

{

System.out.println(a+b+c);

}

public static void main(String args[])

{

Addition a = new Addition();

a.add(10,20);

a.add(20,5,6);

}

}


Output:
30
31

59. Difference between method overloading and overriding?


Method overloading Method overriding
1 In case of method overloading, signature of method changes While in case of method overriding it remain same.
2 Can overload method in one class Can only be done on subclass.
3 Can overload static, final or private method in Java Can not override static, final and private method in Java
4 Overloaded method in Java is bonded by static binding Overridden methods are subject to dynamic binding.


60.What is static and dynamic binding?

static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at run time.


61. Can we overload main() method?

Yes,  we can have many main() methods in a class by overloading the main method.

Example:

public class OverLoadMain

{

	public static void main(String[] args)

	{

		System.out.println("main(String[] args)");

	}

 

	public static void main(String args1)

	{

		System.out.println("main(String arg1)");

	}

	public static void main(String arg1, String arg2)

	{

		System.out.println("main(String arg1, String arg2)");

	}

}

Recommended Posts

Top 1000 Java Interview Questions & Answers - Section V - Method Overloading and Overriding

Top 1000 Java Interview Questions & Answers - Section I - Java Basics

Top 1000 Java Interview Questions & Answers - Section II - OOPs ( Object Oriented Programming )

Top 1000 Java Interview Questions & Answers - Section III - Inheritance

Top 1000 Java Interview Questions & Answers - Section IV - Static

Core Java Important Questions Part 1

Core Java Important Questions Part 2

Core Java Important Questions Part 3

Core Java Important Questions Part 4

Core Java Important Questions Part 5

Core Java Important Questions Part 6

Core Java Important Questions Part 7

Core Java Important Questions Part 8

Core Java Important Questions Part 9

Java Collections Interview Questions Part 1

Java Collections Interview Questions Part 2

Java Collections Interview Questions Part 3

Java MCQs with Answers Set I

Java MCQs with Answers Set II

Java MCQs with Answers Set III

If you have any doubts join the discussion below ,our Moderator will reply to your Queries

Post a Comment

0 Comments

Top Post Ad

Below Post Ad