mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 22:59:53 +00:00
Adds code for polymorphism.
This commit is contained in:
parent
1974241585
commit
eb62012ec2
@ -1,27 +1,58 @@
|
|||||||
package com.scaler.lld;
|
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!
|
* Hello world!
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
public static void main(String[] args) {
|
||||||
public static void main( String[] args )
|
User student = new Student("student", "student@scaler.in", "batch", 100);
|
||||||
{
|
student.printInfo(); // inheritance
|
||||||
// Create accounts for Alice and Bob
|
}
|
||||||
OopBankAccount accountAlice = new OopBankAccount(1, 1000, "Alice");
|
|
||||||
OopBankAccount accountBob = new OopBankAccount(2, 2000, "Bob");
|
|
||||||
|
|
||||||
System.out.println("\nAlice's balance: " + accountAlice.getBalance());
|
public static void resetEmail(List<User> users) {
|
||||||
System.out.println("Bob's balance: " + accountBob.getBalance());
|
for (User user : users) {
|
||||||
|
user.changeEmail("");
|
||||||
|
|
||||||
System.out.println("\nTransferring $100 from Alice to Bob");
|
if (user instanceof Student) {
|
||||||
// Transfer $100 from Alice to Bob
|
Student student = (Student) user;
|
||||||
accountAlice.transfer(accountBob, 100);
|
|
||||||
|
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"}
|
25
oop/code/oop/src/main/java/com/scaler/lld/scaler/Mentor.java
Normal file
25
oop/code/oop/src/main/java/com/scaler/lld/scaler/Mentor.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,12 +1,10 @@
|
|||||||
package com.scaler.lld.scaler;
|
package com.scaler.lld.scaler;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@NoArgsConstructor
|
|
||||||
public class Student extends User {
|
public class Student extends User {
|
||||||
|
|
||||||
private String batchName;
|
private String batchName;
|
||||||
@ -25,9 +23,16 @@ public class Student extends User {
|
|||||||
this.psp = psp;
|
this.psp = psp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeBatch(String batchName) {
|
public Student() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void changeBatch(String batchName) {
|
||||||
this.batchName = batchName;
|
this.batchName = batchName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void printInfo() {
|
||||||
|
System.out.println("\nStudent: " + getName() + " " + getBatchName());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -7,8 +7,8 @@ import lombok.Setter;
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
public class User {
|
public class User {
|
||||||
private String name;
|
private String name;
|
||||||
private String email;
|
private String email;
|
||||||
@ -16,4 +16,26 @@ public class User {
|
|||||||
public void changeEmail(String email) {
|
public void changeEmail(String email) {
|
||||||
this.email = 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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user