Adds code for polymorphism.

This commit is contained in:
Tanmay 2022-09-07 13:19:44 +01:00
parent 1974241585
commit eb62012ec2
4 changed files with 102 additions and 19 deletions

View File

@ -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<User> 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"}

View File

@ -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<Student> mentees = new ArrayList<>();
private String company;
public Mentor(String name, String email, List<Student> mentees, String company) {
super(name, email);
this.mentees = mentees;
this.company = company;
}
}

View File

@ -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());
}
}

View File

@ -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