Java Interview
1.1 Abstract class ?
- A class which is declared with the abstract keyword
- It can have abstract and non-abstract methods (method with the body).
- Abstraction is a process of hiding the implementation details and showing only functionality to the
user.
- There are two ways to achieve abstraction in java (Abstract class & Interface)
- It cannot be instantiated, but can extend
- It can have constructors and static methods also
- It can have final methods which will force the subclass not to change the body of the method.
- Github
Code
1.2 Features of Abstract class ?
- Template
- Loose Coupling
- Code reusability
- Abstraction
- Dynamic Resolution
1.3 Interface?
- It is used to achieve abstraction.
- By interface, we can support the functionality of multiple inheritance.
- It can be used to achieve loose coupling.
- Github
Code
1.4 Java doesn't support multiple inheritances.Why?
- it can lead to diamond problem.
1.5 Object class methods
- hashCode() - // return memory location of objects
- finalize()
- clone()
- toString() - // return classname@hexadecimal_address
1.6 String immutability
- Every time value changes, new memory loction will allocate and will store it there
- If other variable with same string value created then , it reuse old memory location (memory efficient)
- Thread Safe
- Security
1.7 == and equals
- == - check only object references in memory
- .equals() - does the same as ==, overridden possible . But it string & enum it will do deep comparison
1.8 final and finally
- final
- In primitive can't modify
- In object decl - can't change obj ref.
- In class - can't inherit
- In class method - can't override method in chlid class
- Finally
- try{} catch(){} finally{}