Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of Java generic inheritance?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the principle of Java generic inheritance". In daily operation, I believe that many people have doubts about what the principle of Java generic inheritance is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what is the principle of Java generic inheritance?" Next, please follow the editor to study!

A little bit of eye contact

After you create an interface and parent class with a generic declaration, you can create an implementation class for the interface or derive a subclass from the parent class, but it is worth noting that type parameters can no longer be included when using these interfaces and parent classes.

If you use a generic class without passing in actual type parameters, the Java compiler may warn that an unchecked or unsafe operation has been used-- this is a warning for generic checking.

2 actual combat-pass in the actual type parameters

Public class A1 extends Apple {/ / correctly overrides the method of the parent class, and the return value / / is exactly the same as the return value of the parent class Apple. Public String getInfo () {return "subclass" + super.getInfo ();} / * / / the following method is incorrect. When rewriting the parent class method, the return type is inconsistent public Object getInfo () {return "subclass";} * /}

Three actual combat-- no actual type parameters are introduced.

Public class A2 extends Apple {/ / override parent method public String getInfo () {/ / super.getInfo () method returns Object type, / / so adding toString () returns String type return super.getInfo (). ToString ();}}

Four, there is no generic class.

1 finishing touch

Although you can think of the ArrayList class as a subclass of ArrayList, in fact, the ArrayList class is indeed a special ArrayList class, and this ArrayList object can only add String objects as collection elements. But in fact, the system does not generate a new class file for ArrayList, and it does not treat ArrayList as a new class.

In fact, generics have the same behavior for all their possible type parameters, so that the same class can be treated as if it were many different classes. Consistent with this, static variables and methods of a class are also shared among all instances, so type parameters are not allowed in static methods, static initialization, or declaration and initialization of static variables.

Generic classes are not really generated in the system, so generic classes cannot be used after the instanceof operator.

2 actual combat

Public class R {/ / the following code error, cannot use type parameter / / static T info; T age; public void foo (T msg) {} / / in static variable declaration, cannot use type parameter / / public static void bar (T msg) {}} in static method declaration

At this point, the study on "what is the principle of Java generic inheritance" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report