Songboy Java polymorphism and genericity

Posted by jvquach on Wed, 09 Feb 2022 11:27:30 +0100

polymorphic

Three characteristics of object-oriented: inheritance, encapsulation and polymorphism

definition
Polymorphism: refers to the same behavior, which has multiple different manifestations for different objects.
Polymorphism in program: it refers to that the same method has different implementations for different objects

Preconditions [ key ]

  • Inherit or implement [one of two]
  • Parent class reference points to child class object \ interface reference points to implementation class object [format embodiment]
  • Rewriting of methods [meaning embodiment: no rewriting, meaningless]

Embodiment of polymorphism: the reference of the parent class points to the object of its subclass:

Parent type variable name = new Subclass objects;
Variable name.Method name();

Parent class type: refers to the parent class type inherited by the child class object or the implemented parent interface type.
Advantages and disadvantages of polymorphism
benefit

It improves the scalability of the code
malpractice

In the case of polymorphism, you can only call the common content of the parent class, not the unique content of the child class.
You cannot access methods or member variables unique to subclasses, because the characteristic of polymorphic member access is that the compilation depends on the parent class

Reference type conversion

Upward transformation
  • The process of upward conversion from subclass type to parent type. This process is the default.
 Aniaml anl = new Cat(); 
Downward transformation
  • The downward conversion process from parent type to child type is mandatory.
 Aniaml anl = new Cat();  
 Cat c = (Cat)anl;//Downward transformation

generic paradigm

Why use generics?
When a collection does not use generics, any type can be saved when it is saved (Object). But when you take it, you're confused. Taking it out is nothing.
Using generics directly controls the type at compile time and can only store the data defined by generics
Generic type: it represents an unknown data type when defined, and its specific data type is determined when used.
The role of generics is to determine the specific type of an unknown type when creating an Object. When no generic type is specified, the default type is Object type.

Define and use classes with generics

Define format:

Modifier  class Class name<Variables representing generics> {  }
Variables representing generics: Can be any letter, for example: T,E...

Generic types are not specific types when they are defined, but specific types when they are used. Determine the specific data type of the generic when using it.

// Generic class
class ArrayList<E>{ 
	// Parameter is generic
    public boolean add(E e){ }
	// The return value is generic
    public E get(int index){ }
   	....
}

Determine the specific type of generic

Determine generics when creating objects
For example, ArrayList < string > List = new ArrayList < string > ();

At this time, the value of variable E is String type, so our type can be understood as

class ArrayList<String>{ 
     public boolean add(String e){ }

     public String get(int index){  }
     ...
}

Define a class that contains generics

public class MyList<E>{
    E e;
    public E func(E e){
        return e;
    }

    public static void main(String[] args) {
        MyList<String> myList = new MyList<>();
        // Attribute assignment
        myList.e = "ha-ha";
        System.out.println(myList.e );
        System.out.println(myList.func("ha-ha"));
        MyList<Integer> myList2 = new MyList<>();
        // Attribute assignment
        myList2.e = 666;
        System.out.println(myList2.e );
        System.out.println(myList2.func(888));
    }
}

Define and use methods with generics

format

Modifier  <Variables representing generics> Return value type method name(parameter){  }
  • When a method is called, the type of the generic type is determined
public class Test {
    // Modifier < variable representing generics > return value type method name (parameter) {}

    public static <T> T func1(T t){
        return t;
    }

    public static void main(String[] args) {
       Integer i =  func1(100);
        System.out.println(i);
        String s =  func1("2222");
        System.out.println(s);

    }
}

Define interfaces with generics

Modifier  interface Interface name<Variables representing generics> {  }
public interface IA <E>{
    public abstract void m1(E e);
    public default E m2(E e){
        return e;
    }
}
public class ImplA<E> implements IA<E>{

    @Override
    public void m1(E e) {

    }

    @Override
    public E m2(E e) {
        return e;
    }

    public static void main(String[] args) {
        // Determine generics when using
        ImplA<String> implA = new ImplA<>();
        implA.m1("ha-ha");
        System.out.println(implA.m2("Hee hee"));
    }
}

Topics: Java Back-end