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

Case Analysis of final and inheritance Operation in Java

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

Share

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

This article mainly explains the "case analysis of final and inheritance operations in Java". 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 "the case analysis of final and inheritance operations in Java".

A little bit of eye contact

Final is called a Terminator in Java.

1 if final is added to a method of the base class, the method is forbidden to be "modified" twice in the subclass.

2 by adding the final keyword in front of the class, you can prevent the base class from being inherited.

Second, the practical 1--final tagging method cannot be overridden by subclasses.

1 code

Class Person {/ / this method declares that final cannot override final public String talk () {return "Person:talk ()";}} class Student extends Person {public String talk () {return "Student:talk ()";}} public class TestFinalDemo {public static void main (String args []) {Person S1 = new Student (); System.out.println (S1.talk ());}

2 run

3 description

An A JNI has occured error occurred at run time, where JNI means "Java Native interface (Java native interface)".

Three practical battles 2mura-the limitation of inheritance with final

1 code

/ / define the parent class final class SuperClass {String name; int age;} / / the subclass SubClass inherits SuperClassclass SubClass extends SuperClass {/ / do something} public class InheritRestrict {public static void main (String [] args) {SubClass subClass = new SubClass ();}}

2 run

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The type SubClass cannot subclass the final class SuperClass at SubClass. (InheritRestrict.java:18) at InheritRestrict.main (InheritRestrict.java:26)

Thank you for your reading, the above is the content of "case analysis of final and inheritance operations in Java". After the study of this article, I believe you have a deeper understanding of the case analysis of final and inheritance operations in Java, 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!

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