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
### 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.
---
## πΉ super Keyword
Used to access superclass members from subclass.
### 1. Accessing Superclass Methods
### 2. Superclass Constructor
---
## πΉ Polymorphism
"Many forms" - ability of an object to take many forms.
### 1. Compile-time Polymorphism (Method Overloading)
### 2. Runtime Polymorphism (Method Overriding)
---
## πΉ final Keyword
Restricts inheritance and overriding.
---
## πΉ Object Class
All classes implicitly extend Java's
Important methods:
-
-
-
---
#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 valueclass 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
---
## πΉ 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π
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