Frequently asked Tough and Advanced Java Interview Questions and Answers for 2-6 year experienced Java developers, Business Analyst, Data Analyst, Senior software engineers, Tech Lead and freshers asked in MNC's like TCS, Accenture, IBM, CTS, Infosys, Wipro, Tech Mahindra and Morgan Stanley.
Polymorphism can be explained as an object's ability to adapt to the program's context and take multiple forms. The method overriding is an example of run time polymorphism. You can have a method in a subclass, which overrides the method in its super classes with the same name and signature. At run time, Java virtual machine determines the appropriate method to be called.
During method Overloading, method name should remain same. But method signature can vary. Components of signature that can vary are
- Number of arguments
- Datatype of arguments
- Order of arguments
During method Overriding, make sure that the method is not throwing checked exceptions that are new or higher than those declared by the overridden method.But we can't override Static and Final methods.
|Supports Single inheritance||Supports Multiple inheritance|
|Supports abstract and Non-abstract methods||Allows only abstract methods|
|Supports Non-static and non-final variables also.||Variables must be static and final(implicitly)|
|Supports non public member||Only public members are allowed|
|Using extends keyword||Using implements keyword|
|It can invoke if main exists||Pure abstract|
4) Explain the difference between compile time and run time polymorphism in Java ?
|Compile time Polymorphism||Run time Polymorphism|
|Method are called at compile time||Method are called at run time|
|Ex: Overloading||Ex: Overriding|
5) What is the difference between Overloading and Overriding ?
|Methods are overloaded during compile time||Method overriding takes place during runtime|
|All the overloaded methods should be placed in the same class||We can override methods in sub class|
|We can overload static methods||Static methods can't be overridden|
|Methods are bonded together using static binding.||Overridden method are bonded via dynamic bonding based upon actual Object.|
|To overload a method, method signature needs to be changed||There is no need to change the signature|
|Private and final method can be overloaded.||Private and final method can't be overridden|
|Method is relatively fast.||Method is relatively slow.|
6) What is the difference between class and object ?
|Template/Blue print of an object.||It is an instance of a class. Object have states and behaviors.|
|A logical construct.||A Physical reality.|
7) What are the major object oriented concepts in Java ?
It denotes the critical properties of an object which differentiate from other object and thus provide crisply defined conceptual boundaries relative to the perspective viewer.
Encapsulation can be explained as a mechanism which binds the code and the data it manipulates. It also keeps them safe from external intervention and misuse.
One object inherits the properties and methods of another Object.
It is an ability of an object to take on many forms. Ex: Compile time polymorphism – method over loading. Run time polymorphism – method overriding
8) Why Java is not supporting multiple inheritance ?
Main features of java are very Simple. if multiple inheritance is supported, it creates ambiguity around Diamond problem and it does complicate the design and creates problem during casting, chaining etc. So Java will support multi-inheritance via single inheritance with interfaces to overcome above issues.
9) What is meant by final keyword in Java ?
- If final variable is used in front of variable, we can't change the value.
- If the variable is used in front of method, it can't be overridden.
- If it is used in front of Class, class can't be extended by any other class.
10) What is meant by static keyword in Java ?
A static is a member of a class that's not associated with instance. So static members can be accessed without creating an instance of a class.