Skip to main content

Method Overloading and Overriding Interview Questions


Method Overloading and Overriding



1. What is the another name of Method Overloading?

Method Overloading is also known as Static Polymorphism.

2.In java, How will we implement method overloading?

We known, a class can have multiple method 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

3. What kinds of argument variation are allowed in Method Overloading?

Method Overloading only 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

4. It is not possible to do method overloading by changing return type of the method in java. Why ?

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

5. Is it possible to overload main() method in Java?

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

6. How do we implement method overriding in Java?

To override a method, we just provide a new implementation of a method with same name method 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.

7. Is it possible to override a static method in Java?

No. Java does not allow to 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.

8. Why does not allow overriding a static method in java?

To override a method, we need an instance of a class. Static method is not associate with any instance of the class. So the concept of overriding does not apply on static methods. So, Java does not allow overriding a static method.

9. Is it allowed to override an overloaded method in java?

Yes. we can override an overloaded method in Java.

10. What is the difference between overriding and overloading methods in Java?

Differences between method overriding and overloading are:
1.     Method overloading is static polymorphism. Method overriding is run-time 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 run-time concept.

11. Does Java allow virtual functions?

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

12.What is meant by co-variant return type in Java?

A co-variant 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 X is child of class Z. There is a get() non-static method in class Z as well as class X. get() method of class Z can return an instance of Z, and get() method of class X return an instance of X. Here class X 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.



Comments

Popular posts from this blog

DOCKER Interview questions

DOCKER 1. What is Docker? Docker is Open Source software. It provides the automation of Linux application deployment in a software container. We can do operating system level virtualization on Linux with Docker. Docker can package software in a complete file system that contains software code, runtime environment, system tools, & libraries that are required to install and run the software on a server. 2. What is the difference between Docker image and Docker container? Docker container is simply an instance of Docker image. A Docker image is an immutable file, which is a snapshot of container. We create an image with build command. When we use run command, an Image will produce a container. In programming language, an Image is a Class and a Container is an instance of the class. 3. How will you remove an image from Docker? We can use docker rmi command to delete an image from our local system. Exact command is: % docker rmi <Image Id> If we want to fin...

Amazon Web Services(AWS) Interview questions

Amazon Web Services 1. What do you know about AWS Region? An AWS Region is a completely independent entity in a geographical area. There are two more Availability Zones in an AWS Region. Within a region, Availability Zones are connected through lowlatency links. Since each AWS Region is isolated from another Region, it provides very high fault tolerance and stability. For launching an EC2 instance, we have to select an AMI within the same region. 2. What are the important components of IAM? The important components of IAM are as follows: 1.     IAM User : An IAM User is a person or service that will interact with AWS. User can sign into AWS Management Console for performing tasks in AWS. 2.     IAM Group : An IAM Group is a collection of IAM users. We can specify permission to an IAM Group. This helps in managing large number of IAM users. We can simply add or remove an IAM User to an IAM Group to manage the permissions. 3. ...

Top 80+ Android Interview Questions and Answers

Top 80+ Android Interview Questions Answers Basic Android interview questions for experienced and freshers developers and testers: In this technological era, there is a huge demand for Android Application developers and it is one of the highly paid jobs too. Hence many tech-savvy people are trying to make their way into this career. There are several tricky interview questions that are asked in many MNC’s and some small scale companies. And this article includes a set of such Android interview questions and answers that will be helpful to millions of job seekers. Let’s Explore!! Android Interview Questions This article includes a set of most commonly asked android interview questions and answers covering almost all the important concepts in simple terms in order to enable any job seeker to clear the interview successfully. Most Popular Android Interview Questions & Answers Given below is the list of most commonly asked Android Interview questions 01. What is android? Android is a ...