OOP Flashcards
Abstraction
Using simple things to represent complex concepts. (e.g: wrapping a complex algorithm in a class, so the caller can just get the result without worry about what is involved in generating it)
Encapsulation
Protecting objects from external modification by offering a well defined public interface for consumers to work with (e.g: class variables are hidden and can only be accessed by getters/setters and methods)
Inheritance
Allows class to use the properties/methods of another class, child(subclass)-parent(superclass), java use ‘extends’ [is-a relationship]
public class Cat extends Animal{}
Polymorphism
Many forms, compile time (overloading - static) and run time (overriding - dynamic)
A conceptual example is a class of Type Horse extends a class called Animal. Both classes implement a method move(). If an instance of Horse is created and stored in an Animal variable then when we invoke animal.move() it will use the horse’s implementation of move not the Animals.
Inheritance vs Composition
Is a vs has a
Tightly coupled vs loosely coupled
Composition
Instance variable of another object [has-a relationship]
public class Person {
private Job job; //composition has-a relationship
}
Abstract Class
Cannot be instantiated, must be inherited, can have both abstract/regular methods, abstract methods don’t have a body
Interface
Contract with a set of methods that a class must implement, class can implement multiple interfaces, member variables are final by default
Overloading
Same named methods with different arguments
Overriding
Overriding a method from its parent class with different behavior
Multithreading
Small unit of work in parallel, multiple threads concurrently, maximize cpu
Deadlock
Threads blocked waiting on eachother
Synchronization
Only 1 thread can execute at a time
Checked Exceptions
IO or compile time, must be caught or program will not compile
Unchecked Exceptions - runtime or null pointer, program will run without catching them
Unchecked Exceptions
Runtime or null pointer, program will run without catching them
Chained Exceptions
One exception causes another exception
Class
blueprint/template, encapsulates data for the object
public class Class {
// fields or member variables
// constructor
// methods (defines behaviors or operations)
// getters/setters (access and modifiers)
// equals (default compares memory addresses, override to compare contents - when storing in collections like HashMap/HashSet this ensures duplicate objects are handled)
// hashcode (returns int value represents the object within a collection that uses hashing HashMap/HashSet)
// toString (string representation of the object, default is hex of hashcode)
}
Objects
Instance of a class
Garbage Collection
Program on JVM to delete unused objects, automatic memory management
Memory Leaks
Places where memory is not reclaimed when the object isn’t being used anymore
Primitive Type
byte,short,int,long,char,double,boolean (literals)
Wrapper Class
Byte,Short,Int,Long,Character ,Double,Boolean (Objects)
Immutable
Final, value of object is modified, a new object must be created
Autoboxing
auto converts primitive to wrapper class object when used in a collection
Unboxing
Auto converts wrapper class to primitive
Problem with Boxing/Unboxing
Overhead for garbage collector (conversion from value types to reference types (objects) introduce additional memory management.
When a int is boxed, it is wrapped in an object and stored on the heap instead of the stack. Heap allocations are managed by the GC. GC is responsible for tracking & eventually cleaning them up.
Static Variable/Method
Belongs to the class, can be called without creating an instance
Final Variable
Cannot be changed, constant
Final Method
Cannot be overridden
Final Class
Cannot be inherited
What are the access modifiers?
Private - only accessed in the class
Protected - only accessed in the class and subclasses
Public - accessed from anywhere
Diamond Problem
Ambiguity that arises from two classes B/C inherit from a superclass A, and another class D inherits from B/C, this is why you can’t extend multiple abstract classes