Java Concepts Flashcards
To be strong on concepts and Basics
1
Q
Inheritance vs Composition
A
- Composition provides code resusability without extending class, but inheritance code reusability is achieved only through extending classes
- Composition you can reuse code for even final class which is not extensible but Inheritance cannot reuse code in such cases
- Static vs Dynamic - With inheritance you have to decide in code itself which class to extend but with composition you just need to define the type and at runtime any implementation can be used
- Limited code reuse with Inheritance - Inheritance we can extend only one class, but you can have HAS-A relationship with many classes
- Unit Testing - U can mock if you use composition, but when it comes to Inheritance, you need to ahve parent class
- Final Classes - No inheritance only composition
- Encapsulation - Proper coding is required in child class otherwise any changes in the super class will break child calss
2
Q
List to Array - Array to List conversion
A
List to Array -> list_reference.toArray(array_reference)
Array to List -> Arrays.asList();
for List to Array, list_reference.toArray() will result in object array to be created
The array and list object becomes joined at the heap, so update in one object updates another. So we cannot add any new element (as array is fixed size), but we can set the values
3
Q
SOLID
A
S -> Single Responsibility Principle O -> Open/Close Principle L -> Liskov Substitution Principle I -> Interface Seggregation D -> Dependency Inversion