Java polymorphism and object assignment of classes that implement interfaces to methods referenced by interfaces of recommends

  • 2020-06-12 09:15:49
  • OfStack

The flexibility of interfaces lies in "specifying what a class must do, regardless of how you do it."

We can define a reference variable of the interface type to refer to an instance of a class that implements the interface. When this reference calls a method, it will determine which method to call based on the instance of the class it actually refers to, similar to the way the superclass object reference above accesses subclass objects.


// Defines the interface InterA
interface InterA
{
 void fun();
}
// Implementing an interface InterA The class of B
class B implements InterA
{
 public void fun()
 {
  System.out.println( " This is B " );
 }
}

// Implementing an interface InterA The class of C
class C implements InterA
{
 public void fun()
 {
  System.out.println( " This is C " );
 }
}

class Test
{
 public static void main(String[] args)
 {
  InterA a;
  a= new B();
  a.fun();
  a = new C();
  a.fun();
 }
}

The output result is:


This is B
This is C

In the above example, class B and class C are two classes that implement the interface InterA, respectively implementing the interface method fun(). By assigning instances of class B and class C to the interface reference a, the dynamic binding of the method at runtime is realized, and the "1 interface, multiple methods" is fully utilized to demonstrate the dynamic polymorphism of Java.

One thing to note is that when Java invokes a method on an object of its implementation class with an interface variable, the method must already be declared in the interface, and the type and parameters of the implementation method in the implementation class of the interface must exactly match those defined in the interface.

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

extension

Java runtime polymorphism: Inheritance and implementation of interfaces

Java is an object-oriented language, while runtime polymorphism is one of the most powerful mechanisms for code reuse in object-oriented programming, and the concept of dynamics can also be described as "one interface, many methods." The basis of Java's implementation of runtime polymorphism is dynamic method scheduling, which is a mechanism for calling overloaded methods at runtime rather than at compile time. The implementation of java runtime polymorphism is discussed in terms of inheritance and interface implementation.

1. It is implemented by reference variables to subclass objects through inheritance

For example:


// Define the superclass superA 
class superA {
  int i = 100;

  void fun() {
    System.out.println( " This is superA " );
  }
}

// define superA A subclass of subB 
class subB extends superA {
  int m = 1;

  void fun() {
    System.out.println( " This is subB " );
  }
}

// define superA A subclass of subC 
class subC extends superA {
  int n = 1;

  void fun() {
    System.out.println( " This is subC " );
  }
}

class Test {
  public static void main(String[] args) {
    superA a;
    subB b = new subB();
    subC c = new subC();
    a = b;
    a.fun();
    (1)
    a = c;
    a.fun();
    (2)
  }
}  

The operation result is:


This is subB

This is subC

In the above code, subB and subC are subclasses of the superclass superA. We declare three reference variables a, b and c in the class Test to achieve dynamic method invocation by assigning subclass object reference to the superclass object reference variable. One might ask: "Why (1) and (2) do not output: This is superA". This mechanism of java follows a principle: when a superclass object references a variable that references a subclass object, the type of the referenced object, not the type of the referenced variable, determines whose member method is called, but the called method must be defined in the superclass, that is, the method overridden by the subclass.

So don't be confused by (1) and (2) above, although written as ES56en.fun (), since a in (1) is assigned by b and points to an instance of subclass subB, the fun() called by (1) is actually the member method fun() of subclass subB, which overrides the member method fun() of superclass superA. Likewise (2) calls the member method fun() of the subclass subC.

In addition, if the subclass inherits a superclass that is an abstract class, the abstract class cannot be instantiated with the new operator, but an object reference of the abstract class can be created to point to the subclass object for runtime polymorphism. The concrete implementation method is the same as above example.

However, a subclass of an abstract class must override all of the abstract methods that implement the superclass, otherwise the subclass must be decorated with the abstract modifier and, of course, cannot be instantiated.

2. The interface type variable refers to the object of the class that implements the interface

The flexibility of interfaces lies in "specifying what a class must do, regardless of how you do it." We can define a reference variable of the interface type to refer to an instance of a class that implements the interface. When this reference calls a method, it will determine which method to call based on the instance of the class it actually refers to, similar to the way the superclass object reference above accesses subclass objects.

For example:


// Defines the interface InterA 
interface InterA
{
  void fun();
}
// Implementing an interface InterA The class of B 
class B implements InterA
{
  public void fun()
  {
    System.out.println( " This is B " );
  }
}
// Implementing an interface InterA The class of C 
class C implements InterA
{
  public void fun()
  {
    System.out.println( " This is C " );
  }
}
class Test
{
  public static void main(String[] args)
  {
    InterA a;
    a= new B();
    a.fun();
    a = new C();
    a.fun();
  }
} 

The output result is:


This is B

This is C

In the above example, class B and class C are two classes that implement the interface InterA, respectively implementing the interface method fun(). By assigning instances of class B and class C to the interface reference a, the dynamic binding of the method at run time is realized. The dynamic polymorphism of Java is demonstrated by making full use of "one interface, multiple methods".

One thing to note is that when Java invokes a method on an object of its implementation class using an interface variable, the method must already be declared in the interface, and the type and parameters of the implementation method in the implementation class of the interface must exactly match those defined in the interface.

Conclusion: The above is the implementation method of java runtime polymorphism, which can be flexibly applied in the programming process, but runtime polymorphism is not recommended in the code with high performance requirements. After all, the dynamic runtime method invocation of Java is more expensive than the common method invocation.

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

Java static methods do not have a detailed explanation of polymorphism

Dynamic binding makes it possible to program for a base class by making a reference to the correct subclass object.

However, dynamic binding fails in two cases.

Base class methods are modified by private or final

This is easy to understand because private shows that the method is invisible to the subclass, and that the subclass writing another method with the same name is not a copy of the superclass method (Override), but a new method, so there is no polymorphism problem. The same is true for final, because methods are also not overwritten.

2. The method is modified by static

The code is shown below.


class Base {
 public static void staticMethod() {
  System.out.println("Base staticMehtod");
 }

 public void dynamicMehtod() {
  System.out.println("Base dynamicMehtod");
 }
}

class Sub extends Base {
 public static void staticMethod() {
  System.out.println("Sub staticMehtod");
 }

 public void dynamicMehtod() {
  System.out.println("Sub dynamicMehtod");
 }
}

public class TJ4 {
 public static void main(String args[]) {
  Base c = new Sub();
  c.staticMethod();
  c.dynamicMehtod();
 }
}

The output results are as follows:


 Base staticMehtod
 Sub dynamicMehtod

The output is not "Sub staticMehtod" as expected. Because static methods are associated with a class rather than an object, c.staticMethod (); Equivalent Car. staticMethod (); So try not to use instance variables to call static methods to avoid confusion.


Related articles: