Part 3
Java interview questions and answers on oops covered in this post:
Will the code compile If the Child class method throwing a broader
checked exception than the parent class method?
- No. The class won’t be compiled if your child class
overriding method throws a broader exception.
package simplifiedjava.crackedInterview; import java.io.IOException; public class Parent {
public void
check(String relation) throws
IOException{
System.out.println("Parent Class check() method.");
} } package simplifiedjava.crackedInterview;
import
java.net.SocketException; import java.sql.SQLException; public class Child extends Parent{
public void
check(String relation)throws
SQLException{
System.out.println("Child Class check() method.");
}
}
Output: Compile Time Error: Exception SQLException is not compatible with throws clause in
Parent.check(String) |
Will the code compile If the parent class method is not throwing any
exception but the child class method is throwing an unchecked exception
or Runtime Exception?
- Yes. Code will compile if parent class method is not throwing
any exception but child class method throws runtime exception or unchecked
exception.
package simplifiedjava.crackedInterview; public class OverrideOverloadedMethodDemo {
public static void
main(String[] args) {
Parent p = new
Parent();
Child c = new
Child();
c.check("Brother");
} } package simplifiedjava.crackedInterview; import java.io.IOException; public class Parent {
public void
check(String relation){
System.out.println("Parent Class check() method.");
} } package simplifiedjava.crackedInterview; public class Child extends Parent{
public void
check(String relation)throws
NullPointerException{
System.out.println("Child Class check() method.");
} }
Output : Child Class check() method. |
Can I declare the final method inside an interface?
- We cannot declare the final method inside an interface.
- Methods declared inside the interface are by default public
abstract.
- For your reference, I have given the below example.
package simplifiedjava.crackedInterview; public interface MyInterface {
public final void
add(int num1, int num2);
}
Error:
Illegal modifier for the interface method add() only public,
abstract, default, static(For Variable only, not a method) and
strictfp are permitted. |
Can I declare a static method inside an interface?
- Interface cannot have any static method but can have static
variable.
Why interface cannot declare a static method?
- Static method cannot override and a method declared inside
the interface must override in implemented class.
What is the role of finalize() method?
- The role of finalize() method is to perform clean up
activity.
- Garbage collector calls finalize() method just before
destroying an object. finalize() method releases the resources acquired by
the objects which don’t have any references.
- At last, all objects which don’t have references will be
deleted from memory.
Java.lang.Object class have equals() method then why there is a
need to override equals() method in user-defined class?
- Object class equals() method compares references not the
actual content of an object.
- In short, if two references pointing to the same object then
only it will return true otherwise it will return false irrespective of
content. That’s why we have to override the equals() method in the
user-defined class.
- Once we override the equals() method in the user-defined
class then we can implement a logic to compare the actual content of the
object.
Can you override the private method?
- No. The private method cannot override because the private
method is not visible in the child class. If the method is not accessible or
visible in child class how can we override the method?
Can you overload the private method?
- Yes. We can overload the private method. There is no need to
have a parent-child relationship to overload the method.
- While method overloading we need to differentiate the type of
parameter or number of the parameter so we can overload private methods.
Can you overload the static method?
- Yes. We can overload the static method.
Can you override the static method?
- No. We cannot override the static method.
- Static method belongs to class not instance.
- Only instance method can be overridden cause each object has
its member variables and methods.
- If you are trying to override the static method that will be considered as
method shadowing.
- Java interview questions and answers all MNC - Click here
- Basic core java interview questions and answers for freshers - Click here
- Core java interview questions for 3 years experience - Click here
- Core java interview questions and answers for 3-5 years exp - Click here
- Core java interview questions and Answers for 5 - 7 Years exp - Click here
- Basic Java Interview Questions and Answers - Click here
- Java interview questions and answers on oops - Click here
- Java interview questions and answers on Strings - Click here
- Java interview questions on exception handling - Click here
- Interview questions on multithreading in java for experienced - Click here
- Interview questions on serialization in java for experienced - Click here
- Interview questions on inner class in java for experienced - Click here
- Interview questions on Collections in java for experienced - Click here
Upcoming questions in next post:
31. What is constructors. What are the types of constructor.
32. Will the parent class constructor invoked while you have invoked child class constructor without super keyword.
33. What is the difference between method and constructor.
34. Can we have constructor in abstract class. If we can't instantiate abstract class then why there is a need of constructor in abstract class.
35. Can I declare constructor as a abstract.
36. Can you declare constructor inside interface.
37. Can you declare constructor static, final, abstract and synchronized. No
38. Why java doesn't allow static constructor.
39. In your program you have static block, non-static block, constructor and main(). What will be the sequence of execution.
40.What is the purpose to make a constructor private.
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked on some support
projects and then I moved to java projects. I had worked with many
multi-national companies like R-Systems, Tata Consultancy Services,
Cybage Softwares. Fortunately, TCS and Cybage has given
me an opportunity to take interviews for experienced candidates. I
have conducted more than 1000 interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation.
You can reach out to me at mncjavainterview@gmail.com.
|
0 Comments