Java Interview Questions for Selenium Automation Testing

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:

  1. The method name must be the same.
  2. Parameters must be different as
    a. Data types of parameters
    b. Number of parameters
    c. Sequence of data type of parameters
  3. Access specifiers can be anything or different.
  4. Return type can be anything or different.
  5. Exception thrown can be anything.

22. What are the features of method overloading?

πŸ…° The features of method overloading are as follows:

  1. The call to overloaded method is bonded at compile time.
  2. The concept of method overloading is also known as compile-time polymorphism in java.
  3. 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.
  4. 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.
  5. The private methods can be overloaded in Java.
  6. The final methods can be overloaded in Java.
  7. The main method can also be overloaded in Java.
  8. 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:

  1. Method overloading is used when we need to perform same task with different parameters.
  2. Method overloading is done to reuse the same method name.
  3. 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:

  1. To add a new feature or properties.
  2. To override or change the existing functionality.
  3. 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