Important Concepts of Object-Oriented system (Part IV)

String name = "Shivam"; //variable name is statically typed to 
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
package;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;public class Program {
public static void main(String[] args) {
Employee emp = new Employee();
// Output : The id of Employee is 1
//Output : The name of Employee is Shivam
//Employee.javapackage;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;public class Program {public static void main(String[] args) {
Employee emp = new Employee();
  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
//Program.javapackage;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 ;
//Output: 20
//Here, I'm using "+" operator to do Sum
//Employee.javapackage;public class Employee {public void getEmployeeDetail(){
System.out.println("Invoked getDetail from Employee Class ");
public class FulltimeEmployee extends Employee {@Override
public void getEmployeeDetail(){
System.out.println("Invoked getDetail from FulltimeEmployee Class ");
public class Program {public static void main(String[] args) {
FulltimeEmployee fulltimeEmployeeObj=new FulltimeEmployee();
//Output : Invoked getDetail from FulltimeEmployee Class
Employee emp=new Employee();
//Output : Invoked getDetail from Employee Class
Employee emp1=new FulltimeEmployee();
//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
  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

Load Balancing And Rate Limiting for Dummies (Part 1)

Build a Decentralized Todo App Using Vue.js & Rust (WebAssembly)

[AGIP13] Should Aavegotchi introduce an Aging Boost?

Listen to Articles, News, Notes or Any Text Online

Visualizing LoRa packets with an AD9364 based SDR

Securing the Pact Broker with Nginx & LetsEncrypt

Rails: Menu Mixer

Why Django is the best Web Framework

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

Some important programs in c using functions and the programs were commented below:

The importance of learning the basics: Classes, namespaces, and Modifiers