Data Analytics
27K subscribers
1.16K photos
24 videos
26 files
977 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 6/10: Interfaces & Abstract Classes #Java #OOP #Interfaces #AbstractClasses #Programming Welcome to Part 6 of our Java series! Today we'll explore two crucial concepts for achieving abstraction in Java: Interfaces and…
# πŸ“š Java Programming Language – Part 7/10: Packages & Access Modifiers
#Java #Packages #AccessModifiers #Encapsulation

Welcome to Part 7 of our Java series! Today we'll explore how to organize code using packages and control visibility with access modifiers.

---

## πŸ”Ή Packages in Java
Packages help organize classes and prevent naming conflicts.

### 1. Creating and Using Packages
// File: com/example/utils/MathHelper.java
package com.example.utils; // Package declaration

public class MathHelper {
public static int add(int a, int b) {
return a + b;
}
}

// File: MainApp.java
import com.example.utils.MathHelper;

public class MainApp {
public static void main(String[] args) {
int sum = MathHelper.add(5, 3);
System.out.println("Sum: " + sum);
}
}


### 2. Common Java Packages
| Package | Contents |
|---------|----------|
| java.lang | Core classes (auto-imported) |
| java.util | Collections, date/time |
| java.io | Input/output operations |
| java.net | Networking |

---

## πŸ”Ή Access Modifiers
Control visibility of classes, methods, and variables.

### 1. Access Levels Overview
| Modifier | Class | Package | Subclass | World |
|----------|-------|---------|----------|-------|
| public | βœ… | βœ… | βœ… | βœ… |
| protected | βœ… | βœ… | βœ… | ❌ |
| default (no modifier) | βœ… | βœ… | ❌ | ❌ |
| private | βœ… | ❌ | ❌ | ❌ |

### 2. Practical Examples
public class BankAccount {
private double balance; // Only accessible within class

public String accountNumber; // Accessible everywhere

protected String ownerName; // Accessible in package and subclasses

void displayBalance() { // Package-private (default)
System.out.println("Balance: " + balance);
}
}


---

## πŸ”Ή Encapsulation Deep Dive
Proper encapsulation = private fields + public methods.

### 1. Proper Encapsulation Example
public class Student {
private String id;
private String name;
private double gpa;

// Constructor
public Student(String id, String name) {
this.id = id;
this.name = name;
}

// Getter methods
public String getId() { return id; }
public String getName() { return name; }
public double getGpa() { return gpa; }

// Setter methods with validation
public void setName(String name) {
if (name != null && !name.isEmpty()) {
this.name = name;
}
}

public void updateGpa(double newGpa) {
if (newGpa >= 0 && newGpa <= 4.0) {
this.gpa = newGpa;
}
}
}


### 2. Benefits of Encapsulation
βœ”οΈ Better control over data
βœ”οΈ Validation in setters
βœ”οΈ Hiding implementation details
βœ”οΈ Easier to modify internal representation

---

## πŸ”Ή Static Import
Import static members directly.

import static java.lang.Math.PI;
import static java.lang.Math.pow;

public class Circle {
public static double calculateArea(double radius) {
return PI * pow(radius, 2);
}
}


---

## πŸ”Ή Practical Example: Library Management
package com.library.models;

public class Book {
private String isbn;
private String title;
private String author;
private boolean isAvailable;

public Book(String isbn, String title, String author) {
this.isbn = isbn;
this.title = title;
this.author = author;
this.isAvailable = true;
}

// Getters and setters
public String getIsbn() { return isbn; }
public String getTitle() { return title; }
public boolean isAvailable() { return isAvailable; }

public void setAvailable(boolean available) {
isAvailable = available;
}
}

package com.library.system;

import com.library.models.Book;

public class Library {
public void borrowBook(Book book) {
if (book.isAvailable()) {
book.setAvailable(false);
System.out.println("Book borrowed: " + book.getTitle());
} else {
System.out.println("Book not available");
}
}
}


---
❀2