Core Java Interview Question's Answers - 2
What is overloading?
- The process of writing multiple methods with the same name in a class is known as Method Overloading.
- Here parameter list must be different in terms of Number or Order or Type of parameter(NOT).
- Method name and parameter list together is known as Method Signature so in short we can say in case of Overloading Method Signature must be different.
- Method Signature does not include Return type of method so Return type can be anything.
- Method Overloading is also known as Early Binding or Compile Time Polymorphism or Static Polymorphism.
- It is known as Early Binding because from the invocation statement which actual version of methods will be invoked, this binding is made earlier the execution of program by the compiler.
- It is known as Compile Time Polymorphism because Compiler is making decision during the Compilation process about which exact methods.
What is overriding?
- If we are not okay with the method implementation in super class then we can re-implement in sub class, this process of modifying the existing functionality is known as Overriding.
Restrictions on the particular method of subclass while Overriding.
- Name should be same.
- Parameter list should be same.
- Return type should be same if primitive or same or sub (co-variant) if reference.
- Static method is Hidden not Overridden but we need to follow the rules of Overriding.
- Instance method should be Overridden as instance method only.
- Access modifier should be same or wider but not narrower.
- Final method cannot be Overridden.
Difference between Overloading and Overriding.
- Def-The process of writing multiple methods with the same name but with different set of parameters in terms of NOT.
- Parameter list must be different
- Return type can be anything.
- Any access modifiers.
- Static method possible.
- Early Binding
- Compile time polymorphism.
- Static Polymorphism
- Usually Within same class
- Def - If we want to modify the functionality of super class methods then we write the same method in subclass with diff implementation.
- Parameter list must be same.
- Return type must be same or co-variant.
- Wider or same in subclass.
- We cannot override static method.
- Late Binding.
- Runtime Polymorphism.
- Dynamic polymorphism.
- In different class.
Difference between Method and Constructor.
- This member of class contains the code describing behavior of object.
- It must have return type.
- It can have any name.
- It can have both types of return statement either empty return statement and return statement with value
- We can override it.
- We need to invoke it explicitly.
- Method recursion is there.
- No such
- Compiler never writes any method on it's own.
- One of the 5 member of class which contains the instance initialization code.
- It can not have return type.
- It shares the name of enclosing class.
- It can have only one type of return statement i.e empty return statement.
- Since it is not inherited so we can't override.
- Jvm invokes it.
- No such
- Constructor chaining is there.
- Compiler writes default if no constructor is written inside a class.
What is Import?
- Import is keyword in java.
- It is used to import classes.
- If we don’t import then we need to refer that class with fully qualified class name that will make the code lengthy and reduce the readability of code.
- There are two type of import statement.
2 -Implicit import or multi import.
- Java.lang package is automatically imported in all java files.
- Import statement should appear after package statement.
What is static import?
- If we want to import only static members of a class but not the whole class then we can use static import.
Syntax to import static members of a class.
- import static dhiraj.chandra.com.Person.age;
- Here also we have two types
1 - Single or explicit.
2 - Multi or Implicit.
- By using this kind of import since we are not importing the whole class so we can’t create object of that class.