OOP_Java Flashcards
What must we know about Java before fully being able to implement OOP in Java?
Know Java basics!
This course has intermediate topics!
What are the styles of programming used in computer science?
Object-oriented and functional are the two most popular used today!
What are programming paradigms?
They are styles of writing code.
OOP, Procedural, Functional
Many languages support multiple paradigms.
Not languages!
Ex. Python, Ruby, Java, Javascript
Which programming languages support multiple programming paradigms?
OOP, Procedural, Functional:
Python, Ruby, Javascript, Java
What is everything based on in object-oriented programming (OOP)?
Everything is based on the concept of objects
The objects are units that contain:
data
methods (behaviors) that act on the data
What is the challenge with OOP?
It’s fairly complex.
Many books fail to explain it properly.
What is an object?
Units that contain:
data (state)
operations on data (behavior) - methods/functions
Together in a single unit
Describe functional programming
vs. OOP?
Functional:
Data (fields)
Behavior (methods/functions)
*Completely independent of each other
OOP:
Data (fields)
Behavior (methods/functions)
*Grouped together in Units
Which style of programming is the best?
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).
What is a problem in the software industry?
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!
What is problem-solving?
The definition of engineering problem-solving.
When is Object-Oriented Programming a better choice?
Graphical user interfaces and games!
When is functional programming a best choice?
In applications that require a high amount of reliability.
Problems that involved messages that get passed around and transformed along the way.
When might our application use different paradigms in a single application?
the event-driven paradigm in parts that make sense
functional or object-oriented paradigm parts that make sense.
Is there a one-size-fits-all for software engineering?
No!
Every problem is different.
depends on the project, the context, and the budget!
What is object-oriented programming?
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!
What are the benefits of OOP?
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.
What is covered in this course?
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
What is the most important section of this course?
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
What does refactoring procedural code into OOP look like?
Procedural:
A bunch of methods calling each other in one main class!
OOP:
Use abstraction
Use encapsulation
Use inheritance
What is covered on classes in Java?
Encapsulation
Abstraction
Constructors
Getters/Setters
Method Overloading
*Many of these concepts are misunderstood by developers
What is a class?
A template or blueprint for creating objects.
The fields are variables storing data
The methods act on the data values
What is an object?
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
What is UML?
A simple visual language we use to show classes and their relationships
Unified modeling language
visualize classes
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.
What is a real-world example of a class?
UML
Textbook class
When do we use Pascal naming convention?
Classes
When do we use camel case notation?
Methods in a class
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
How do we implement the TextBox class in Java?
What are reference types by default?
Null
This can cause our program to crash
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!
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)
Are two objects equal?
No.
They can be in different states.
They can have different field values.
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)
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
What is encapsulation?
Inside a single “unit” or “object”
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
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
What are “getters/setters” ?
Common methods in Java
Used to get or set the value of a field!
Implemented in the class.
How do we create a new class?
Right Click src folder
Pascal Notation
What does setting a variable to a reference type mean?
Means you are referencing the same object in memory
What does this code demonstrate?
Object-Oriented Programming
Textbox class is instantiated into TextBox Objects
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
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!
What does encapsulation mean?
First principle of OOP:
bundle data + methods that operate on data
=
in a single unit (object)
When do we use the static keyword in declaring a method?
Only when we want to call a method from the main class.
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
How can we refactor this code to object-oriented design?
Main classes is cleaner
encapsulated data + methods that act on data
Where should we put data validation logic?
Encapsulate the logic in our Class
What are exceptions?
Classes in Java Library
That exception, if not handled will terminate our program
How can we encapsulate the logic for data validation in our program?
How can we prevent an object from going into an invalid state?
use setters to handle error handling and data validation
encapsulate the logic in the object
What do we want to do with our fields in java?
set them to private
use getter/setter methods to work with them
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
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
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
What is the idea behind abstraction?
Hide implementation details
expose a number of methods for others to use!
What is coupling?
The level of dependency between classes
An extremely important concept in OOP
Many developers don’t understand this
What do we want to reduce?
The level of coupling between classes
no such thing as zero coupling
all classes are working together
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
What is the benefit of reducing coupling?
Reducing coupling, reducing impact of changes
What’s a best practice for reducing coupling?
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
What’s the main idea behind abstraction and reducing class coupling?
Reduce the number of methods exposed outside of a class
Reduces coupling
Reduces complexity
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
What is a good IntelliJ shortcut?
Option + Enter
Opens lightbulb for creating methods and fields
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
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
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
What is the general convention for organizing members in a class?
Public Fields
Private Fields
Constructors
Public Methods
Private Methods
Getters/Setters
What is a constructor?
A special method that is called when we instantiate an object
Used to create a new object
What is the role of the constructor?
initialize our fields to their default values
Num = 0;
boolean = false
reference type = null;
If we don’t create a custom constructor, what will Java do?
Java compiler will initialize a custom constructor for us
default constructor
all values set to default values!
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!