Polymorphism in Java - ProgrammerTech
Language:

Search

Polymorphism in Java

  • Share this:
Polymorphism in Java

Polymorphism in Java

Polymorphism is the available methods for accessing class functions and variables. In the previous lessons, we learned a form of accessing class variables and functions. And it is to create an object of the same name as the class so that you can access the variables and functions of the class. And we learned another form, which is that we can access the variables and functions of the parent class after inheritance by means of. Creating an object from the child class to access the variables and functions of the parent class, and this is another form.

How does an object derived from the father class access the variables and functions of the son class?

The father class can access the functions and variables of the son in only one case, which is the application of the Override principle.

Before we talk about the Override principle, what do we benefit from this figure and why do we make the object that is derived from the father class access the variables and functions of the son class? Many resort to this override principle so that the derived object can access the children's classes in order to shorten the objects. I mean, instead of when we have three classes and each class is inherited from one class, instead of doing. By creating three objects, using the Override principle, we can create one object from the parent class.

We can access the variables and functions of the son, but provided that these variables are applied. Override principle so that a parent-derived object can access these functions and variables. We said that we take advantage of this method to reduce the number of objects because whenever we create an object, we reserve a place in memory. And the more objects, the slower the program, which is why we resort to mitigating this problem through Override.

 

Override concept in Java

It is to have a function defined in more than one class with the same name and the same structure, or to have a variable. More than one class is defined with the same name and type, but provided that it is defined in the parent class. Here we say that we have applied the Override principle, which is to create a function in the parent class and create it with the same. The name and the same structure in the rest of the son classes so that the father can access these variables and functions. We also benefit from Override when we have two functions with the same name and the same structure applied. Override principle, which means that we reserve one place from the memory and do not reserve more than one place.

The goal of object-oriented programming (oop) is to reduce the amount of memory and speed up the program's work, and this lesson is one of the most important lessons that apply this principle. In this lesson, we will learn on a simple example how the father class accesses the functions and variables of the son class. And we will deal with an extensive topic on how it is beneficial for the father class to reach the son class.

Example 1

package poly;
class A
{
    int x;
    void print ()
    {
        System.out.println("A");
    }
}
class B extends A
{
    int x;
    void print ()
    {
        System.out.println("A");
    }
}
public class Poly{
     public static void main(String []args){
         A ob=new A ();
         ob.x=9;
         ob.print();
         A ob1=new B ();
         ob1.print();
         ob1.x=8;
     }
}

We had a class named Class A, which will be the father and has a variable named x, and we have another class that inherits. It is from class A and has a variant with the same name as in class. The father's name is int x. Here we apply the principle of Override and in this way the father class will be able to reach the son class. In the main program, we created an object from the parent class named ob, and we said that it is equal to new A. We note that we took a copy of the parent class. And we said ob.x=9 and the question here is what is x in which we stored the value 9? Is it dependent on the father or the son? Because the same name is found in the father and son.

And the answer is as long as we say that the derived object is equal to new A, then this means that x is meant for the parent class A. And we derive the object ob1 and then we say that it equals new B. Now here we referred to the son class and the object was derived from the father, but it refers to the son. Then we said ob1.x=8 here will be x of class B because the object ob1 refers to the son class and in this way we were able, by means of the father class, to access the variables and functions of the son class.

When we run the above code, we will get the following result:

A
A

Example 2

package poly;
class A
{
    int x;
    void print()
    {
        System.out.println("I am parrent")
    }
}
class B extends A
{
    int x;
    int y; 
    void print()
    {
        System.out.println("I am child")
    }
}
public class Poly{
     public static void main(String []args){
        A ob1=new A();
        ob1.x=10;  
        ob1.print();
        A ob2=new B ();
        ob2.x=7;
        ob2.print(); 
     }
}

We have a father's class named A and it has variables and a function, and a son named class B inherited from him, but in this son he was. It has a variable more than its father, which is int y, and this variable will not apply the Override principle because there is no such variable in the father. And then we entered the main program and we derived an object from the parent class, which is ob1, then we said that ob1.x=10 and this is the parent class. Then we print it via print and then we set a value for the class son ob2.x=7 and print it.

Example 3 interest profitability

package branch;
class Bank
{
    double profet(double pay)
    {
        return pay*0.4;
    }
}
class Branch1 extends Bank
{
    double profet(double pay)
    {
        return pay*0.3;
    }
}
class Branch2 extends Bank
{
    double profet(double pay)
    {
        return pay*0.2;
    }
}
public class Branch{
     public static void main(String []args){
         Bank A=new Bank();
         System.out.println(A.profet(1000);
         A=new Branch1();
         System.out.println(A.profet(1000);
         A=new Branch2();
         System.out.println(A.profet(1000);
     }
}

We considered that we have a bank, and we created a class-app in the name of Bank, where we will consider it the main branch, and in it, a double function calculates the value of the amount paid and returns the amount of profit 0.4. And there was another branch of this bank, which is the son class Branch1 inherited from the father class and the amount of profit here is 0.3. And there was another branch, which was the Class Branch2, which was inherited from the father's class, and the amount of profit in it was 0.2. Then we entered the main program, we derived an object from the father's class and printed the interest account from the father's class 1000, and the same thing was for the two sons.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com