Data Analytics
27.1K subscribers
1.17K photos
24 videos
29 files
985 links
Dive into the world of Data Analytics – uncover insights, explore trends, and master data-driven decision making.
Download Telegram
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
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);
}
}


---