oop notes
oop notes
Definition:
An exception is an error that occurs during runtime. Exception handling allows for
managing these errors gracefully.
Key Components:
o try: Block to test code for errors.
o catch: Handles exceptions thrown in the try block.
o finally: Executes cleanup code, regardless of whether an exception occurred.
o throw: Used to explicitly throw an exception.
o throws: Declares exceptions a method might throw.
Code Examples:
Definition:
Serialization is the process of converting an object into a byte stream to save it to a file or
transmit it over a network. Deserialization recreates the object from this stream.
Steps to Serialize and Deserialize:
o Implement the Serializable interface in the class.
o Use ObjectOutputStream to write the object.
o Use ObjectInputStream to read the object.
Code Examples:
import java.io.*;
// Serializable class
public class Student implements Serializable {
int id;
String name;
// Serialization
Student student = new Student(1, "John");
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("student.dat"));
out.writeObject(student);
out.close();
// Deserialization
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("student.dat"));
Student s = (Student) in.readObject();
in.close();
System.out.println(s.id + " " + s.name);
Definition:
Generics enable types (classes and methods) to operate on objects of various types while
providing compile-time type safety.
Benefits:
o Compile-time type checking.
o No need for type casting.
// Generic Class
public class Box<T> {
private T value;
Wildcard Examples:
Serialization Example:
import java.io.*;
// Serialization
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("student.dat"));
out.writeObject(student);
out.close();
System.out.println("Serialization done.");
}
}
import java.io.*;
System.out.println("Deserialization done.");
System.out.println("ID: " + student.id + ", Name: " +
student.name);
}
}
public T get() {
return value;
}
java
CopyEdit
public class Main {
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.print(element + " ");
}
System.out.println();
}
printArray(intArray);
printArray(strArray);
}
}
Wildcard Example:
java
CopyEdit
import java.util.ArrayList;
import java.util.List;
printList(strings);
}
}
Practice question
1. Relationships Between Objects
Definition:
Relationships between objects define how objects interact and
relate to each other.
o Association: A "uses-a" relationship (e.g., a student uses a
library).
o Aggregation: A "has-a" relationship (e.g., a department has
professors).
o Composition: A "part-of" relationship (e.g., a car has an
engine).
class Engine {
void start() {
System.out.println("Engine started.");
}
}
class Car {
private Engine engine = new Engine(); // Composition
void drive() {
engine.start();
System.out.println("Car is driving.");
}
}
public class Main { public static void main(String[] args) {Car car = new
Car();
car.drive();
}
}
Practice Question:
Implement a class Library that contains multiple Books using aggregation.
Here are short notes, code examples, and practice questions for each topic, simplified for
easier understanding:
Definition:
Relationships between objects define how objects interact and relate to each other.
o Association: A "uses-a" relationship (e.g., a student uses a library).
o Aggregation: A "has-a" relationship (e.g., a department has professors).
o Composition: A "part-of" relationship (e.g., a car has an engine).
Code Example:
java
CopyEdit
class Engine {
void start() {
System.out.println("Engine started.");
}
}
class Car {
private Engine engine = new Engine(); // Composition
void drive() {
engine.start();
System.out.println("Car is driving.");
}
}
Practice Question:
Implement a class Library that contains multiple Books using aggregation.
Abstract Class:
o Cannot be instantiated.
o Used as a base class to provide common functionality for
derived classes.
o Contains abstract methods (no body).
Final Class:
o Cannot be extended.
o Used to prevent inheritance.
Practice Question:
Create an abstract class Shape with a method calculateArea().
Extend it to Circle and Rectangle.
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks.");
void sound() {
System.out.println("Cat meows.");
dog.sound();
cat.sound();
}
}
3. Introduction to GUI (Graphical User Interface)
Definition:
GUI allows users to interact with the application using graphical
elements like buttons, text fields, etc.
Practice Question:
Create a GUI with a text field and a button. When clicked, the
button should display the entered text in a label.
import javax.swing.*;
frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
}
}
4. Inheritance
Definition:
Inheritance allows a class to inherit properties and methods from
another class.
Practice Question:
Create a class Person with properties name and age. Extend it to
Student with additional property grade.
Code Example:
class Parent {
void display() {
System.out.println("I am a parent class.");
}
}
Definition:
Encapsulation hides the internal state of an object and allows
controlled access through getters and setters.
Practice Question:
Create a class Car with private properties speed and fuel. Use
methods to access and modify them.
Code Example:
class BankAccount {
private double balance;
6. Method Overloading
Definition:
Method overloading allows multiple methods with the same name
but different parameter lists.
Practice Question:
Create a class Shape with an overloaded method calculateArea()
for circle and rectangle.
Code Example:
class Calculator {
int add(int a, int b) {
return a + b;
}