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
### 2. Common Java Packages
| Package | Contents |
|---------|----------|
|
|
|
|
---
## πΉ Access Modifiers
Control visibility of classes, methods, and variables.
### 1. Access Levels Overview
| Modifier | Class | Package | Subclass | World |
|----------|-------|---------|----------|-------|
|
|
|
|
### 2. Practical Examples
---
## πΉ Encapsulation Deep Dive
Proper encapsulation = private fields + public methods.
### 1. Proper Encapsulation Example
### 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.
---
## πΉ Practical Example: Library Management
---
#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