Thursday, January 25, 2024

POLYMORPHISM AND OOPS UNSERSTANDING

 

<

POLYMORPHISM IN JAVA

Before going to the polymorphism we want to know what is oops(Object Oriented Programming)

OOPS

OOPS Which stands for object oriented programming .Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic.(OOP) is a fundamental programming paradigm used by nearly every developer at some point in their career. OOP is the most popular programming paradigm used for software development.

Why is called an oops?

In dictionary meaning of an object is "an entity that exists in the real world", and oriented means "interested in a particular kind of thing or entity". In basic terms, OOP is a programming pattern that is built around objects or entities, so it's called object-oriented programming.

Father of oops concept



Object-Oriented Programming” (OOP) was coined by Alan Kay circa 1966 or 1967 while he was at grad school

OOPS have seven rules of instructions provide. Each one have some Specific functionable one. 


They are four major concept are in oops:



This blog we discuss about the one of the pillar of  OOPS - polymorphism 


POLYMORPHISM

Polymorphism is the ability of a variable, object or function to take an multiple forms

   


 EASY TO UNDERSTAND BY THIS ONE 


multiple form:
Which means in the program two or more method have a same name but differ in the parameter passing ( different no of parameter, without parameter and different types of parameter ).The same no of methods have a same name is called a Method overloading.  In single line we will said about the multiple form "The same name of the functions or methods performs different operation.

Types of polymorphism

  • Compile time polymorphism
    1. Function overloading
    2. Operator overloading
  • Run time polymorphism
    1. Virtual Function

COMPILE TIME POLYMORPHISM

The term method overloading allows us to have more than one method with the same name. Since this process is executed during compile time, that's why it is known as Compile-Time Polymorphism. Compile-time is the time period when a program code is translated into a low-level code or machine code, either by a compiler or an interpreter. Compile-time is the period of time from the beginning to the end of the process.

RUN TIME POLYMORPHISM

Run-time polymorphism is also known as dynamic or late binding polymorphism. The method functionality is decided dynamically at run time based on the object. It is also called “Late Binding” as the process of binding the method with the object occurs late after compilation.

________________________________________________________________________
Method overloading Method overriding
Same method name used multiple time with different no of parameter , different types of parameter the same method using more time is called method overloading


   Function overriding is called dynamic                      polymorphism. same function call will perform different types of operation at run time.


Method overloading & compile time Polymorphism

1.Different no of parameter

package methodol;


public class MethodOverLoading {



//This program to demonstrate the working of method

//overloading by changing the number of parameters

// 1 parameter

void meth1(int num1)

{

System.out.println("number 1 = " + num1);

}


// 2 parameter

void meth1(int num1, int num2)

{

System.out.println("number 1 = " + num1

+ " number 2 = " + num2);

}


public static void main(String[] args)

{

MethodOverLoading obj = new MethodOverLoading();

// 1st show function

obj.meth1(3);

// 2nd show function

obj.meth1(4, 5);

}

}

OUTPUT:

number 1 = 3

number 1 = 4 number 2 = 5

example, we implement method overloading by changing several parameters. We have created two methods, meth1(int num1 ) and meth1(int num1, int num2 ). In the meth1(int num1) method display, one number and the void meth1(int num1, int num2 ) display two numbers


2.Different types of datatypes

//Java program to demonstrate the working of method

//overloading by changing the Datatype of parameter


public class MethodOverLoading {


// arguments of this function are of integer type

static void meth1(int a, int b)

{

System.out.println("This is integer function "+a+" "+b);

}


// argument of this function are of float type

static void meth1(double a, double b)

{

System.out.println("This is double function "+a+" "+b);

}


public static void main(String[] args)

{

// 1st show function

meth1(1, 2);

// 2nd show function

meth1(1.2, 2.4);

}

}

OUPUT:

This is integer function 1 2

This is double function 1.2 2.4

example, we changed the data type of the parameters of both functions. In the first meth1() function datatype of the parameter is int. After giving integer type input, the output will be ‘ This is integer function.’ In the second meth1() function datatype of a parameter is double. After giving double type input, the output would be ‘This is double function.’

Different types of data types using char

//Java program to demonstrate the working of method

//overloading by changing the sequence of parameters


public class MethodOverLoading {


// arguments of this function are of int and char type

static void meth1(int a, char ch)

{

System.out.println("integer : " + a

+ " and character : " + ch);

}


// argument of this function are of char and int type

static void meth1(char n, int a)

{

System.out.println("character : " + n

+ " and integer : " + a);

}


public static void main(String[] args)

{

// 1st show function

meth1(6, 'G');


// 2nd show function

meth1('s', 7);

}

}

OUTPUT:

integer : 6 and character : G

character : s and integer : 7

In the above example, in the first show, function parameters are int and char, and in the second shoe, function parameters are char, and int. changed the sequence of data type.


Method over riding & run time polymorphism

RULES OF METHOD OVERRING

  1. The method must have the same name as in the parent class.
  2. The method must have the same parameter as in the parent class.
  3. There must be an IS-A relationship (inheritance).

1.Without override

package methodoride;


class vechi {



void meth1()

{

System.out.println("Vehicle is running");

}

}

//Creating a child class

class MethodOverRide extends vechi{

public static void main(String args[]){

//creating an instance of child class

MethodOverRide obj = new MethodOverRide();

//calling the method with child class instance

obj.meth1();

}

}

OUTPUT:

Vehicle is running

I have to provide a specific implementation of run() method in subclass that is why we use method overriding.

2.Example of Java Method Overriding 

class Vechi{

//defining a method

void run(){

System.out.println("Vehicle is running");

}

}

//Creating a child class

class MethodOverRide extends Vechi{

//defining the same method as in the parent class

void run(){

System.out.println("Bike is running safely");

}

public static void main(String args[])

{

MethodOverRide obj = new MethodOverRide();//creating object

obj.run();//calling method

}

}


OUTPUT:

Bike is running safely


3.way one to method override:

class Sharemar{

int getRateOfInterest(){

return 0;

}

}

//Creating child classes.


class Itc extends Sharemar{

int getRateOfInterest(){

return 8;

}

}

class Ipo extends Sharemar{

int getRateOfInterest(){

return 7;

}

}

class Index extends Sharemar{

int getRateOfInterest(){

return 9;

}

}

//Test class to create objects and call the methods

class MethodOverRide{

public static void main(String args[]){

Itc s=new Itc();

Ipo i=new Ipo();

Index a=new Index();

System.out.println("Itc Rate of Interest: "+s.getRateOfInterest());

System.out.println("Ipo Rate of Interest: "+i.getRateOfInterest());

System.out.println("Index Rate of Interest: "+a.getRateOfInterest());

}

}

OUTPUT:

Itc Rate of Interest: 8

Ipo Rate of Interest: 7

Index Rate of Interest: 9

-----------------------------------------------------------------------------------

Note:📔🖉

a static method cannot be overridden. It can be proved by runtime polymorphism

    Can we override main method ?

                             No, because the main is a static method.

No comments:

Post a Comment

DDL command in sql

DATA DEFINITION LANGUAGE DBMS language database language are used to read,update and store data in a database.There are several suc...