The characteristics and understanding of java polymorphism

Keywords: Programming

Polymorphism: Different objects use the same behavior to produce different behavior outcomes.

The manifestation of polymorphism:
1. Rewriting between subclasses and parent classes.
2. Overload within the class.
3. Implementation of the same interface by different classes.

Advantages of polymorphism:
1. It conforms to the principle of inversion of dependency in design pattern. For interface programming, it relies on abstraction rather than concrete, which reduces the coupling degree.
2. Enhance the extensibility of the code, the new behavior will not affect the original code.
3. It improves the flexibility of the code and can replace the objects that produce the same behavior flexibly.

.
.
.
.
Rewrite:
Occurs between subclasses and parent classes
Conditions: 1. There is an inheritance relationship between subclasses and parent classes. 2. Subclasses override parent classes. 3. References to parent classes point to objects of subclasses.

Example:

Parent class

public class ParentTest {

    public void getFamily() {
        Log.d(TAG, "grandpa");
    }
}

Subclass A

public class TestA extends ParentTest {
    private final String TAG="TestA";

    public void getFamily() {
        Log.d(TAG, "Dad");
    }
}

Subclass B

public class TestB extends ParentTest {
    private final String TAG="TestB";
    @Override
    public void getFamily() {
        Log.d(TAG, "Mom");
    }
}

main method call

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//Calling methods of parent classes 
	   new ParentTest().getFamily();
//Call subclass A method
        ParentTest familyMember=new TestA();
        familyMember.getFamily();
//Call subclass B method
         familyMember=new TestB();
        familyMember.getFamily();

    }



}

Print results:
Content Values: Grandpa
TestA: Dad
TestB: Mom

The above code reflects the polymorphism between subclasses and parent classes, and it can be found that TestB and TestA can flexibly replace each other, and the same behavior returns different results. This also verifies the flexibility of polymorphism.

.
.
.

Overload:
Means that within a class, the method name is the same, but the number and type of its transfer parameters are different and the return types are different.

Example:

public class Test {

    public String getType(int type){
        return "What comes in is int";
    }

    public String getType(String type){
        return "What comes in is String";
    }

    public String getType(boolean type){
        return "What comes in is boolean";
    }
}

public class MainActivity extends AppCompatActivity {

    private final String TAG="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Test test=new Test();
        Log.d(TAG, test.getType(1));
        Log.d(TAG, test.getType("a"));
        Log.d(TAG, test.getType(true));
    }



}

Result

Interface implementation: Different classes implement the same interface, calling the same behavior of different objects through the reference of the interface.

Example:

Interface

public interface ParentTest {

    void getTest();
}

Interface Implementation Class TestA

public class TestA implements ParentTest {
    
    private final String TAG="TestA";
    
    @Override
    public void getTest() {
        Log.d(TAG, "TestA");
    }
}

Interface Implementation Class TestB

public class TestB implements ParentTest {
    private final String TAG="TestB";
    @Override
    public void getTest() {
        Log.d(TAG, "TestB");
    }
}

public class MainActivity extends AppCompatActivity {

    private final String TAG="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
	//Call using interface references
        ParentTest test=new TestA();
         test.getTest();

         test=new TestB();
        test.getTest();

    }



}

Result

Posted by ginginca on Wed, 21 Aug 2019 23:41:33 -0700