Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the inheritance and implementation of Java generics

Shulou Source: shulou.com Published: 2022-06-02 13:08:21 09月28日 Update

This article mainly explains "what is the inheritance and implementation of Java generics". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the inheritance and implementation of Java generics"?

A little bit of eye contact

Once generic classes and generic interfaces are defined, they can be inherited and implemented.

Inheritance of two generic classes

1 code

Class A {E t;} public class Bextends A {public static void main (String [] args) {System.out.println ("B test");}}

2 run

B test

3 description

When subclass B is defined, if An is omitted, the T of B automatically becomes Object. It is recommended to add it when defining to retain the type parameters of the parent class. Class B can also add a new generic T1.

Implementation of three generic interfaces

1 code

Interface IT {public E dis ();} public class testIT implements IT {E e; public testIT (E) {this.e = e;} public E dis () {return e;} public static void main (String [] args) {testIT tt = new testIT ("test"); System.out.println (tt.dis ());}}

2 run

Test

3 description

The implementation class testIT cannot be omitted.

You must implement all the methods in the IT interface, just like the normal implementation class.

Thank you for reading, the above is the content of "what is the inheritance and implementation of Java generics". After the study of this article, I believe you have a deeper understanding of what is the inheritance and implementation of Java generics, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Methods interfaces learning code content runs ordinary parameters subclasses that is suggestions ideas situations articles times more knowledge knowledge points articles types Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL NVidia macOS Redmi