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 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…
# πŸ“š Java Programming Language – Part 5/10: Inheritance & Polymorphism
#Java #OOP #Inheritance #Polymorphism #Programming

Welcome to Part 5 of our Java series! Today we'll explore two fundamental OOP concepts: Inheritance and Polymorphism.

---

## πŸ”Ή Inheritance in Java
Inheritance allows a class to acquire properties and methods of another class.

### 1. Basic Inheritance Syntax
// Parent class (Superclass)
class Vehicle {
String brand;

public void start() {
System.out.println("Vehicle starting...");
}
}

// Child class (Subclass)
class Car extends Vehicle { // 'extends' keyword
int numberOfDoors;

public void honk() {
System.out.println("Beep beep!");
}
}

// Usage:
Car myCar = new Car();
myCar.brand = "Toyota"; // Inherited from Vehicle
myCar.start(); // Inherited method
myCar.honk(); // Child's own method


### 2. Inheritance Types
Java supports:
- Single Inheritance (One parent β†’ one child)
- Multilevel Inheritance (Grandparent β†’ parent β†’ child)
- Hierarchical Inheritance (One parent β†’ multiple children)

*Note: Java doesn't support multiple inheritance with classes*

---

## πŸ”Ή Method Overriding
Subclass can provide its own implementation of an inherited method.

class Vehicle {
public void start() {
System.out.println("Vehicle starting...");
}
}

class ElectricCar extends Vehicle {
@Override // Annotation (optional but recommended)
public void start() {
System.out.println("Electric car starting silently...");
}
}


---

## πŸ”Ή super Keyword
Used to access superclass members from subclass.

### 1. Accessing Superclass Methods
class ElectricCar extends Vehicle {
@Override
public void start() {
super.start(); // Calls Vehicle's start()
System.out.println("Battery check complete");
}
}


### 2. Superclass Constructor
class Vehicle {
String brand;

public Vehicle(String brand) {
this.brand = brand;
}
}

class Car extends Vehicle {
int doors;

public Car(String brand, int doors) {
super(brand); // Must be first statement
this.doors = doors;
}
}


---

## πŸ”Ή Polymorphism
"Many forms" - ability of an object to take many forms.

### 1. Compile-time Polymorphism (Method Overloading)
class Calculator {
// Same method name, different parameters
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}


### 2. Runtime Polymorphism (Method Overriding)
Vehicle v1 = new Vehicle();  // Parent reference, parent object
Vehicle v2 = new Car(); // Parent reference, child object

v1.start(); // Calls Vehicle's start()
v2.start(); // Calls Car's start() if overridden


---

## πŸ”Ή final Keyword
Restricts inheritance and overriding.

final class CannotBeExtended { }  // Cannot be inherited

class Parent {
final void cannotOverride() { } // Cannot be overridden
}


---

## πŸ”Ή Object Class
All classes implicitly extend Java's Object class.

Important methods:
- toString() - String representation
- equals() - Compare objects
- hashCode() - Hash code value

class MyClass { }  // Automatically extends Object

MyClass obj = new MyClass();
System.out.println(obj.toString()); // Outputs something like MyClass@1dbd16a6


---
Data Analytics
# πŸ“š 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…
## πŸ”Ή Practical Example: Employee Hierarchy
class Employee {
String name;
double salary;

public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}

public void work() {
System.out.println(name + " is working...");
}
}

class Manager extends Employee {
String department;

public Manager(String name, double salary, String dept) {
super(name, salary);
this.department = dept;
}

@Override
public void work() {
System.out.println(name + " is managing " + department);
}

public void conductMeeting() {
System.out.println("Conducting department meeting");
}
}

// Usage:
Employee emp1 = new Employee("Ahmed", 5000);
Manager mgr1 = new Manager("Fatima", 8000, "Marketing");

emp1.work(); // "Ahmed is working..."
mgr1.work(); // "Fatima is managing Marketing"
mgr1.conductMeeting();


---

## πŸ”Ή Best Practices for Inheritance
1. Favor Composition Over Inheritance - When possible
2. Keep Inheritance Hierarchies Shallow - Avoid deep inheritance trees
3. Use Abstract Classes for Partial Implementations
4. Document Overridden Methods Properly
5. Follow Liskov Substitution Principle - Subclass should be substitutable for superclass

---

### πŸ“Œ What's Next?
In Part 6, we'll cover:
➑️ Interfaces
➑️ Abstract Classes
➑️ Difference Between Interfaces and Abstract Classes

#JavaOOP #Inheritance #Polymorphism πŸš€
Please open Telegram to view this post
VIEW IN TELEGRAM