diff --git a/oop/code/oop/src/main/java/com/scaler/lld/App.java b/oop/code/oop/src/main/java/com/scaler/lld/App.java index 58c2e48..5008657 100644 --- a/oop/code/oop/src/main/java/com/scaler/lld/App.java +++ b/oop/code/oop/src/main/java/com/scaler/lld/App.java @@ -1,27 +1,58 @@ package com.scaler.lld; -import com.scaler.lld.basics.OopBankAccount; +import java.util.List; + +import com.scaler.lld.scaler.Student; +import com.scaler.lld.scaler.User; /** * Hello world! * */ -public class App -{ - public static void main( String[] args ) - { - // Create accounts for Alice and Bob - OopBankAccount accountAlice = new OopBankAccount(1, 1000, "Alice"); - OopBankAccount accountBob = new OopBankAccount(2, 2000, "Bob"); +public class App { + public static void main(String[] args) { + User student = new Student("student", "student@scaler.in", "batch", 100); + student.printInfo(); // inheritance + } - System.out.println("\nAlice's balance: " + accountAlice.getBalance()); - System.out.println("Bob's balance: " + accountBob.getBalance()); + public static void resetEmail(List users) { + for (User user : users) { + user.changeEmail(""); - System.out.println("\nTransferring $100 from Alice to Bob"); - // Transfer $100 from Alice to Bob - accountAlice.transfer(accountBob, 100); + if (user instanceof Student) { + Student student = (Student) user; + + System.out.println("Name :" + student.getName() + " " + student.getPsp()); + + student.setPsp(0); + System.out.println("Name :" + student.getName() + " " + student.getPsp()); + + } + + } - System.out.println("\nAlice's balance: " + accountAlice.getBalance()); - System.out.println("Bob's balance: " + accountBob.getBalance()); } } + +// Reusable code +// In order to reset email, I just had to call the parent's changeEmail fn +// Instead of defining a method for each class +// instanceof +// A a = (A) b; +// 6:05 +// 10:35 +// Three types of DP +// 1. Creational - OOP +// Factory - Simple Factory - No OOP +// 2. Structural - +// 3. Behavioural - + +// 1. Subtyping +// - Compile and run time + +// Method overloading - Compile time +// 2. Generic Polymorphism +// 3. Adhoc polymorphism - Duck Typing + +// PATCH /edit-student (JSON patchBody) +// { field: "name", op: "set", value: "Tanmay"} \ No newline at end of file diff --git a/oop/code/oop/src/main/java/com/scaler/lld/scaler/Mentor.java b/oop/code/oop/src/main/java/com/scaler/lld/scaler/Mentor.java new file mode 100644 index 0000000..94e351f --- /dev/null +++ b/oop/code/oop/src/main/java/com/scaler/lld/scaler/Mentor.java @@ -0,0 +1,25 @@ +package com.scaler.lld.subtyping; + +import java.util.ArrayList; +import java.util.List; + +import com.scaler.lld.scaler.Student; +import com.scaler.lld.scaler.User; + +import lombok.Getter; +import lombok.Setter; + +// Step 1 - Extend parent class +@Getter +@Setter +public class Mentor extends User { + private List mentees = new ArrayList<>(); + private String company; + + public Mentor(String name, String email, List mentees, String company) { + super(name, email); + this.mentees = mentees; + this.company = company; + } + +} diff --git a/oop/code/oop/src/main/java/com/scaler/lld/scaler/Student.java b/oop/code/oop/src/main/java/com/scaler/lld/scaler/Student.java index b41f93f..b70a0b0 100644 --- a/oop/code/oop/src/main/java/com/scaler/lld/scaler/Student.java +++ b/oop/code/oop/src/main/java/com/scaler/lld/scaler/Student.java @@ -1,12 +1,10 @@ package com.scaler.lld.scaler; import lombok.Getter; -import lombok.NoArgsConstructor; import lombok.Setter; @Getter @Setter -@NoArgsConstructor public class Student extends User { private String batchName; @@ -25,9 +23,16 @@ public class Student extends User { this.psp = psp; } - public void changeBatch(String batchName) { + public Student() { + } + + void changeBatch(String batchName) { this.batchName = batchName; } + @Override + public void printInfo() { + System.out.println("\nStudent: " + getName() + " " + getBatchName()); + } } \ No newline at end of file diff --git a/oop/code/oop/src/main/java/com/scaler/lld/scaler/User.java b/oop/code/oop/src/main/java/com/scaler/lld/scaler/User.java index ce6e403..c81c45b 100644 --- a/oop/code/oop/src/main/java/com/scaler/lld/scaler/User.java +++ b/oop/code/oop/src/main/java/com/scaler/lld/scaler/User.java @@ -7,8 +7,8 @@ import lombok.Setter; @Getter @Setter -@NoArgsConstructor @AllArgsConstructor +@NoArgsConstructor public class User { private String name; private String email; @@ -16,4 +16,26 @@ public class User { public void changeEmail(String email) { this.email = email; } + + // Method overloading + // Method signature: + // 1. Method name + // 2. # of args + // 3. Data type of args + + public void printInfo() { + } + + public void printInfo(String title) { + System.out.println(" \n User: " + title + " " + this.getName()); + } } + +// Interfaces +// Class - Blueprint +// Interface - Blue print of behaviour +// Database db = new MySqlDB(); +// Interfaces - define methods with an impl. +// Abstract - mixture of interface and class +// implemented methods + not-implemented +// Abstract classes - state