1.
Classes, Objects, and Methods
Key Concepts
• OOP (Object-Oriented Programming) – uses objects and classes to model real-world
entities.
• Class – blueprint/template for creating objects.
• Object – instance of a class.
• Method – block of code that performs a task.
• Data Abstraction – hides unnecessary details.
• Encapsulation – wraps data and methods into a class.
• Inheritance – allows one class to acquire properties of another.
• Polymorphism – one interface, many forms.
OOP vs Procedural Programming
OOP Procedural
Focuses on objects/data Focuses on procedures
Data secured Data less secure
Bottom-up approach Top-down approach
Models real-world entities Does not model entities
Important Terms
• Getter (Accessor) – retrieves a value.
• private – highest data security.
• public – accessible by other classes.
• static – can be used without creating an object.
• void – method returns nothing.
Possible Exam Questions
1. Difference between Class and Object.
2. Four OOP concepts.
3. Purpose of methods.
4. Meaning of public, private, static, and void.
2. Inheritance and Interfaces
Inheritance
• Allows a class to inherit fields and methods from another class.
• Uses the extends keyword.
• Promotes code reuse.
• Creates an IS-A relationship.
Example
class Car extends Vehicle {
}
Method Overriding
• Subclass provides its own version of a superclass method.
• Same method name and signature.
• Uses @Override.
super Keyword
Used to call superclass methods.
[Link]();
Interfaces
• Collection of abstract methods.
• Uses interface keyword.
• Implemented using implements keyword.
• Cannot instantiate interface objects.
Keywords to Remember
• extends → inheritance
• implements → interface implementation
• super → access superclass member
Common Exam Questions
1. Difference between inheritance and interface.
2. Purpose of super.
3. What is method overriding?
4. Difference between extends and implements.
3. Exception Handling
Debugging
Process of finding and fixing program errors.
Debugging Techniques
1. Understand the problem
2. Backtracking
3. Debugging tools
4. Breakpoints
5. Binary search
6. Rubber ducking
7. Log analysis
8. Clustering bugs
9. Take breaks
10. Take notes
Exception Handling
Error vs Exception
Error Exception
Serious problem Recoverable problem
Usually cannot recover Can recover
Common Exceptions
Exception Meaning
ArithmeticException Divide by zero
InputMismatchException Wrong input type
IOException File access problem
ArrayIndexOutOfBoundsException Invalid array index
NoSuchElementException Element doesn't exist
try-catch-finally
try
Contains risky code.
catch
Handles exceptions.
finally
Executes whether an exception occurs or not.
try{
// code
}
catch(Exception e){
// handle error
}
finally{
// always executes
}
Common Exam Questions
1. Difference between Error and Exception.
2. Purpose of try-catch.
3. What is a stack trace?
4. Examples of runtime exceptions.
4. File Input and Output (I/O)
File Types
Text File
• Human readable
• Uses ASCII or Unicode
Binary File
• Stored as 0s and 1s
• Examples: images, music, class files
Path Class
Stores file location information.
Path filePath = [Link]("C:\\[Link]");
Absolute vs Relative Path
Absolute Path
Complete file location.
C:\Java\[Link]
Relative Path
Depends on current directory.
[Link]
Useful Path Methods
Method Purpose
toString() String version of path
getFileName() Gets file name
getNameCount() Counts path elements
getName() Gets path element
Files Class
Used for:
• Read files
• Write files
• Delete files
• Check file attributes
Common Exam Questions
1. Difference between text and binary files.
2. Absolute vs relative path.
3. Purpose of Path and Files classes.
5. Enumerations and Nested Classes
Enumeration (enum)
A fixed set of constants.
enum Period{
PRELIM,
MIDTERM,
PREFINAL,
FINAL
}
Enum Methods
Method Purpose
toString() Constant name
ordinal() Position number
equals() Compare constants
compareTo() Compare positions
Static Enum Methods
Method Purpose
valueOf() Convert string to enum
values() Returns enum array
Nested Classes
Class inside another class.
Types
1. Static Member Class
2. Inner Class
3. Local Class
4. Anonymous Class
Common Exam Questions
1. Purpose of enums.
2. Difference between ordinal() and valueOf().
3. Four types of nested classes.
6. Regular Expressions (Regex)
Definition
Pattern used to match strings.
Package:
[Link]
Common Symbols
Symbol Meaning
| OR
[] Character range
. Any character
Symbol Meaning
* 0 or more
+ 1 or more
? 0 or 1
{x} Exactly x
{x,y} Between x and y
Examples
"cat|dog"
Matches cat OR dog.
"[a-z]at"
Matches:
• cat
• bat
• hat
"[0-9]."
Digit followed by any character.
Pattern and Matcher
Pattern p = [Link]("[A-F]{5,}.*");
Matcher m = [Link](str);
Matcher Methods
Method Purpose
matches() Exact match
find() Search substring