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 .
  1. Since, type checking is done at compile time so interpreter or at runtime program can run at full speed, without resolving types.
  2. It minimizes the exceptions at runtime because of type checking.
Python example :-name = "Shivam"; //variable name is linked to String typename =5;//Here, as we are assigning a integer value, it will become 
//int .
  1. compiler or interpreter implicitly performs certain kinds of type conversions. So applications can be built in a rapid manner.
public interface Employee{}
public class Developer{}
public class Manager extends Developer implements Employee{}
  1. Manager IS-A Developer
  2. Manager IS-A Employee
Manager managerObj = new Manager();
Developer developer = managerObj;
Employee employee = managerObj;
Object object = managerObj;
  1. Compile time Polymorphism
  2. Runtime Polymorphism
//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);
}
}
  1. If we will remove “public void PrintSalary(int salary)” then it ‘ll call “public void PrintSalary(long salary)” method.
  2. If we will remove “public void PrintSalary(long salary)” method then it’ll call “public void PrintSalary(double salary)” method.
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
  1. Method signature always should be the same.
  2. To achieve method override there must be IS-A relationship(inheritance).
  3. Method of public or Protected abstract class can be overridden.
  4. If the Access modifier of parent class method is Public, then the overriding method in child class can’t have private, protected and default access modifier, because these modifiers are more restrictive than public and compiler will give compilation error “Cannot reduce the visibility of the inherited method from Employee”.
  5. If a class is extending an abstract class or an interface then it has to override all the abstract methods of abstract class and Interfaces.
  6. Super keyword is used to call the parent class constructor or method from child class constructor or method.
  7. Private, final method can’t be overridden.
  8. Static method can’t be overridden because it is bound with class and we can only override instance methods, but it can be re-defined.
  9. We can’t override a constructor.
  10. We can’t able to override main() method because it is static.

--

--

--

Technical Consultant | Passionate about exploring new Technology | Cyber Security Enthusiast | Technical Blogger | Problem Solver

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My response to people who think open source is the answer to everything

How to create a live dashboard using JotForm Report Builder

About token(Saddle (SDL))

Why your Business Needs HyperNet Genset Monitoring Solution

Manjaro #1: Dual-boot Windows 10 / ArchLinux

Finding High Dividend Stocks with Python

How to migrate your domain to Route53 and connect it to an EC2 Instance?

Project KFC Clone

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
Kumar Shivam

Kumar Shivam

Technical Consultant | Passionate about exploring new Technology | Cyber Security Enthusiast | Technical Blogger | Problem Solver

More from Medium

Chapter 4: Methods use Instance variables

Chapter 05- Extra-Strength Methods

INTRODUCTION TO HTTP

Let’s Loop through Arrays