OOP_Java Flashcards

1
Q

What must we know about Java before fully being able to implement OOP in Java?

A

Know Java basics!

This course has intermediate topics!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the styles of programming used in computer science?

A

Object-oriented and functional are the two most popular used today!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are programming paradigms?

A

They are styles of writing code.

OOP, Procedural, Functional

Many languages support multiple paradigms.

Not languages!

Ex. Python, Ruby, Java, Javascript

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which programming languages support multiple programming paradigms?

A

OOP, Procedural, Functional:

Python, Ruby, Javascript, Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is everything based on in object-oriented programming (OOP)?

A

Everything is based on the concept of objects

The objects are units that contain:

data

methods (behaviors) that act on the data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the challenge with OOP?

A

It’s fairly complex.

Many books fail to explain it properly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an object?

A

Units that contain:

data (state)

operations on data (behavior) - methods/functions

Together in a single unit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe functional programming

vs. OOP?

A

Functional:

Data (fields)

Behavior (methods/functions)

*Completely independent of each other

OOP:

Data (fields)

Behavior (methods/functions)

*Grouped together in Units

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which style of programming is the best?

A

It depends on the type of problem you are trying to solve!

Some paradigms make more sense in a given context

No paradigm works best in all situations.

Every problem is different.

It all depends on the problem, context, budget

Ex.

Some make the mistake to try to solve all problems in one style or way of programming (OOP vs. functional).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a problem in the software industry?

A

Engineers are more excited about new languages and features than actually solving problems.

They think they know how to solve problems when actually they don’t!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is problem-solving?

A

The definition of engineering problem-solving.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When is Object-Oriented Programming a better choice?

A

Graphical user interfaces and games!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When is functional programming a best choice?

A

In applications that require a high amount of reliability.

Problems that involved messages that get passed around and transformed along the way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When might our application use different paradigms in a single application?

A

the event-driven paradigm in parts that make sense

functional or object-oriented paradigm parts that make sense.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Is there a one-size-fits-all for software engineering?

A

No!

Every problem is different.

depends on the project, the context, and the budget!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is object-oriented programming?

A

A programing paradigm that groups data and methods that act on the data together in objects.

Object-oriented programming is about objects.

These objects interact with each other to perform various tasks!

Ex.

A car consists of thousands of collaborating objects that are reusable and replaceable!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the benefits of OOP?

A

Ex.

We can break a large application into smaller parts, we can focus on code for that object.

Fix the object or plugin another object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is covered in this course?

A

Classes - building blocks of OOP

What they are, how to build them

Encapsulation

Abstraction

Refactor Mortgage calculator to OOP

Inheritance & Polymorphism

Interfaces - coupling problems

JDK classes + frameworks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the most important section of this course?

A

The exercise of refactoring code into OOP format is a crucial skill all Java developers must have!

hardcore coding techniques NOT found in any other courses.

Taking procedural code and refactoring into OOP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does refactoring procedural code into OOP look like?

A

Procedural:

A bunch of methods calling each other in one main class!

OOP:

Use abstraction

Use encapsulation

Use inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is covered on classes in Java?

A

Encapsulation

Abstraction

Constructors

Getters/Setters

Method Overloading

*Many of these concepts are misunderstood by developers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is a class?

A

A template or blueprint for creating objects.

The fields are variables storing data

The methods act on the data values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is an object?

A

An instance of a class

independent of each other.

Stored in separate memory locations

Can be in its own state

Ex.

Current speed vs. Current Gear

different in each car object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is UML?

A

A simple visual language we use to show classes and their relationships

Unified modeling language

visualize classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is the difference between a class and an object?
A class is a blueprint or template for creating objects Objects are an instance of a class but are independent of each other.
26
What is a **real-world** example of a **class**?
**UML** Textbook class
27
When do we use **Pascal naming convention**?
Classes
28
When do we use **camel case notation**?
Methods in a class
29
When do we use the **.this keyword**?
When we want to **set the value of the object's field** to the **value of the parameter** that is **passed into** the method. Ex. this.text = text; \*\*sets current object text field to the value of the parameter text Our object has a field text Our method passes in a parameter called text we want to set the **text passed into our setText method** **as the value of the text field in our object**
30
How do we implement the **TextBox class** in Java?
31
What are reference types by default?
Null This can cause our program to crash
32
Why **do we instantiate** a new object using the **"var" keyword** in Java?
The **"var" keyword** in **Java** The **Java compiler** will **detect the type** based on what's on the **right side w/ the new operator**! We can reduce the redundent type declaration In **Javascript** var is for **variable**!
33
What is the **most popular exceptions** in java?
**null pointer exception** Happens when we try **calling methods** on an **object** that is **null** (field exist)
34
Are two objects equal?
No. They can be in different states. They can have different field values.
35
How does Java **manage primitive and reference variables in memory**?
**Heap** stores objects + fields **Stack** stores primitives variables that store references to objects (heap addresses)
36
How does **memory de-allocation** work in Java?
When we exit a method all variables stored on the stack **immediately removed** leaving an object on the heap w/ no reference in the stack after some time, objects in heap, not reference are erased Garbage collector = removes unused objects on heap
37
What is encapsulation?
Inside a single "unit" or "object"
38
What is a example of proceduring programming?
Data Functions that operate on the data long parameters for functions a fat main. file technically this example is okay because length of code and simple one method as programs get longer, more complex it becomes difficult to separate the different functions and their values because each function isn't encapsulated in a main class changing an object a change in one function may break the implementation of
39
What's the challenge with procedural programming?
With this style of programming we will end up with a bloated or fat main class You end up with methods that have so many parameters
40
What are "getters/setters" ?
Common methods in Java Used to get or set the value of a field! Implemented in the class.
41
How do we create a new class?
Right Click src folder Pascal Notation
42
What does setting a **variable to a reference type** mean?
Means you are referencing the **same object in memory**
43
What does this **code demonstrate**?
**Object-Oriented** Programming **Textbox class** is instantiated into **TextBox Objects**
44
Is using **classes and objects** writing code using **Object Oriented code** (**OOP)**?
**No.** **Object-oriented programming** is way **more** than using **classes and objects** Going to show the **techniques in this course**
45
What is **spaghetti code**?
Programs with fat main classes soon as you change something, other things break so many functions all over the place to **re-use functions** have to **copy code from here/there** but arguments and **parameters differ** everything is interconnected!
46
What does **encapsulation** mean?
**First principle of OOP:** **bundle data** + **methods that operate on data** = in a **single unit** **(object)**
47
**When do we** use the **static keyword** in **declaring** a **method**?
Only when we want to **call a method** from the **main class**.
48
Why **don't we** need to **add parameters** to **this method**?
We are **storing** these **inside these fields** in this **class already** **A sign of OOP is methods without parameters**
49
How can we **refactor** this **code** to **object-oriented** **design**?
Main classes is **cleaner** **encapsulated data** + **methods that act on** data
50
Where should we put **data validation logic**?
**Encapsulate** the **logic** in our **Class**
51
What are **exceptions**?
**Classes** in **Java Library** That **exception**, if **not handled** will **terminate our program**
52
How can we **encapsulate** the **logic** for **data validation** in our **program**?
53
How can we **prevent** an **object** from going into an i**nvalid state**?
use **setters** to **handle error handling** and **data validation** **encapsulate** the **logic** in **the object**
54
What do we want to do with **our fields** in **java**?
set them to private use getter/setter methods to work with them
55
How can we **modify access** to our **fields**?
Set them to **private fields** in a class create **getter/setter** methods for **accessing** these private fields **IntelliJ** light bulb **automatically** builds **getter/setters**
56
What is **abstraction**?
The **second principle** of **OOP**: **Why?** Reduce complexity **Hide implementation** **details** of a **class** hide **unnecessary details** in our classes Treat it like a **black box**
57
What is a **real-world** example of **abstraction**?
**TV remote**: Remote control w/ **buttons** inside electronics/transistors **(complex)** all we want to do is **change the channel** don't care about the **states**
58
What is **the idea** behind **abstraction**?
**Hide implementation details** **expose** a **number of methods** for **others to use**!
59
What is coupling?
The level of **dependency** **between classes** An **extremely important** concept in OOP Many developers **don't understand** this
60
What do we **want to reduce**?
The level of **coupling between classes** ## Footnote **no such thing as zero coupling** **all classes are working together**
61
What's the **problem with coupling**?
The more our **classes are coupled**, the more costly a change in a single class has on the entire class ecosystem **Reducing coupling**, reducing **impact of changes** What situation do we try to avoid? Having an application with a thousand tightly coupled classes Make one change to a class and cause other dependent classes to stop functioning
62
What is the **benefit of reducing** **coupling**?
**Reducing coupling**, reducing **impact of changes**
63
What's a **best practice** for **reducing coupling**?
Making **methods private.** **Better to hide** or **delete methods** The **more methods a class provide**s, the more **other classes are going to get coupled** to it Each accessible method is a **coupling point**
64
What's the **main idea** behind **abstraction** and **reducing class coupling**?
**Reduce** the number of methods **exposed** outside of a **class** ## Footnote **Reduces coupling** **Reduces complexity**
65
What is an **IntelliJ shortcut** for **creating a new method**?
Write the **method name** and **give it a parameter name** Click **lightbulb** to **generate a method**
66
What is a good **IntelliJ shortcut**?
Option + Enter Opens lightbulb for creating methods and fields
67
What is **Object-Oriented Programming** not?
Creating **classes** with **fields and methods only!** OOP has a **number of techniques** It's **not** about **features only** - fields and methods It's a **way of thinking** **Encapsulation** - grouping fields and methods **Abstraction** - hiding implementation details
68
What do we **want to do** with our **class interfaces**?
Make the **interfaces** as **simple as possible** This is the **principle of abstraction** in **OOP**
69
**How** can we **avoid instantiating** an **object** in an **invalid state**?
**Set** the **initial values** to a **valid state** This is a **constructor function's role**
70
What is the **general convention** for **organizing members** in **a class**?
Public Fields Private Fields Constructors Public Methods Private Methods Getters/Setters
71
What is a **constructor**?
A **special method** that is called when we **instantiate** an **object** **Used to create a new object**
72
What is the **role** of the **constructor**?
**initialize our** **fields** to their **default values** Num = 0; boolean = false reference type = null;
73
If **we don't** create a **custom constructor**, what will **Java** do?
**Java compiler will initialize** a **custom constructo**r for us **default constructor** all **values** set to **default values**!
74
How can we **create** a **custom constructor**?
Under the **field declarations** **declare** a new method using the exact **PascalCase name** of the **class** Ex. call **setBaseSalary** and **pass arguments** this **setBaseSalary** has **data validation logic** Therefore we'll never be able to **set our object field** to **invalid states**!
75
What is this an **example of**?
**Object-oriented** programming **Abstraction** and encapsulation **Class** only has **one accessible** method The **interface** is **very simple** and **clean**
76
What is **method overloading**?
A **different implementation** of a **method** with **different parameters** **implementations of a method** with **different paraments**
77
What will make our **applications** hard to maintain?
Overloading a method too many times So many different implementations
78
When is it best to **overload a method**?
Dealing with completely **different parameters** Ex. One takes **an integer**, one could **take an object** Completely **different parameters**!
79
What is **constructor overloading**?
**Technically**, constructors are methods!
80
What is **one way** to **overload** a **constructor**?
**Pass zero** in for a **parameter** **Duplicate** the **constructor code** change **parameters** for the **function** **initialize** one of the **fields** to **zero value**
81
What is **another method** for **overloading** a **constructor**?
Use **.this operator** to call the **constructor function** Can **call the constructor** **Why?** Reuse **logic in the constructor method**
82
What is an **IntelliJ shortcut** for **looking** at the **parameters** of a **method**?
**Command** **+ p**
83
What is an **IntelliJ** shortcut for **duplicating** code?
**command** + **d**
84
What is the **IntelliJ** shortcut for **jumping** to a **method declaration**?
**Command** + **b**
85
**What** are **instance** **members**?
**Fields or methods** that **belong** to each **instance of a class (object)** ## Footnote **Accessible with the .dot operator**
86
What are **static members**?
**Fields and methods** that **belong** to **a class**
87
How do we **access** **static members**?
**Directly** through **the class** We **don't need** to **instantiate an object** **Ex.** **Employee class** **.dot operator** accessible employee .field object not required
88
**When** do we use **static fields**?
When a value is **independent of objects** Want to **share it across all objects** Ex. numberOfEmployees in Employee class
89
What is important to note about **static methods**?
They can only access other **static methods and fields** in the class cannot access **instance methods** Must **create an object** to access the **instance methods**
90
What is an **example** of **static member**s?
**Many classes** in **Java** have **static libraries** Ex. **Integer.parseInt ( )** this is a static method in the Integer class
91
What **classes** do we need in our **application**?
Think of **responsibilities** or **concerns** Each class has a **single responsibility!** Ex. Restaurant Chef, waiter, waitress All **objects collaborate** to **create a service**
92
How many **responsibilities** should each **class** have?
**One** **Single - responsibility** principle
93
What is the **intelliJ** shortcut for **refactoring**?
**Control + T** Refactor: Move, Rename, etc.
94
What is the **incorrect** way to **refactor code** into **OOP**?
**Cut and paste** into a **new class** Causes methods to break Use **IntelliJ** (**Option + T**)
95
What is the best **method** for **refactoring** this **code**?
IntelliJ (**Option + T**) Creates a **new Class** Moves the **implementation** to **clas****!s**
96
What is **safe refactoring**?
Instead of **manually moving code** around Use **IntelliJ** Refactor w/ **Control + T** **Why?** Faster, produces fewer errors!
97
**How** can we move **two methods** into a **new class**?
IntelliJ **Control + T** ## Footnote **Select both methods!**
98
What is an **IntelliJ** shortcut for **generating code** for **constructor**s?
**Command + n** Generate **getter, setter, constructor**!
99
How can we **refactor this code**?
**Change signature** **parameters** are now **fields encapsulated** in the **class** remove them w/ **IntelliJ**
100
How can we find **breaking changes**?
**Hover cursor** over **class name** **right click**
101
Have often should we use **static methods** and **fields**?
Not that **often**! They're **troublesome**
102
What is an **IntelliJ shortcut** for refactoring a **static method** into an **instance method**?
**Control + T** Convert to **an instance method**
103
What is an **example** of **encapsulation in action**?
Store **parameter values** in **fields of the class** Having some state Having some
104
How do we **initialize** our **private fields**?
Using a **constructor**! Remove **parameters** in signatures by creating **private fields** to **store their values in the class** Set the **parameter values** to the **objects fields**
105
**How** can we **create** a **getter** using **IntelliJ**?
Click the **red light bulb** **Read-only** getter
106
How do we **refactor code** for calculating **monthly interest** and **number of payments** in our **mortgage calculator** class?
**Control + T** **Extract** both expressions into private **getter methods** **Abstract:** **getMonthlyInterest** **getNumberOfPayments**
107
What is the **IntelliJ shortcut** for **moving lines of code** down a **class**?
**Option** + **Shift** + **Down Arrow** (Up Arrow)
108
As a **best practice**, where should our **getters and setters** be **in our class**?
At the **bottom** of the **methods**
109
How can we extract and refactor displaying remaining balance in our MortgageReport class?
**Calculate balance** and **store in an array** in **CalculatorMortgage** class Iterate **over array** using a **foreach loop** to **display**
110
How can we refactor this **inline variable** code?
Control + T Inline variable
111
What is **included** in the **Inheritance section**?
**Important concepts**: **Constructors** - base class vs. child class **Access modifiers** - accessing private base class fields **Overriding methods** - changing child method behavior **Comparing objects** - creating equal methods to compare objects **Polymorphism** -
112
How can we **refactor** our **currency object** in our **MortgageReport** class?
**Refactor** -\> **Extract** -\> **field** **initialize** in a **constructor**
113
What is **inheritance**?
**Re-use code** Define all **behavior** in a **single class** Have other classes **inherit behaviors**
114
What is a **real-world** example of **inheritance**?
**Graphical User Interface**: Textbox, drop-down list, radio buttons **Objects share common behavior!** enable or disable setSize **Re-use code!** Define **common behavior** in a **single class** (UI Control) Have other classes **re-use code!**
115
How can we **implement** a **UIControl superclass**?
Give it some members isEnabled disable / enable
116
**How** can we have a class **inherit all the features** we defined in a **superclass**?
add "**extends** + **SuperClass name**" Now objects can **access methods** from Superclass!
117
What members did our **TextBox class inherit**?
**TextBox Members:** clear ( ), setText ( ) **UIControl Members** (**Inherited)**: disable ( ) , isEnabled ( ) , enable ( )
118
What is the **object class**?
**Additional methods** all objects have **Object class** in Java.Lang package **inheritance**
119
What does **Java Compiler automatically** do for us?
Adds the **extends Object** to our classes All Java Classes **Inherit from the Object class**!
120
What are the **member**s in the **java** base **Object class**?
**getClass** - returns class objects (read meta data about object) **equals** - comparing objects **hashcode** - integer based on location **toString** - returns string of object **notify** - concurrency **notifyall** - concurrency
121
What is the **hash method** in the **Object base class**?
**Returns hash** of the **object reference** in **memory**!
122
What does the base **Object class' equals method** compare?
Compares two objects based on the **hash** code! **Not** the **coordinates** or values! We can **override this method** to **compare values**!
123
What **does** the base **Object class'** **toString method** return?
**Returns** **string representation** of **an object** **Has two parts**: **1. fully qualified name** of class (package) **2. hashcode** represented as hexidecimal There are situations where **we can override**! To **return values** in an **object instead**!
124
How do **constructors** work **given inheritance**?
**First,** the **constructor** of the **base class** is called **Then,** the **constructor** of the **sub-class**
125
What if our **base clas**s has a **parameter** in the **construct**or?
We must **explicitly call the base class** constructor as the **first statement** in our **child class constructor**!
126
If our **base class constructor** has a **parameter**, how do we pass a **value** in our **subclass**?
In our **subclass constructor** Call the "**super**" keyword to call the base constructor pass a **value** to the parameter here
127
Are **private members** **inherited** by **subclasses**?
**No**, they are **not inherited** by subclasses. **Private fields/methods** are **not inherited!** **Not accessible outside the class!** **Private members** are used to **hide implementation details**
128
What is the **protected keyword**?
It **sets fields** to public **inside the package** We can **acces**s the field **only inside** the **packag**e! **Treated** like a **bad practice**/**code smell**! **Avoid it**! Hard to **maintain application**s! Use **public and private**!
129
What is the **default access modifier**?
**Package private** **public** anywhere **in the package** **privat**e outside the **package** classes in other packages **cannot inherit** **Stay away from this!** **Stick with public and private!**
130
What is **method overridding**?
Inherit a **method from base class** want to **change implementation** override a **method in base class** Not method **overloading** (changing signatures)
131
When do we use **method overriding**?
When we **inherit** a **method** from a **base class** but **aren't happy with it** and want to **change its behavior**
132
What is an **annotation**?
A **label we attach** to a **class member** Gives **extra information** to the **Java compiler** **like @override!** Helps compiler **double-check** code! Ensure the **exact same signatures** used!
133
What is an **example** of **method overriding**?
The **string method** from **base class**! Want to override it to **display the values** NOT **the location in memory**.
134
How do we **implement method overriding**?
After the **constructor** **@Override** annotation!
135
What is **upcasting** and **downcasting**?
136
What's an **example** of **upcasting**?
textBox (child) **is a** UI control (base) Therefore, TextBox is **automatically** **upcast** to **UIControl** **I.e.** we have a method that requires a UI control object but instead pass a child of the UIControl class and it upcasts it and acts on the data type!
137
When giving a **derivative of a base class** to a method, how can we **access the child class'** methods?
**Explicitly** **cast it** **Using a Prefix** **Downcasting**
138
**When** do **we use** **downcasting**?
Even though at run time we are passing a child object to its parent's method, at compile time we **don't have access** to the child's methods. Must explicitly cast it down
139
**How** do we **prevent exceptions** when **casting**?
When **casting**: **General** cannot cast to **specific** (**parent**) cannot cast to (**child**) **specific** can cast to **general** **(child)** can cast to **(parent)**
140
What will this **expression produce**?
**False.** **Why?** Points are reference types, **values stored** in variables are the **address in memory (not values)**. We have two different point objects, two different values. The default **equal method** in the base object **compares references** (hashes), **not values**!
141
What is a shortcut for over-riding methods?
Command + n Override methods List of methods!
142
How can we solve the **class cast exception**?
Make sure the **object passed** at runtime is an **instanceOf** our **child class** Ex. If not **instanceOf** return false otherwise, **cast it** and **compare values**
143
How can we manually **override** the **equals method** in the **Object class**?
Check if the **object reference** is **equal** (this) Check if **instanceOf** **cast** and **compare values**!
144
What is the **best practice** for **overriding** the **equals method**?
**Override** the **hash method** **also**! Hash generated **based on values** in object **NOT location** in memory
145
What is an **IntelliJ shortcut** for overriding the **equals and hash** methods?
Command + n (**generate menu**) Select equals ( ) and hash ( )
146
What is **polymorphism**?
The **fourth principle in OOP**: "Many forms" Allows an **object** to take different **forms**
147
Where can we use **polymorphism**?
Rendering **an object** in **different ways** **W/o** polymorphism we **must use** a long **if statement** will have to modify if statement, etc.
148
How does **polymorphism** work?
# Define **render ( )** in parent class **each child class** has its own algorithm (override) for **render ( )** **ie. Override** each **subclasses method** **call render and objects behave differently**
149
What is **polymorphism** in **action**?
Each **object** has it's **own implementation** of the **render ( ) method** When we iterate over the array of **different objects**, the **render method** is taking **different form**s!
150
When do we use **abstract classes**?
We declare a **class** We don't want to **instantiate it** or **create a new object** Example. UIControl is an **abstract concept** provides **common code** for subclasses
151
How can we **declare a class** as **abstract**?
Add the **abstract keyword** When to use? When we want **common code** for **subclasses** But don't want the **parent class instantiated** Example. **UIControl** is **abstract class** **TextBox, CheckBox** inherit members!
152
How can we **implement** an **abstract method**?
use **abstract keyword** remove **curly braces**
153
What do **abstract methods** force **subclasses** to do?
**Implement** the **abstract method**!
154
What is a **final class**?
**Prevents** other classes from **extending**! Cannot **inherit from a final class** Want to prevent other classes from extending We don't use these often! Prevents **inheritance, polymorphism,** etc. Ex. String class in **Java** Immutable, cannot extend this class
155
What is a **final method**?
When we declare a **method as final**, we **cannot override** it Used when **subclasses** accidentally **break assumptions** or change behavior
156
What should we **not create**?
Deep **inheritance hierarchies**! People **new to inheritance**, make **this mistake**!
157
What is the **problem here**?
**Deep inheritance hierarchies** are **tightly coupled** Any changes made to **parent,** have to modify **child classes**
158
How many **inheritance level**s should we have?
**One or two** levels! **No more than three** levels! Avoid **deep inheritance**.
159
What is **multiple inheritances**?
In **C++ and Python** A **class** can have **multiple parents**! This **bring complexities** and **ambiguities**. Which class to **inherit** from.
160
What is the **diamond problem**?
Which method should D inherit? You are **not going to need it**. Java **doesn't support it!**
161
What are **interfaces**?
One of the **most powerful,** least understood **concepts** of **Object-Oriented Programming**.
162
What is covered in **this section** on **interfaces**?
What interfaces are Why we need them How to use them properly Dependency injection
163
What are **one** of the most **misunderstood features** of **Java**?
**Interfaces** **Why?** A lot of people don't understand the purpose, the meaning has changed since Java v8
164
What do we use **interfaces to build**?
Loosely **coupled**, **testable** applications **Why?** Allows us to not directly couple classes
165
What is an **interface really**?
A type **similar to a class** Only includes **method declarations** Includes **no code** only **defines capabilities** Allows decoupling!
166
How do **interfaces** work?
Put an **interface** between **classes to decouple them** **Interface** only includes **method declarations** **Programming against interfaces:** Code our classes to work with interfaces Loosely coupled applications Makes extending applications easy
167
How do we **name interfaces**?
-**able** suffix Ex. Drag **able**
168
What do **interfaces** **define** and what to **classes define**?
**Interfaces** are about **"what"** **Classes** are about **"how"** Classes will **implement the algorithms** or the "**how**"
169
How can we create a new interface?
Right click -\> **interface**
170
What doesn't an interface have?
Unnessary public keyword - all methods must be access outside class no fields, no state, no data!
171
How do we use an interface?
use the **implements** keyword in the **class declaration**
172
As a **best practice**, what should we apply to our **interface methods**?
The **@override** annotation!
173
What is **dependency injection**?
**Principle:** Says **creating an object** and **using an object** are **different concerns** We should **separate these concerns!** Take **responsibility for creating dependency** and **put it into another class** Other class will **inject** a **dependency** via: **Constructo**r **injection** **setter injection** **method injection**
174
What is called **poor man's dependency injection**?
**Creating and injecting** dependencies **by hand** In a **real application** may **have hundreds of objects** **Don't want** to create and **pass them all** to constructors! Use **dependency injection** **frameworks** for this. "Spring" is one approach.
175
What is **dependency injection** using a **constructor**?
It's the responsibility of our **main class** to pass a **concrete object** to the TaxReport class. The **TaxReport** class only implements **an interface**! **Most common** **injection approach**!
176
What is **dependency injection** using **setters**?
Creating a method to **inject dependency** **Why?** Allows us to **change the dependency** through the lifetime of **our application**
177
What is the **benefit** of using **setter injection** for our **dependencies**?
We can **change the dependencies** of a class **throughout the lifetime** of the **application**. But we have to **remember to call it**! Can pass a **new instance** of a class that **implements the interface**!
178
What is **method injection**?
**Pass the dependency into the method** that **uses the dependency!**
179
**Why** should we **be careful** when **designing interfaces**?
**Interfaces** allow us to **decouple from objects**. This allows us to change class methods without breaking dependencies. However, If we **change the methods** required in our interfaces, we need to recompile all the **dependencies**!
180
What is the **interface segregation principle**?
A **very** important **topic in OOP** divide **big fat interfaces** into **smaller lighter interfaces** **Why?** Reduce **impact of changes!**
181
What should we **avoid** in **designing our interfaces**?
**Mixing** **concerns** or **capabilities** **Why?** If a **class implements** the interfaces but **doesn't use a method** and you later change the method, it will still have to be recompiled! **Every time** interfaces change **classes dependent** on these **get affected**!
182
What does the **interface segregation principle** say we **should do**?
**Each interface** should **focus** on a **single capability**! We should **divide the interface** into **smaller ones!** **Each one** focused on a **single capability**.
183
How can we **work** with **multiple interfaces**?
Use **inheritance** **One interface** can inherit from **another interface**! This allows us to **use methods** from **both interfaces** while **avoiding fat large** interfaces with **many coupling points**!
184
What is an **intelliJ shortcut** for **generating** an interface?
**Refactor menu** -\> **extract** -\> **interface**
185
What can an **interface** have in **Java**?
**Multiple parents**!