Java Concepts Flashcards Preview

Java8 > Java Concepts > Flashcards

Flashcards in Java Concepts Deck (3)
Loading flashcards...
1

Inheritance vs Composition

1. Composition provides code resusability without extending class, but inheritance code reusability is achieved only through extending classes
2. Composition you can reuse code for even final class which is not extensible but Inheritance cannot reuse code in such cases
3. 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
4. Limited code reuse with Inheritance - Inheritance we can extend only one class, but you can have HAS-A relationship with many classes
5. Unit Testing - U can mock if you use composition, but when it comes to Inheritance, you need to ahve parent class
6. Final Classes - No inheritance only composition
7. Encapsulation - Proper coding is required in child class otherwise any changes in the super class will break child calss

2

List to Array - Array to List conversion

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

SOLID

S -> Single Responsibility Principle
O -> Open/Close Principle
L -> Liskov Substitution Principle
I -> Interface Seggregation
D -> Dependency Inversion