Data Analytics
# π Java Programming Language β Part 3/10: Methods & Functions #Java #Programming #Methods #Functions #OOP Welcome to Part 3 of our Java series! Today we'll dive into methods - the building blocks of Java programs. --- ## πΉ What are Methods in Java? Methodsβ¦
# π Java Programming Language β Part 4/10: Object-Oriented Programming (OOP) Basics
#Java #OOP #Programming #Classes #Objects
Welcome to Part 4 of our Java series! Today we'll explore the fundamentals of Object-Oriented Programming in Java.
---
## πΉ What is OOP?
Object-Oriented Programming is a paradigm based on:
- Objects (instances of classes)
- Classes (blueprints for objects)
- 4 Main Principles:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
---
## πΉ Classes and Objects
### 1. Class Definition
### 2. Creating Objects
---
## πΉ Constructors
Special methods called when an object is instantiated.
### 1. Default Constructor
### 2. Parameterized Constructor
### 3. Constructor Overloading
---
## πΉ Encapsulation
Protecting data by making fields private and providing public getters/setters.
---
## πΉ 'this' Keyword
Refers to the current object instance.
---
## πΉ Practical Example: Student Management System
---
## πΉ Static vs Instance Members
| Feature | Static | Instance |
|---------------|---------------------------|---------------------------|
| Belongs to | Class | Object |
| Memory | Once per class | Each object has its own |
| Access | ClassName.member | object.member |
| Example |
---
#Java #OOP #Programming #Classes #Objects
Welcome to Part 4 of our Java series! Today we'll explore the fundamentals of Object-Oriented Programming in Java.
---
## πΉ What is OOP?
Object-Oriented Programming is a paradigm based on:
- Objects (instances of classes)
- Classes (blueprints for objects)
- 4 Main Principles:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
---
## πΉ Classes and Objects
### 1. Class Definition
public class Car {
// Fields (attributes)
String brand;
String model;
int year;
// Method
public void startEngine() {
System.out.println("Engine started!");
}
}### 2. Creating Objects
public class Main {
public static void main(String[] args) {
// Creating an object
Car myCar = new Car();
// Accessing fields
myCar.brand = "Toyota";
myCar.model = "Corolla";
myCar.year = 2022;
// Calling method
myCar.startEngine();
}
}---
## πΉ Constructors
Special methods called when an object is instantiated.
### 1. Default Constructor
public class Car {
// Default constructor (created automatically if none exists)
public Car() {
}
}### 2. Parameterized Constructor
public class Car {
String brand;
String model;
int year;
public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
}
// Usage:
Car myCar = new Car("Toyota", "Corolla", 2022);### 3. Constructor Overloading
public class Car {
// Constructor 1
public Car() {
this.brand = "Unknown";
}
// Constructor 2
public Car(String brand) {
this.brand = brand;
}
}---
## πΉ Encapsulation
Protecting data by making fields private and providing public getters/setters.
public class BankAccount {
private double balance; // Private field
// Public getter
public double getBalance() {
return balance;
}
// Public setter with validation
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}---
## πΉ 'this' Keyword
Refers to the current object instance.
public class Person {
private String name;
public Person(String name) {
this.name = name; // 'this' distinguishes field from parameter
}
}---
## πΉ Practical Example: Student Management System
public class Student {
private String id;
private String name;
private double gpa;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
// Getters and setters
public String getId() { return id; }
public String getName() { return name; }
public double getGpa() { return gpa; }
public void updateGpa(double newGpa) {
if (newGpa >= 0 && newGpa <= 4.0) {
this.gpa = newGpa;
}
}
public void printInfo() {
System.out.printf("ID: %s, Name: %s, GPA: %.2f\n",
id, name, gpa);
}
}
// Usage:
Student student1 = new Student("S1001", "Ahmed");
student1.updateGpa(3.75);
student1.printInfo();---
## πΉ Static vs Instance Members
| Feature | Static | Instance |
|---------------|---------------------------|---------------------------|
| Belongs to | Class | Object |
| Memory | Once per class | Each object has its own |
| Access | ClassName.member | object.member |
| Example |
Math.PI | student.getName() |public class Counter {
static int count = 0; // Shared across all instances
int instanceCount = 0; // Unique to each object
public Counter() {
count++;
instanceCount++;
}
public static void printCount() {
System.out.println("Total count: " + count);
}
}---