In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the three object-oriented features of 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 "how to understand the three object-oriented features of Java".
Inherit
Inheritance in Java can only inherit only, but multiple inheritance can be achieved by inheriting other classes from inner classes.
Public class Son extends Father {public void go () {System.out.println ("son go");} public void eat () {System.out.println ("son eat");} public void sleep () {System.out.println ("zzzzzz");} public void cook () {/ / Multi-inheritance new Mother (). Cook () implemented by anonymous inner classes; / / the inner class inherits the second parent class to achieve multi-inheritance Mom mom = new Mom (); mom.cook () } private class Mom extends Mother {@ Overridepublic void cook () {System.out.println ("mom cook");} encapsulation
The main reason for encapsulation is that Java has access control. Public > protected > package = default > private. Encapsulation protects the information in the class and provides only the information you want to be accessed by the outside world.
The access scope of the class
Within and outside the package of An and public, all classes are visible in B and protected, and the subclasses with inheritance relationship outside the package are visible (subclass objects can be called) C, (default) means default, not only for this class access, but also for the same package. D and private can only be seen in the same class.
There are generally two types of polymorphism, one is rewriting overwrite, the other is overloading override.
Overriding is because the subclass in the inheritance relationship has a method with the same name and parameter as the parent class, which overrides the method of the parent class. It is overloaded because a method with the same name can pass in multiple parameter combinations. Note that if the parameters of a method with the same name are the same, it cannot exist at the same time even if the return values are different, and the compilation will make an error. From the perspective of jvm implementation, rewriting is also called run-time polymorphism, and you can't see which method is called by the subclass at compile time, but the runtime operator stack will first look up the method in the class information of the subclass according to the reference of the subclass, and then look for the method in the class information of the parent class if you can't find it. Overloading, on the other hand, is compile-time polymorphism, because at compile time you can determine the combination of incoming parameters and determine which specific method to call. Explanation of upward and downward transitions: public static void main (String [] args) {Son son = new Son (); / / first of all, it is clear that transformations refer to changes in references on the left. / / the father reference type is Father, which points to the Son instance, which is an upward transformation, using either the methods of the subclass or the methods of the parent class. / / transform upwards, and the method that runs father Father father = son; father.smoke (); / / cannot use methods unique to the subclass. / / father.play (); compilation will report an error father.drive (); / / the reference of the Son type points to the instance of Father, so it is a downward transformation, and the non-overridden method of the subclass cannot be used, but the method of the parent class can be used. / / make a downward transition. After running the son method Son son1 = (Son) father; / /, a normal Son instance son1.play (); son1.drive (); son1.smoke (); ```/ / must undergo an upward transition before the downward transition. / / in the process of downward transformation, it is divided into two situations: case 1: if the object referenced by the parent class refers to the subclass object, / / then it is safe in the process of downward transformation. That is, the compilation will not go wrong. / / because the runtime Son instance does have these methods Father F1 = new Son (); Son S1 = (Son) F1; s1.smoke (); s1.drive (); s1.play () / / case 2: if the object referenced by the parent class is the parent class itself, it is not safe during the downward transformation, and the compilation will not go wrong, but a java.lang.ClassCastException error will occur at run time. It can use instanceof to avoid such errors. / / because the runtime Father instance does not have these methods. Father f2 = new Father (); Son S2 = (Son) f2; s2.drive (); s2.smoke (); s2.play (); / / downward and upward transition applications, some people think that this operation is meaningless, but it can actually be used for type aggregation in method parameters, and then decompose the specific operation. / / for example, the add method passes in the List reference type as a parameter, and passes in a specific class with a downward transition of add (new LinkedList ()); add (new ArrayList ()) / / Summary / / upward and downward transitions are both for references, at compile time, to determine which method to use according to the reference type, and automatically transform when the method is passed in (if necessary). The runtime will point the reference to the instance and will report an error if it is an insecure transition. / / continue to execute the method if it is safe. } public static void add (List list) {System.out.println (list); / / experienced an upward transition when operating a specific collection / / ArrayList arr = (ArrayList) list;// LinkedList link = (LinkedList) list;}
Summary:
Both upward and downward transitions are aimed at references, which are carried out at compile time, and which method is used according to the type of reference. And the transformation occurs automatically when the method is passed in (if necessary). The runtime refers to the instance, reports an error if it is an insecure transition, and continues to execute the method if it is secure.
Static dispatch at compile time: actually calling the corresponding method based on the reference type. Public static void main (String [] args) {Father father = new Son (); static dispatch a = new static dispatch (); / / compile time determines that the reference type is Father. / / so the first method is called. A.play (father); / / after the downward transformation, the reference type is Son, and the second method is called. / / therefore, only references are determined at compile time, and instantiated at run time. A.play ((Son) father); / / when there is no method of Son reference type, the first method is automatically transformed upwards. A.smoke (father); / /} public void smoke (Father father) {System.out.println ("father smoke");} public void play (Father father) {System.out.println ("father"); / / father.drive ();} public void play (Son son) {System.out.println ("son"); / / son.drive () } method overload priority match public static void main (String [] args) {method overload priority match a = new method overload priority match (); / / ordinary overloads are generally different parameters of the same name method. / / here we discuss what happens when a method with the same name has only one parameter. / / the method of the char parameter is called at this time. / / when there is no method with char parameter. A method of type int is called, and if there is no int, long / / is called, that is, there is a calling order char-> int-> long-> double->.. / / when there is no method corresponding to the basic type, it is boxed automatically and the wrapper class method is called. / / if there is no wrapper class method, the method of the interface implemented by the wrapper class is called. / / finally, call the char... that holds multiple parameters Method. A.eat ('a'); a.eat ('ahem);} public void eat (short I) {System.out.println ("short");} public void eat (int I) {System.out.println ("int");} public void eat (double I) {System.out.println ("double") } public void eat (long I) {System.out.println ("long");} public void eat (Character c) {System.out.println ("Character");} public void eat (Comparable c) {System.out.println ("Comparable");} public void eat (char... C) {System.out.println (Arrays.toString (c)); System.out.println ("...");} / / public void eat (char I) {/ / System.out.println ("char") / /} Thank you for your reading, the above is the content of "how to understand the three major object-oriented features of Java". After the study of this article, I believe you have a deeper understanding of how to understand the three object-oriented features of 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: 244
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.