Type Here to Get Search Results !

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

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 V - Method Overloading and Overriding


40. What is the other name of Method Overloading?

Method Overloading is also known as Static Polymorphism.


41. How will you implement method overloading in Java?

In Java, a class can have multiple methods with same name but different arguments. It is called Method Overloading. To implement method overloading we have to create two methods with same name in a class and do one/more of the following:

1. Different number of parameters

2. Different data type of parameters

3. Different sequence of data type of parameters

 

42. What kinds of argument variations are allowed in Method Overloading?

Method Overloading allows two methods with same name to differ in:

1. Number of parameters

2. Data type of parameters

3. Sequence of data type of parameters

 

43. Why it is not possible to do method overloading by changing return type of method in java?

If we change the return type of overloaded methods then it will lead to ambiguous behavior. How will clients know which method will return what type. Due to this different return type are not allowed in overloaded methods.


44. Is it allowed to overload main() method in Java?

Yes, Java allows users to create many methods with same name ‘main’. But only public static void main(String[] args) method is used for execution.


45. How do we implement method overriding in Java?

To override a method, we just provide a new implementation of a method with same name in subclass. So there will be at least two implementations of the method with same name.  One implementation is in parent class. And another implementation is in child class.


46. Are we allowed to override a static method in Java?

No. Java does not allow overriding a static method. If you create a static method with same name in subclass, then it is a new method, not an overridden method.


47. Why Java does not allow overriding a static method?

To override a method, you need an instance of a class. Static method is not associated with any instance of the class. So the concept of overriding does not apply here.

Therefore, Java does not allow overriding a static method.


48. Is it allowed to override an overloaded method?

Yes. You can override an overloaded method in Java.

 

49. What is the difference between method overloading and method overriding in Java?

Differences between method overloading and overriding are:

1. Method overloading is static polymorphism. Method overriding is runtime polymorphism.

2. Method overloading occurs within the same class. Method overriding happens in two classes with hierarchy relationship.

3. Parameters must  be different in method overloading. Parameters must be same in method overriding.

4. Method overloading is a compile time concept. Method overriding is a runtime concept.


50. Does Java allow virtual functions?

Yes. All instance methods in Java are virtual functions by default. Only class methods and private instance methods are not virtual methods in Java.


51. What is meant by covariant return type in Java?

A covariant return type of a method is one that can be replaced by a "narrower" type when the method is overridden in a subclass.

Let say class B is child of class A. There is a get() method in class A as well as class B. get() method of class A can return an instance of A, and get() method of class B return an instance of B.  Here class B overrides get() method, but the return type is different.

Before Java 5, any method that overrides the method of parent class would have same return type.

From Java 5 onwards, a child class can override a method of parent class and the child class method can return an object that is child of object return by parent class method.


Recommended Posts

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