Oops
Oops
OOP is a programming paradigm based on objects that contain data (attributes) and code
(methods). Objects are instances of classes, which act as blueprints.
2. Key Concepts
Class Example:
class Employee {
employeeName = s;
Object Creation:
obj1.setName("Raj");
Constructor in Java
Definition:
A constructor is a special method in a class used to initialize objects when they are created.
It has the same name as the class and no return type.
Types of Constructors:
1. Non-parameterized Constructor
class Employee {
Employee() {
System.out.println("Employee created!");
}
}
2. Parameterized Constructor
class Employee {
String employeeName;
int salary;
this.employeeName = name;
this.salary = salary;
3. Copy Constructor
class Employee {
String employeeName;
int salary;
Employee(Employee e) {
this.employeeName = e.employeeName;
this.salary = e.salary;
• Constructor Chaining:
• Constructor Overloading:
String employeeName;
int salary;
Employee() {
this("Unknown", 0);
this.employeeName = name;
this.salary = salary;
Advantages:
• Definition: Bundling of data (attributes) and methods into a single unit (class) to
protect data from unauthorized access.
• Key Concept:
• Example:
class BankAccount {
}
Access Modifiers
Public
Protected
Default
Private
void showMessage()
Inheritance
Inheritance allows a child class to inherit properties and methods from a parent class,
promoting code reuse and hierarchy.
Key Concepts
• Parent Class (Superclass) - Provides common properties and methods.
• Child Class (Subclass) - Inherits and can modify or extend parent class functionality.
Polymorphism
Polymorphism allows the same method or object to behave differently based on context.
o Resolved at compile-time.
Abstraction in Java
Abstraction hides implementation details and shows only the essential features. It helps
simplify complex systems and improve maintainability.
Key Features
Benefits
1. Abstract Classes
2. Interfaces
interface Example {
Example.staticMethod();
interface Example {
new Demo().defaultMethod();
Why?
Common Questions
Can an abstract class extend another abstract class? Yes, it inherits methods but
doesn’t need to implement them unless it's concrete.
Can an abstract class have a constructor? Yes, but it cannot be instantiated directly.
The constructor runs when a subclass is created.
Interface in Java
• Example:
interface Animal {
void eat();
void sleep();
• Key Points:
interface Example {
new Demo().defaultMethod();
}
interface Utility {
class Test {
Utility.staticMethod();
Interface Inheritance
Static Variables
• Example:
class Counter {
Counter() { count++; }
Static Methods
• Example:
class MathUtils {
class Main {
System.out.println(MathUtils.add(5, 3));
Static Block
• Example:
class Example {
class Example {
class Utils {
System.out.println(message);
class Main {
Utils.printMessage("Hello, Static!");
Inner classes are defined within another class, allowing better encapsulation and logical
grouping. They have access to all members of the outer class.
class Outer {
o Example:
class Outer {
int y = 42;
class Inner {
o Example:
class Outer {
void method() {
class LocalInner {
}
}
o Example:
};
1. Association
• Types:
2. Aggregation
class Department {
List<Employee> employees;
3. Composition
• Example: House contains Rooms, and if the house is destroyed, rooms cease to exist.
class House {
Comparison Table
Teacher-
Example Employee-Department Car-Engine
Student
Object cloning is creating an exact copy of an object with a different memory location. It is
done using the Cloneable interface and the clone() method of the Object class.
Key Points:
• Shallow cloning shares references, while deep cloning creates new copies.
Working of Cloning
Types of Cloning
1. Shallow Cloning
Example:
class Address {
String city;
String name;
Address address;
2. Deep Cloning
Example:
String city;
String name;
Address address;
return cloned;
Exception handling manages runtime errors and ensures the normal flow of a program.
• Improves debugging.
Try-Catch Mechanism
Example:
try {
int num = 10 / 0;
} catch (ArithmeticException e) {
} finally {
System.out.println("Execution completed.");
throw vs throws
throw new
Example void divide() throws ArithmeticException
ArithmeticException("Error");
Custom Exceptions
CustomException(String message) {
super(message);
Example:
• Checked:
• Unchecked:
int num = 10 / 0;
Real-Life Example
try {
} catch (FileNotFoundException e) {
Introduction
• Allows defining classes, interfaces, and methods that work with multiple data types.
Key Benefits
list.add("Hello");
list.add(100);
String str = (String) list.get(1);
list.add("Hello");
1. Generic Classes
Syntax:
class Box<T> {
private T value;
2. Generic Methods
Syntax:
class Example {
Syntax:
private T num;
list.add(10);
rawBox.set("Hello");
• Compiler replaces generic types with raw types (Object or upper bound).
class Box<T> {
T value;
this.value = value;
T get() {
return value;
class Box {
Object value;
this.value = value;
Object get() {
return value;
}}
Impact: No runtime type information, cannot use instanceof with generics, generic
arrays not allowed.
Summary
File Handling refers to reading and writing files for data storage and retrieval.
• Create a file
• Write to a file
• Append data
• Delete a file
import java.io.File;
import java.io.IOException;
class Main {
try {
if (file.createNewFile())
} catch (IOException e) {
e.printStackTrace();
}
Handling Errors: IOException is used for handling errors like invalid paths or permission
issues.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
class Main {
} catch (IOException e) {
e.printStackTrace();
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class Main {
public static void main(String[] args) {
System.out.println(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
import java.io.*;
class Logger {
this.path = path;
new File(path).createNewFile();
writer.write(message);
writer.newLine();
} catch (IOException e) {
System.out.println("Logging failed!");
}}
Common Issues