Data Analytics
# 📚 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…
# 📚 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 Abstract Classes.
---
## 🔹 Interfaces in Java
Interfaces define contracts that classes must implement (100% abstraction).
### 1. Interface Declaration (Pre-Java 8)
### 2. Implementing Interfaces
### 3. Modern Interfaces (Java 8+) Features
---
## 🔹 Abstract Classes
Classes that can't be instantiated and may contain abstract methods.
### 1. Abstract Class Example
### 2. Extending Abstract Classes
---
## 🔹 Key Differences
| Feature | Interface | Abstract Class |
|------------------------|-----------------------------------|------------------------------------|
| Instantiation | Cannot be instantiated | Cannot be instantiated |
| Methods | All abstract (pre-Java 8) | Can have both abstract & concrete |
| Variables | Only constants | Any variables |
| Multiple Inheritance | Class can implement many interfaces | Class extends only one abstract class |
| Default Methods | Supported (Java 8+) | Not applicable |
| Constructor | No constructors | Can have constructors |
| When to Use | Define contracts/APIs | Share code among related 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 Abstract Classes.
---
## 🔹 Interfaces in Java
Interfaces define contracts that classes must implement (100% abstraction).
### 1. Interface Declaration (Pre-Java 8)
interface Drawable {
// Constant fields (implicitly public static final)
String COLOR = "Black";
// Abstract methods (implicitly public abstract)
void draw();
double calculateArea();
}### 2. Implementing Interfaces
class Circle implements Drawable {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Drawing a circle");
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}### 3. Modern Interfaces (Java 8+) Features
interface Vehicle {
// Traditional abstract method
void start();
// Default method (with implementation)
default void honk() {
System.out.println("Beep beep!");
}
// Static method
static void printType() {
System.out.println("I'm a vehicle");
}
}---
## 🔹 Abstract Classes
Classes that can't be instantiated and may contain abstract methods.
### 1. Abstract Class Example
abstract class Animal {
// Concrete method
public void breathe() {
System.out.println("Breathing...");
}
// Abstract method (no implementation)
public abstract void makeSound();
}### 2. Extending Abstract Classes
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof woof!");
}
}
// Usage:
Animal myPet = new Dog();
myPet.breathe(); // Inherited concrete method
myPet.makeSound(); // Implemented abstract method---
## 🔹 Key Differences
| Feature | Interface | Abstract Class |
|------------------------|-----------------------------------|------------------------------------|
| Instantiation | Cannot be instantiated | Cannot be instantiated |
| Methods | All abstract (pre-Java 8) | Can have both abstract & concrete |
| Variables | Only constants | Any variables |
| Multiple Inheritance | Class can implement many interfaces | Class extends only one abstract class |
| Default Methods | Supported (Java 8+) | Not applicable |
| Constructor | No constructors | Can have constructors |
| When to Use | Define contracts/APIs | Share code among related classes |
---
Data Analytics
# 📚 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…
## 🔹 Practical Example: Payment System
---
## 🔹 Marker Interfaces
Interfaces with no methods (used to mark classes).
---
## 🔹 Functional Interfaces (Java 8+)
Interfaces with exactly one abstract method (for lambdas).
---
## 🔹 Best Practices
1. Use interfaces for multiple inheritance needs
2. Use abstract classes for code reuse among related classes
3. Prefer interfaces for APIs
4. Use @Override annotation consistently
5. Follow interface segregation principle (small, focused interfaces)
---
### 📌 What's Next?
In Part 7, we'll cover:
➡️ Packages
➡️ Access Modifiers
➡️ Encapsulation Deep Dive
#JavaInterfaces #AbstractClasses #OOPConcepts 🚀
interface PaymentMethod {
void processPayment(double amount);
boolean validate();
}
abstract class OnlinePayment implements PaymentMethod {
protected String cardNumber;
public OnlinePayment(String cardNumber) {
this.cardNumber = cardNumber;
}
@Override
public boolean validate() {
return cardNumber != null && cardNumber.length() == 16;
}
}
class CreditCardPayment extends OnlinePayment {
public CreditCardPayment(String cardNumber) {
super(cardNumber);
}
@Override
public void processPayment(double amount) {
if (validate()) {
System.out.printf("Processing $%.2f via credit card %s\n",
amount, cardNumber.substring(12));
}
}
}
class PayPalPayment implements PaymentMethod {
private String email;
public PayPalPayment(String email) {
this.email = email;
}
@Override
public boolean validate() {
return email != null && email.contains("@");
}
@Override
public void processPayment(double amount) {
if (validate()) {
System.out.printf("Processing $%.2f via PayPal to %s\n",
amount, email);
}
}
}---
## 🔹 Marker Interfaces
Interfaces with no methods (used to mark classes).
interface Serializable { } // Example of marker interface
class MyClass implements Serializable {
// Class implementation
}---
## 🔹 Functional Interfaces (Java 8+)
Interfaces with exactly one abstract method (for lambdas).
@FunctionalInterface
interface Calculator {
int operate(int a, int b);
}
// Usage with lambda:
Calculator add = (a, b) -> a + b;
Calculator multiply = (a, b) -> a * b;
System.out.println(add.operate(5, 3)); // 8
System.out.println(multiply.operate(5, 3)); // 15
---
## 🔹 Best Practices
1. Use interfaces for multiple inheritance needs
2. Use abstract classes for code reuse among related classes
3. Prefer interfaces for APIs
4. Use @Override annotation consistently
5. Follow interface segregation principle (small, focused interfaces)
---
### 📌 What's Next?
In Part 7, we'll cover:
➡️ Packages
➡️ Access Modifiers
➡️ Encapsulation Deep Dive
#JavaInterfaces #AbstractClasses #OOPConcepts 🚀
❤3