Data Analytics
27K subscribers
1.17K photos
24 videos
28 files
981 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.iss.onember |
| 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);
}
}


---
Data Analytics
## ๐Ÿ”น Modern Browser APIs ### 1. Web Components class MyElement extends HTMLElement { connectedCallback() { this.innerHTML = `<h2>Custom Element</h2>`; } } customElements.define('my-element', MyElement); ### 2. Intersection Observer const observerโ€ฆ
# ๐Ÿ“š JavaScript Tutorial - Part 7/10: Object-Oriented JavaScript & Prototypes
#JavaScript #OOP #Prototypes #Classes #DesignPatterns

Welcome to Part 7 of our JavaScript series! This comprehensive lesson will take you deep into JavaScript's object-oriented programming (OOP) system, prototypes, classes, and design patterns.

---

## ๐Ÿ”น JavaScript OOP Fundamentals
### 1. Objects in JavaScript
JavaScript objects are dynamic collections of properties with a hidden [[Prototype]] property.

// Object literal (most common)
const person = {
name: 'Ali',
age: 25,
greet() {
console.log(`Hello, I'm ${this.name}`);
}
};

// Properties can be added dynamically
person.country = 'UAE';
delete person.age;


### 2. Factory Functions
Functions that create and return objects.

function createPerson(name, age) {
return {
name,
age,
greet() {
console.log(`Hi, I'm ${this.name}`);
}
};
}

const ali = createPerson('Ali', 25);


### 3. Constructor Functions
The traditional way to create object blueprints.

function Person(name, age) {
// Instance properties
this.name = name;
this.age = age;

// Method (created for each instance)
this.greet = function() {
console.log(`Hello, I'm ${this.name}`);
};
}

const ali = new Person('Ali', 25);


The `new` keyword does:
1. Creates a new empty object
2. Sets this to point to the new object
3. Links the object's prototype to constructor's prototype
4. Returns the object (unless constructor returns something else)

---

## ๐Ÿ”น Prototypes & Inheritance
### 1. Prototype Chain
Every JavaScript object has a [[Prototype]] link to another object.

// Adding to prototype (shared across instances)
Person.prototype.introduce = function() {
console.log(`My name is ${this.name}, age ${this.age}`);
};

// Prototype chain lookup
ali.introduce(); // Checks ali โ†’ Person.prototype โ†’ Object.prototype โ†’ null


### 2. Manual Prototype Inheritance
function Student(name, age, grade) {
Person.call(this, name, age); // "Super" constructor
this.grade = grade;
}

// Set up prototype chain
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;

// Add methods
Student.prototype.study = function() {
console.log(`${this.name} is studying hard!`);
};


### 3. Object.create()
Pure prototypal inheritance.

const personProto = {
greet() {
console.log(`Hello from ${this.name}`);
}
};

const ali = Object.create(personProto);
ali.name = 'Ali';


---

## ๐Ÿ”น ES6 Classes
Syntactic sugar over prototypes.

### 1. Class Syntax
class Person {
// Constructor (called with 'new')
constructor(name, age) {
this.name = name;
this.age = age;
}

// Instance method
greet() {
console.log(`Hello, I'm ${this.name}`);
}

// Static method
static compareAges(a, b) {
return a.age - b.age;
}
}


### 2. Inheritance with `extends`
class Student extends Person {
constructor(name, age, grade) {
super(name, age); // Must call super first
this.grade = grade;
}

study() {
console.log(`${this.name} is studying`);
}

// Override method
greet() {
super.greet(); // Call parent method
console.log(`I'm in grade ${this.grade}`);
}
}


### 3. Class Features (ES2022+)
class ModernClass {
// Public field (instance property)
publicField = 1;

// Private field (starts with #)
#privateField = 2;

// Static public field
static staticField = 3;

// Static private field
static #staticPrivateField = 4;

// Private method
#privateMethod() {
return this.#privateField;
}
}


---

## ๐Ÿ”น Property Descriptors
Advanced control over object properties.

### 1. Property Attributes
const obj = {};

Object.defineProperty(obj, 'readOnlyProp', {
value: 42,
writable: false, // Can't be changed
enumerable: true, // Shows up in for...in
configurable: false // Can't be deleted/reconfigured
});