Important Concepts of Object-Oriented system (Part IV)

String name = "Shivam"; //variable name is statically typed to 
//String
name = 5;// it will give us compilation error because as name is of
//String type so we can't assign int value in it .
Python example :-name = "Shivam"; //variable name is linked to String typename =5;//Here, as we are assigning a integer value, it will become 
//int .
public interface Employee{}
public class Developer{}
public class Manager extends Developer implements Employee{}
Manager managerObj = new Manager();
Developer developer = managerObj;
Employee employee = managerObj;
Object object = managerObj;
//Example of Method Overloading
//Employee.java
package com.thirstybrain.app.polymorphism;public class Employee {public void getEmployeeDetail(String name){
System.out.println("The name of Employee is "+ name);
}
public void getEmployeeDetail(int id){
System.out.println("The id of Employee is "+ id);
}
//Return type of methods don't consider in Method overloading
//after JDK 1.5
}
//Program.javapackage com.thirstybrain.app.polymorphism;public class Program {
public static void main(String[] args) {
Employee emp = new Employee();
emp.getEmployeeDetail(1);
// Output : The id of Employee is 1
emp.getEmployeeDetail("Shivam");
//Output : The name of Employee is Shivam
}
}
//Employee.javapackage com.thirstybrain.app.polymorphism;public class Employee {public void PrintSalary(int salary){
System.out.println("salary from PrintSalary(int salary) method is "+salary);
}
public void PrintSalary(long salary{
System.out.println("salary from PrintSalary(Long salary) method is "+salary);
}public void PrintSalary(double salary){ System.out.println
("salary from PrintSalary(Double salary) method is "+salary);
}public void PrintSalary(Integer salary){
System.out.println("salary from PrintSalary(Integer salary) method is "+salary);
}
}
//Program.javapackage com.thirstybrain.app.polymorphism;public class Program {public static void main(String[] args) {
Employee emp = new Employee();
emp.PrintSalary(1000);
}
}
Preference order
emp.PrintSalary(null);
//Program.javapackage com.thirstybrain.app.polymorphism;public class Program {public static void main(String[] args) {
String firstName = "Kumar";
String lastName = "Shivam";
System.out.println(firstName+" "+lastName);
//Output: Kumar Shivam
//Here, i'm using "+" operator to do concatenation
int num1 = 10 ;
int sum= num1 + 10 ;
System.out.println(sum);
//Output: 20
//Here, I'm using "+" operator to do Sum
}
}
//Employee.javapackage com.thirstybrain.app.single;public class Employee {public void getEmployeeDetail(){
System.out.println("Invoked getDetail from Employee Class ");
}
}
//FulltimeEmployee.java
package com.thirstybrain.app.single;
public class FulltimeEmployee extends Employee {@Override
public void getEmployeeDetail(){
System.out.println("Invoked getDetail from FulltimeEmployee Class ");
}
}
//Program.java
package com.thirstybrain.app.single;
public class Program {public static void main(String[] args) {
FulltimeEmployee fulltimeEmployeeObj=new FulltimeEmployee();
fulltimeEmployeeObj.getEmployeeDetail();
//Output : Invoked getDetail from FulltimeEmployee Class
Employee emp=new Employee();
emp.getEmployeeDetail();
//Output : Invoked getDetail from Employee Class
Employee emp1=new FulltimeEmployee();
emp1.getEmployeeDetail();
//Output : Invoked getDetail from FulltimeEmployee Class

//Since all the method in java are virtual by default .
// Reference is of Employee(i.e. Parent) class
//but instance if of FulltimeEmployee(i.e. Child) class
//.That’s why it will call getEmployeeDetail() method of
//FulltimeEmployee(i.e. Child)

}
}
UseCase: Loan method implementation for multiple lenders
bytecode

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store