Welcome Learners!. In this post Interview Questions on Java Part 2, you will be having few questions and answers related to java. This is the extension of my previous post Interview Questions on Java. Please do refer it before you continue with this post. These questions will definitely help you in your interviews. Please do read carefully and prepare well.
Interview Questions on Java Part 2
Questions 11-15
Q11. Can You overload a main method?
Ans: Yes. You can have any number of main methods but with different method signature and implementation in
the class.
Q12. Does the order of public and static declaration matter in main method?
Ans: No it doesn’t matter but void should always come before main().
Q13. What are constructors?
Ans: Constructors initialize an object. Whenever you create an object JVM automatically calls the constructor of the class.
Q14. Explain Polymorphism?
Ans: The dictionary definition of polymorphism refers to a principle in biology in which an organism or
species can have many different forms or stages. You can also use this principle to object-oriented
programming and languages like the Java language. Subclasses of a class can define their own unique
behaviors and yet share some of the same functionality of the parent class.
Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon.
Overloading and overriding are two types of polymorphism.
Q15. What is Overloading?
Abs: In a class, if two methods have the same name and a different argument, it is known as overloading in Object oriented concepts.
Questions 16-20
Q16. Explain Overriding?
Ans: When a class defines a method using the same name, return type, and arguments as a method in its
superclass, the method in the class overrides the method in the superclass. When the method is invoked
for an object of the class, it is the new definition of the method that is called, and not the method
definition from superclass.
Q17. What is the access scope of a protected method?
Ans: A protected method can be accessed by the classes within the same package or by the subclasses of
the class in any package.
Q18. What is the impact of marking a constructor as private?
Ans: Nobody can instantiate the class from outside that class.
Q19. What is meant by default access?
Ans: Default(Friendly) access means the class, method, constructor or the variable can be accessed only
within the package.
Q20. What is the impact of declaring a method as final?
Ans: A method declared as final can’t be overridden. A sub-class doesn’t have the independence to provide
different implementation to the method. A method can be overloaded.