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
Q

What is the difference between a class and an object?

A

A class is a blueprint or template for creating objects

Objects are an instance of a class but are independent of each other.

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

What is a real-world example of a class?

A

UML

Textbook class

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

When do we use Pascal naming convention?

A

Classes

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

When do we use camel case notation?

A

Methods in a class

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

When do we use the .this keyword?

A

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

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

How do we implement the TextBox class in Java?

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

What are reference types by default?

A

Null

This can cause our program to crash

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

Why do we instantiate a new object using the “var” keyword in Java?

A

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!

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

What is the most popular exceptions in java?

A

null pointer exception

Happens when

we try calling methods on an object that is null (field exist)

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

Are two objects equal?

A

No.

They can be in different states.

They can have different field values.

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

How does Java manage primitive and reference variables in memory?

A

Heap

stores objects + fields

Stack

stores primitives

variables that store references to objects (heap addresses)

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

How does memory de-allocation work in Java?

A

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

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

What is encapsulation?

A

Inside a single “unit” or “object”

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

What is a example of proceduring programming?

A

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

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

What’s the challenge with procedural programming?

A

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

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

What are “getters/setters” ?

A

Common methods in Java

Used to get or set the value of a field!

Implemented in the class.

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

How do we create a new class?

A

Right Click src folder

Pascal Notation

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

What does setting a variable to a reference type mean?

A

Means you are referencing the same object in memory

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

What does this code demonstrate?

A

Object-Oriented Programming

Textbox class is instantiated into TextBox Objects

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

Is using classes and objects writing code using Object Oriented code (OOP)?

A

No.

Object-oriented programming is way more than using classes and objects

Going to show the techniques in this course

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

What is spaghetti code?

A

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!

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

What does encapsulation mean?

A

First principle of OOP:

bundle data + methods that operate on data

=

in a single unit (object)

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

When do we use the static keyword in declaring a method?

A

Only when we want to call a method from the main class.

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

Why don’t we need to add parameters to this method?

A

We are storing these inside these fields in this class already

A sign of OOP is methods without parameters

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

How can we refactor this code to object-oriented design?

A

Main classes is cleaner

encapsulated data + methods that act on data

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

Where should we put data validation logic?

A

Encapsulate the logic in our Class

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

What are exceptions?

A

Classes in Java Library

That exception, if not handled will terminate our program

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

How can we encapsulate the logic for data validation in our program?

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

How can we prevent an object from going into an invalid state?

A

use setters to handle error handling and data validation

encapsulate the logic in the object

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

What do we want to do with our fields in java?

A

set them to private

use getter/setter methods to work with them

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

How can we modify access to our fields?

A

Set them to private fields in a class

create getter/setter methods for accessing these private fields

IntelliJ light bulb automatically builds getter/setters

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

What is abstraction?

A

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

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

What is a real-world example of abstraction?

A

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

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

What is the idea behind abstraction?

A

Hide implementation details

expose a number of methods for others to use!

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

What is coupling?

A

The level of dependency between classes

An extremely important concept in OOP

Many developers don’t understand this

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

What do we want to reduce?

A

The level of coupling between classes

no such thing as zero coupling

all classes are working together

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

What’s the problem with coupling?

A

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

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

What is the benefit of reducing coupling?

A

Reducing coupling, reducing impact of changes

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

What’s a best practice for reducing coupling?

A

Making methods private.

Better to hide or delete methods

The more methods a class provides, the more other classes are going to get coupled to it

Each accessible method is a coupling point

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

What’s the main idea behind abstraction and reducing class coupling?

A

Reduce the number of methods exposed outside of a class

Reduces coupling

Reduces complexity

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

What is an IntelliJ shortcut for creating a new method?

A

Write the method name and give it a parameter name

Click lightbulb to generate a method

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

What is a good IntelliJ shortcut?

A

Option + Enter

Opens lightbulb for creating methods and fields

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

What is Object-Oriented Programming not?

A

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

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

What do we want to do with our class interfaces?

A

Make the interfaces as simple as possible

This is the principle of abstraction in OOP

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

How can we avoid instantiating an object in an invalid state?

A

Set the initial values to a valid state

This is a constructor function’s role

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

What is the general convention for organizing members in a class?

A

Public Fields

Private Fields

Constructors

Public Methods

Private Methods

Getters/Setters

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

What is a constructor?

A

A special method that is called when we instantiate an object

Used to create a new object

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

What is the role of the constructor?

A

initialize our fields to their default values

Num = 0;

boolean = false

reference type = null;

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

If we don’t create a custom constructor, what will Java do?

A

Java compiler will initialize a custom constructor for us

default constructor

all values set to default values!

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

How can we create a custom constructor?

A

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!

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

What is this an example of?

A

Object-oriented programming

Abstraction and encapsulation

Class only has one accessible method

The interface is very simple and clean

76
Q

What is method overloading?

A

A different implementation of a method with different parameters

implementations of a method with different paraments

77
Q

What will make our applications hard to maintain?

A

Overloading a method too many times

So many different implementations

78
Q

When is it best to overload a method?

A

Dealing with completely different parameters

Ex.

One takes an integer, one could take an object

Completely different parameters!

79
Q

What is constructor overloading?

A

Technically, constructors are methods!

80
Q

What is one way to overload a constructor?

A

Pass zero in for a parameter

Duplicate the constructor code

change parameters for the function

initialize one of the fields to zero value

81
Q

What is another method for overloading a constructor?

A

Use .this operator to call the constructor function

Can call the constructor

Why?

Reuse logic in the constructor method

82
Q

What is an IntelliJ shortcut for looking at the parameters of a method?

A

Command + p

83
Q

What is an IntelliJ shortcut for duplicating code?

A

command + d

84
Q

What is the IntelliJ shortcut for jumping to a method declaration?

A

Command + b

85
Q

What are instance members?

A

Fields or methods that belong to each instance of a class (object)

Accessible with the .dot operator

86
Q

What are static members?

A

Fields and methods that belong to a class

87
Q

How do we access static members?

A

Directly through the class

We don’t need to instantiate an object

Ex.

Employee class .dot operator accessible

employee .field object not required

88
Q

When do we use static fields?

A

When a value is independent of objects

Want to share it across all objects

Ex.

numberOfEmployees in Employee class

89
Q

What is important to note about static methods?

A

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
Q

What is an example of static members?

A

Many classes in Java have static libraries

Ex.

Integer.parseInt ( )

this is a static method in the Integer class

91
Q

What classes do we need in our application?

A

Think of responsibilities or concerns

Each class has a single responsibility!

Ex. Restaurant

Chef, waiter, waitress

All objects collaborate to create a service

92
Q

How many responsibilities should each class have?

A

One

Single - responsibility principle

93
Q

What is the intelliJ shortcut for refactoring?

A

Control + T

Refactor:

Move, Rename, etc.

94
Q

What is the incorrect way to refactor code into OOP?

A

Cut and paste into a new class

Causes methods to break

Use IntelliJ (Option + T)

95
Q

What is the best method for refactoring this code?

A

IntelliJ (Option + T)

Creates a new Class

Moves the implementation to clas!s

96
Q

What is safe refactoring?

A

Instead of manually moving code around

Use IntelliJ

Refactor w/ Control + T

Why?

Faster, produces fewer errors!

97
Q

How can we move two methods into a new class?

A

IntelliJ Control + T

Select both methods!

98
Q

What is an IntelliJ shortcut for generating code for constructors?

A

Command + n

Generate getter, setter, constructor!

99
Q

How can we refactor this code?

A

Change signature

parameters are now fields encapsulated in the class

remove them w/ IntelliJ

100
Q

How can we find breaking changes?

A

Hover cursor over class name

right click

101
Q

Have often should we use static methods and fields?

A

Not that often!

They’re troublesome

102
Q

What is an IntelliJ shortcut for refactoring a static method into an instance method?

A

Control + T

Convert to an instance method

103
Q

What is an example of encapsulation in action?

A

Store parameter values in fields of the class

Having some state

Having some

104
Q

How do we initialize our private fields?

A

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
Q

How can we create a getter using IntelliJ?

A

Click the red light bulb

Read-only getter

106
Q

How do we refactor code for calculating monthly interest and number of payments in our mortgage calculator class?

A

Control + T

Extract both expressions into private getter methods

Abstract:

getMonthlyInterest

getNumberOfPayments

107
Q

What is the IntelliJ shortcut for moving lines of code down a class?

A

Option + Shift + Down Arrow (Up Arrow)

108
Q

As a best practice, where should our getters and setters be in our class?

A

At the bottom of the methods

109
Q

How can we extract and refactor displaying remaining balance in our MortgageReport class?

A

Calculate balance and store in an array in CalculatorMortgage class

Iterate over array using a foreach loop to display

110
Q

How can we refactor this inline variable code?

A

Control + T

Inline variable

111
Q

What is included in the Inheritance section?

A

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
Q

How can we refactor our currency object in our MortgageReport class?

A

Refactor -> Extract -> field

initialize in a constructor

113
Q

What is inheritance?

A

Re-use code

Define all behavior in a single class

Have other classes inherit behaviors

114
Q

What is a real-world example of inheritance?

A

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
Q

How can we implement a UIControl superclass?

A

Give it some members

isEnabled

disable / enable

116
Q

How can we have a class inherit all the features we defined in a superclass?

A

add “extends + SuperClass name

Now objects can access methods from Superclass!

117
Q

What members did our TextBox class inherit?

A

TextBox Members:

clear ( ), setText ( )

UIControl Members (Inherited):

disable ( ) , isEnabled ( ) , enable ( )

118
Q

What is the object class?

A

Additional methods all objects have

Object class in Java.Lang package inheritance

119
Q

What does Java Compiler automatically do for us?

A

Adds the extends Object to our classes

All Java Classes Inherit from the Object class!

120
Q

What are the members in the java base Object class?

A

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
Q

What is the hash method in the Object base class?

A

Returns hash of the object reference in memory!

122
Q

What does the base Object class’ equals method compare?

A

Compares two objects based on the hash code!

Not the coordinates or values!

We can override this method to compare values!

123
Q

What does the base Object class’ toString method return?

A

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
Q

How do constructors work given inheritance?

A

First, the constructor of the base class is called

Then, the constructor of the sub-class

125
Q

What if our base class has a parameter in the constructor?

A

We must explicitly call the base class constructor

as the first statement in our child class constructor!

126
Q

If our base class constructor has a parameter, how do we pass a value in our subclass?

A

In our subclass constructor

Call the “super” keyword to call the base constructor

pass a value to the parameter here

127
Q

Are private members inherited by subclasses?

A

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
Q

What is the protected keyword?

A

It sets fields to public inside the package

We can access the field only inside the package!

Treated like a bad practice/code smell!

Avoid it!

Hard to maintain applications!

Use public and private!

129
Q

What is the default access modifier?

A

Package private

public anywhere in the package

private outside the package

classes in other packages cannot inherit

Stay away from this!

Stick with public and private!

130
Q

What is method overridding?

A

Inherit a method from base class

want to change implementation

override a method in base class

Not method overloading (changing signatures)

131
Q

When do we use method overriding?

A

When we inherit a method from a base class

but aren’t happy with it and want to change its behavior

132
Q

What is an annotation?

A

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
Q

What is an example of method overriding?

A

The string method from base class!

Want to override it to display the values NOT the location in memory.

134
Q

How do we implement method overriding?

A

After the constructor

@Override annotation!

135
Q

What is upcasting and downcasting?

A
136
Q

What’s an example of upcasting?

A

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
Q

When giving a derivative of a base class to a method, how can we access the child class’ methods?

A

Explicitly cast it

Using a Prefix

Downcasting

138
Q

When do we use downcasting?

A

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
Q

How do we prevent exceptions when casting?

A

When casting:

General cannot cast to specific

(parent) cannot cast to (child)

specific can cast to general

(child) can cast to (parent)

140
Q

What will this expression produce?

A

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
Q

What is a shortcut for over-riding methods?

A

Command + n

Override methods

List of methods!

142
Q

How can we solve the class cast exception?

A

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
Q

How can we manually override the equals method in the Object class?

A

Check if the object reference is equal (this)

Check if instanceOf

cast and compare values!

144
Q

What is the best practice for overriding the equals method?

A

Override the hash method also!

Hash generated based on values in object NOT location in memory

145
Q

What is an IntelliJ shortcut for overriding the equals and hash methods?

A

Command + n

(generate menu)

Select equals ( ) and hash ( )

146
Q

What is polymorphism?

A

The fourth principle in OOP:

“Many forms”

Allows an object to take different forms

147
Q

Where can we use polymorphism?

A

Rendering an object in different ways

W/o polymorphism we must use a long if statement

will have to modify if statement, etc.

148
Q

How does polymorphism work?

A

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
Q

What is polymorphism in action?

A

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 forms!

150
Q

When do we use abstract classes?

A

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
Q

How can we declare a class as abstract?

A

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
Q

How can we implement an abstract method?

A

use abstract keyword

remove curly braces

153
Q

What do abstract methods force subclasses to do?

A

Implement the abstract method!

154
Q

What is a final class?

A

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
Q

What is a final method?

A

When we declare a method as final, we cannot override it

Used when subclasses accidentally break assumptions or change behavior

156
Q

What should we not create?

A

Deep inheritance hierarchies!

People new to inheritance, make this mistake!

157
Q

What is the problem here?

A

Deep inheritance hierarchies are tightly coupled

Any changes made to parent, have to modify child classes

158
Q

How many inheritance levels should we have?

A

One or two levels!

No more than three levels!

Avoid deep inheritance.

159
Q

What is multiple inheritances?

A

In C++ and Python

A class can have multiple parents!

This bring complexities and ambiguities.

Which class to inherit from.

160
Q

What is the diamond problem?

A

Which method should D inherit?

You are not going to need it.

Java doesn’t support it!

161
Q

What are interfaces?

A

One of the most powerful, least understood concepts of Object-Oriented Programming.

162
Q

What is covered in this section on interfaces?

A

What interfaces are

Why we need them

How to use them properly

Dependency injection

163
Q

What are one of the most misunderstood features of Java?

A

Interfaces

Why?

A lot of people don’t understand the purpose, the meaning has changed since Java v8

164
Q

What do we use interfaces to build?

A

Loosely coupled, testable applications

Why?

Allows us to not directly couple classes

165
Q

What is an interface really?

A

A type similar to a class

Only includes method declarations

Includes no code

only defines capabilities

Allows decoupling!

166
Q

How do interfaces work?

A

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
Q

How do we name interfaces?

A

-able suffix

Ex.

Drag able

168
Q

What do interfaces define and what to classes define?

A

Interfaces are about “what”

Classes are about “how”

Classes will implement the algorithms or the “how

169
Q

How can we create a new interface?

A

Right click -> interface

170
Q

What doesn’t an interface have?

A

Unnessary public keyword - all methods must be access outside class

no fields, no state, no data!

171
Q

How do we use an interface?

A

use the implements keyword in the class declaration

172
Q

As a best practice, what should we apply to our interface methods?

A

The @override annotation!

173
Q

What is dependency injection?

A

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:

Constructor injection

setter injection

method injection

174
Q

What is called poor man’s dependency injection?

A

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
Q

What is dependency injection using a constructor?

A

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
Q

What is dependency injection using setters?

A

Creating a method to inject dependency

Why?

Allows us to change the dependency through the lifetime of our application

177
Q

What is the benefit of using setter injection for our dependencies?

A

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
Q

What is method injection?

A

Pass the dependency into the method that uses the dependency!

179
Q

Why should we be careful when designing interfaces?

A

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
Q

What is the interface segregation principle?

A

A very important topic in OOP

divide big fat interfaces into smaller lighter interfaces

Why?

Reduce impact of changes!

181
Q

What should we avoid in designing our interfaces?

A

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
Q

What does the interface segregation principle say we should do?

A

Each interface should focus on a single capability!

We should divide the interface into smaller ones!

Each one focused on a single capability.

183
Q

How can we work with multiple interfaces?

A

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
Q

What is an intelliJ shortcut for generating an interface?

A

Refactor menu -> extract -> interface

185
Q

What can an interface have in Java?

A

Multiple parents!