Big-O and Object-Oriented Concepts
Definition: Describes the upper bound of an algorithm’s time or space complexity. Represents the worst-case scenario.
Common Big-O Values:
- O(1) - Constant time
- O(log n) - Logarithmic time
- O(n) - Linear time
- O(n log n) - Linearithmic time
- O(n^2) - Quadratic time
- O(2^n) - Exponential time
- O(n!) - Factorial time
|
Key Considerations: Focuses on how the runtime or space requirements grow as the input size increases.
|
Examples:
- Accessing an element in an array by index: O(1)
- Searching for an element in a linked list: O(n)
- Sorting an array using merge sort: O(n log n)
|
Default Constructor: A constructor with no parameters. If no constructor is defined, Java provides a default constructor.
Parameterized Constructor: A constructor with parameters to initialize object attributes.
this Keyword: Refers to the current object. Used to differentiate between instance variables and method parameters with the same name.
super Keyword: Refers to the parent class. Used to call the parent class’s constructor or access parent class members.
|
Copy Constructor: Creates a new object that is a copy of an existing object.
* Shallow Copy: Copies the values of the object’s fields. If the fields are references to other objects, only the references are copied.
* Deep Copy: Copies the values of the object’s fields and recursively copies the objects referenced by those fields.
|
Example (Copy Constructor - Deep Copy):
public class MyClass {
private int[] data;
public MyClass(MyClass other) {
this.data = new int[other.data.length];
for (int i = 0; i < other.data.length; i++) {
this.data[i] = other.data[i];
}
}
}
|
Inheritance: A mechanism where a new class (child class) inherits properties and behaviors from an existing class (parent class). Use the extends keyword.
Reference Diagrams: Visual representations of object relationships and memory allocation.
Reference Semantics: Variables hold references to objects, not the objects themselves. Assigning one variable to another copies the reference, not the object.
|
Polymorphism: The ability of an object to take on many forms. Achieved through inheritance and interfaces.
* Overriding: Providing a specific implementation of a method in a subclass that is already defined in its superclass.
* Overloading: Defining multiple methods in the same class with the same name but different parameters.
|
Example (Inheritance):
class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
|
ArrayLists, Generics, and Expressions
ArrayList: A dynamic array that can grow or shrink in size. Part of the java.util package.
Adding Elements: add(element) appends to the end, add(index, element) inserts at a specific index.
Removing Elements: remove(index) removes the element at the specified index, remove(Object o) removes the first occurrence of the specified element.
Printing Elements: Iterate through the ArrayList and print each element.
Example:
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
System.out.println(names); // Output: [Alice, Bob]
names.remove("Alice");
System.out.println(names); // Output: [Bob]
|
Benefits of Generics with ArrayList: Type safety, prevents runtime errors related to incorrect types.
|
Boxing: Automatic conversion of a primitive type to its corresponding wrapper class object (e.g., int to Integer ).
Unboxing: Automatic conversion of a wrapper class object to its corresponding primitive type (e.g., Integer to int ).
Example:
Integer intObj = 5; // Boxing
int num = intObj; // Unboxing
|
Potential Issues: NullPointerException if unboxing a null wrapper object.
|
Concrete Class: A class that provides implementations for all its methods. Can be directly instantiated using new .
Interface: A blueprint of a class. Contains only abstract methods (methods without implementation) and constants. Cannot be directly instantiated, but can be implemented by classes.
Example:
interface MyInterface {
void doSomething();
}
class MyClass implements MyInterface {
@Override
public void doSomething() {
System.out.println("Doing something");
}
}
MyClass obj = new MyClass(); // Valid
// MyInterface iface = new MyInterface(); // Invalid - cannot instantiate an interface
MyInterface iface = new MyClass(); // Valid - instantiating a class that *implements* the interface
|