OOPs_Java_Notes
OOPs_Java_Notes
1. Introduction to OOP
which can contain data and code. Data is in the form of fields (also known as attributes),
- Encapsulation: Wrapping data (variables) and code (methods) together as a single unit. Access
- Abstraction: Hiding complex implementation details and showing only the necessary features.
- Inheritance: One class inherits properties and behaviors from another class.
class Car {
String color;
void drive() {
System.out.println("Car is driving");
}
- Object: Instance of a class.
myCar.drive();
- Constructor:
Car(String c) {
color = c;
4. Inheritance Example
class Animal {
void sound() {
System.out.println("Animal sound");
void sound() {
System.out.println("Bark");
5. Abstraction Example
void draw() {
System.out.println("Drawing Circle");
6. Interface Example
interface Drawable {
void draw();
System.out.println("Drawing Rectangle");
7. Polymorphism Example
s.draw();
8. Final Keyword
Used for memory management. Static members belong to the class, not to instances.
Conclusion:
OOP in Java allows better code organization, reuse, and maintainability by modeling real-world