Unit 4
Unit 4
History of OOP
2. Features of OOP
3. Object-Oriented Concepts
java
Copy code
class Car {
String model;
void drive() { System.out.println("Driving " + model); }
}
Car myCar = new Car();
myCar.model = "Toyota";
myCar.drive(); // Output: Driving Toyota
4. Inheritance
Concept: Allows a class to inherit properties and methods of
another class.
Example:
java
Copy code
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void bark() { System.out.println("Dog barks"); }
}
Dog dog = new Dog();
dog.sound(); // Output: Animal sound
dog.bark(); // Output: Dog barks
5. Packages
java
Copy code
package com.myapp;
public class Utility { /*...*/ }
6. Interface
java
Copy code
interface Animal {
void sound();
}
class Cat implements Animal {
public void sound() { System.out.println("Meow"); }
}
java
Copy code
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() { System.out.println("Drawing Circle"); }
}
8. Polymorphism
java
Copy code
Animal myAnimal = new Dog();
myAnimal.sound(); // Calls Dog's implementation
9. Inner Classes
java
Copy code
String str = "Hello World";
System.out.println(str.toUpperCase()); // Output: HELLO
WORLD
java
Copy code
FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
12. Networking
java
Copy code
button.addActionListener(e -> System.out.println("Button
clicked"));
14. Multithreading
java
Copy code
class MyThread extends Thread {
public void run() { System.out.println("Running thread"); }
}
MyThread t1 = new MyThread();
t1.start();
Concept: Provides data structures like List, Set, and Map for
handling collections of objects.
Example:
java
Copy code
List<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");
16. APIs