21. How method overloading is implemented in Java?
🅰 Here is the list of rules by which method overloading can be implemented in Java. They are:
- The method name must be the same.
- Parameters must be different as
a. Data types of parameters
b. Number of parameters
c. Sequence of data type of parameters
- Access specifiers can be anything or different.
- Return type can be anything or different.
- Exception thrown can be anything.
22. What are the features of method overloading?
🅰 The features of method overloading are as follows:
- The call to overloaded method is bonded at compile time.
- The concept of method overloading is also known as compile-time polymorphism in java.
- Method overloading is generally done in the same class. But it can also be done in the subclass. We will have to make a relationship between the parent class and child class by using extends keyword for it.
- Method overloading in Java cannot be done by changing the return type of the method because there may occur ambiguity. But the overloaded methods can change the return type.
- The private methods can be overloaded in Java.
- The final methods can be overloaded in Java.
- The main method can also be overloaded in Java.
- Both static and instance methods can be overloaded in Java.
23. When to use method overloading in Java?
🅰 Method overloading is used for the following purpose. They are:
- Method overloading is used when we need to perform same task with different parameters.
- Method overloading is done to reuse the same method name.
- It is used to achieve the compile-time polymorphism in Java.
24. Why method overloading is not possible by changing return type of method?
🅰 In Java, Method overloading cannot be done when the return type, method name, and argument list are the same because there may occur ambiguity.
25. Can we overload main() method in Java?
🅰 Yes, we can overload the main() method in Java. A class can have any number of main() methods but the execution always starts from public static void main(String args) only.
26. What is type conversion in Java?
🅰 The process of converting a value from one data type to another is known as type conversion.
27. What is implicit conversion or implicit casting?
🅰 When two data types are compatible with each other, Java compiler performs the conversion automatically or implicitly. This is called implicit casting.
28. What is widening?
🅰 The process of converting lower data type into higher data type is called widening in java.
29. What is explicit type casting or narrowing conversion?
🅰 The conversion of a higher data type into a lower data type is called narrowing conversion.
30. What is class casting in Java?
🅰 The process of converting a class type into another class type having relationship between them through inheritance is called class casting.
31. What is upcasting and downcasting in Java?
🅰 When the reference variable of super class refers to the object of sub class, it is known as upcasting.
When subclass reference refers to super class object, it is called downcasting.
32. What is method overriding in Java?
🅰 When the method of superclass is overridden in the subclass to provide more specific implementation, it is called method overriding.
33. Why we need method overriding in Java program?
🅰 We need method overriding due to mainly three reasons. They are:
- To add a new feature or properties.
- To override or change the existing functionality.
- To inherit the existing functionality.
34. What are the features of method overriding?
🅰 The features of method overriding are as follows:
- Method overriding technique supports the runtime polymorphism.
- It allows a subclass to provide its own implementation of the method which is already provided by the superclass.
- Only the instance method can be overridden in Java.
- An instance variable can never be overridden in Java.
- The overriding method can not be more restrictive than the overridden method of the superclass.
- Overriding concept is not applicable for private, final, static, and main method in Java.
- Method overriding can be done by changing the covariant return type only.
- Overriding method cannot throw any checked exception
35. How do we implement method overriding in Java?
🅰 To implement method overriding in a program, we need to follow the following rules. They are:
- Subclass method name must be the same as superclass method name.
- The parameters of subclass method must be the same as superclass method parameters. i.e. the method signature must be the same or matched.
- Must be Is-A relationship (Inheritance).
- Subclass method’s access modifier must be the same or higher than superclass method access modifier.
36. Why can’t private method be overridden?
🅰 We cannot override private method because when superclass method is private that will not be visible to subclass, whatever methods we writing in the subclass will be treated as a new method but not an overridden method.
37. Can we override static method in Java?
🅰 No, we cannot override a static method. We cannot also override a static method with an instance method because static method is bound with class whereas an instance method is bound with an object.
38. Can we stop method overriding in Java?
🅰 Yes, we can stop method overriding by declaring method as final.
39. What is the use of method overriding?
🅰 Method overriding is used to achieve runtime polymorphism. It is used to change the existing functionality of the superclass method.
40. What are the differences between method overloading and method overriding?
🅰 Go to this tutorial: Difference between overloading and overriding