JAVA's object-oriented design features-post-book Sense-01-21-day learning java-in-depth analysis

Source: Internet
Author: User
Tags variable scope

What are the characteristics of </pre>java as an object-oriented language? This article attempts to enumerate some of the most basic: from the <21 java> to pick some concepts, and do a certain deep excavation understanding. <p></p><p> This chapter attempts to recompile the order angle to understand the differences in Java inheritance overrides, Overrides, overloads. There may be a wrong place, please point out. </p><p></p><p><table border= "1" width= "cellspacing=" 1 "cellpadding=" 1 ">< Tbody></tbody></table></p><p>1 control Logic: The concept of </p><p>1.1 package, Java provides the concept of package, more convenient management of the class. </p><p>1.2 final concept, final is actually a mechanism for initializing member variables, local variables, and methods to manage and prevent errors. The </p><p>final method can inherit, however, the </p><p>1.3 static concept cannot be overridden: (Understanding static, I have a description in the back, that is, the concept is not compiled) </p ><p> when considering the scope of a variable, the effect of static is to make an enlarged change to the scope of the variable,</p><p> A static modified member variable belongs to a class (in fact, the scope is a class) and does not change because a new object has been created. </p><p> Development Look, Java class operations to consider variables and classes, the scope of the object, for example, the static variable scope is dependent on the class, not the object, then the object-dependent method (such as this) operation on the static variable will result in an error. </p><p> unless the method of this object is already confirmed, or, from a Java perspective, and opens up the code space for his own existence:</p><p> that is, static member variables are available through < strong> non-static </strong> way to access. </p><p></p><pre code_snippet_id= "540606" Snippet_file_namE= "blog_20141203_2_9778037" name= "code" class= "Java" >//bike class describes a bike class bike{//declares a variable of type string colorstring color = "yellow";//Declares a method public final void Getmes () {System.out.println ("member variable of the parent class color:" + color);}} The test class describes the final decorated member variable public class Test9 extends bike{//declares a variable of type string colorstring color = "green"; The main entry function of the//java program public static void Main (string[] args) {//Create object instance of test class Test9 t = new Test9 ();//Call Method Print result T.getmes ();}}



Similarly, if it is a static method, such as:

public static void Main (string [] args)

{

System.out.printlin (color); Similarly, this color variable is also wrong if it is not a static variable.

}


2 Inheritance:

Java inheritance has his characteristics, first: is a single inheritance, that is, a subclass of the parent class only one. Secondly, it is multi-level inheritance.

2.1 Default:in JAVA:

The difference between default and public in Java is whether the same package is supported.

2.2 Private

Java's private is a good package of the key technologies you want to encapsulate, and the following example illustrates an example of this package:


class door{  private String color; Constructor public Door () {this.color = "Private type member variable in parent class" for the//door class defined as private type;} private void Opendoor ()///due to the method defined as private, it is not possible to use the {System.out.println ("This method is Private Method!") in the inheriting class;} public void opendoors ()//through the re-encapsulation of the interface, you can put and private methods, rewritten, and then changed to public use, which can be seen by the inheriting class. {Opendoor ();}} Wood_door inheritance with Doorpublic class Wood_door extends door{string color;<span style= "White-space:pre" ></span> Here you try a member variable of the same name//wood_door the constructor public Wood_door () {This.color = "is a member variable in this class";//If you remove the definition of color in this class, you will get an error here,  The reason is that the member variable of the parent class is private}public static void Main (string[] args) {Wood_door wd = new Wood_door ();//access is the member variable and method of the parent class WD. Opendoors (); Here is the public method, if the private interface is not re-encapsulated in the parent class by means of the new publicly method, the error <span style= "White-space:pre" ></span>// The member variable SYSTEM.OUT.PRINTLN (wd.color) of this class is accessed;}} 
The results of the operation are as follows:
This method is Private Method! is a member variable in this class




2.3 Protected:

Protected mainly refers to the categories of different packages and packages, and the scope of the member variables and methods affected by them. V

2.4 Override and Overload

Override is the interface of the method of the class is exactly the same, however, the implementation of the method is different.

Overload is the same as the name of the method, but the parameter list of the method is different.

2.5 static method

The "hsy75" case, the understanding of static methods, in fact, to go into, compiled. If you understand the steps of compiling, it is better to understand the limitations of static.

Static is the declaration of the compilation at the time of the decision to open up the area, I can understand that a section of the code area, since the compile time has been added, then, very good, all

The subsequent instantiation of the object (which should actually be an area on the heap of memory) is obviously unable to implement the simple inheritance of the original static method.


Static methods can be inherited, but cannot be overridden

"Hsy75" is quoted as a summary of the book, in fact, sometimes feel that the person who wrote the book is also relatively chaotic, always put forward those strange concept of contrast, feel some concept even more than eight Poles hit together.

Static methods can be inherited, the static method is nothing more than a compile time is added, a piece of code snippet of course can be instantiated to the heap, as to can be rewritten, this is what connection.

Because the override is override, it has the same name as the inherited parent class method but has different specific behavior. Overrides are subclasses of the concept of a parent class,

To modify the previous example the//door class describes a gate class Door{}//wood_door class that describes a wooden door class Wood_door extends Door{}class math{//getmes () method is modified for static public type, note static public door getmes () {return new Door ();}} public class Son4 extends Math{//getmes () method is modified for static public type, note static public Wood_door Getmes () {return new Wood_door ();} public static void Main (String args[]) {//Creates an object instance of the son class, the name of the object reference MMath m = new Son4 ();//hsy75 case, here if you change to son m = new Son (); is another result//print and display the result System.out.println (M.getmes ());}}

In the case of "hsy75", the following example is an example of my rewriting of books as a validation supplement to these concepts
The Math class, which describes a mathematical class that is used for mathematical calculation  of class math{//overloaded method Add, the argument list is of type int of the public int add (int i, int j) {return i + j;} Overloaded method Add, the parameter list is float type public float add (float I, float j) {return i + j;} Static methods cannot override an example of static public int sub (int i, int j) {return i-j;} Static methods cannot be overridden for example 2static public int mux (int i, int j) {return i * j;} The static method cannot override the example 3public int div (int i, int j) {return i/j;}} public class Chapter8_math extends math{public int add (int i, int j) {return i;} static public int sub (int i, int j) {return J;} Static methods cannot be overridden for example 2 "case 0"/* Such compile error public int mux (int i, int j) {return i * j;} *///static methods cannot be overridden for example 3 "case 0"/* Such compile error static public int div (int i, int j) {return i/j;} */public static void Main (String args[]) {
<span style= "White-space:pre" ></span>//"Case 1" chapter8_math m = new Chapter8_math ();// Created an instance of the subclass Chapter8_math of the object instance of math whose object reference is MSystem.out.println (M.add (1.0f, 2.3f));//Perform overloaded methods, Execute that method according to the type of the Add parameter System.out.println (M.add (1, 2));//Perform rewrite succeeded//static method cannot rewrite example <strong><span style= "COLOR: #ff0000 ;" >system.out.println (M.sub (1, 2));//Perform rewrite success </span></strong>
"Case 2" math m2 = new Chapter8_math ();//Creates an instance of an object instance of the subclass Chapter8_math of math whose object reference is MSystem.out.println (M2.add (1.0f, 2.3f );//Execute the overloaded method, execute that method according to the type of the Add parameter System.out.println (M2.add (1, 2));//Perform rewrite succeeded//static method cannot rewrite example <strong><span style= " Color: #ff0000; " >system.out.println (M2.sub (1, 2));//Perform rewrite unsuccessful </span></strong>
<span style= "White-space:pre" ></span>//"Case 3" math m3 = new Math ();//Create an object instance of math, Its object reference is MSystem.out.println (M3.add (1.0f, 2.3f));//Execute the overloaded method, execute that method according to the type of the added parameter System.out.println (M3.add (1, 2));// Failed to perform rewrite? No, because it is not an example of a rewrite//static method that cannot be overridden System.out.println (M3.sub (1, 2));//execution rewrite unsuccessful? No, because it's not a rewrite}}

The results of the operation are as follows:

3.3
1

2


3.3
1

-1


3.3
3
-1

Summary:

1 The difference between overloading and overriding

Overloading and rewriting are good differences, see if the interface is consistent. "The case" is very good interpretation of this point. Since the overloads themselves are inside the parent class, there is no doubt that all outputs are 3.3

2 static methods cannot be rewritten-is this definition correct in the "hsy75 case" book???

2.1 See "Case 0" The parent class is a static subclass is public, or the parent class is the public subclass is static, both of which are not supported by Java, the compilation has no pass.

2.2 Static methods belong to the class, and belong to the class is actually compiled.

First we look at the difference between "Case 1" and "Case 2", they all create an instance of Chapter8_math, but there is a difference between the object's application type, the object's reference type is different, the compiler type is different, the compiler type is different, The effect of static and static member variables has been brought into play.

Whether you compile math first, or Chaper8_math (the subclass of Math), as long as you create a Chapter8_math instance, the Add method, which is declared as PUBILC in the parent class, is clearly overridden in the subclass.

Of course, when we were wondering about "Case 3", we might have overlooked that "case 3" was created as a math instance, so from the compilation to the heap is a typical parent class, so this is not the overriding operation at all.


Now, there is a key point to the static method problem of the static modification,

The sub method, which is now declared static, is obviously due to different types of object references, or different classes of compilation, resulting in different results of rewriting, failures and successes.

From the result printing you can see that as long as the object's reference declaration is Chapter_math, that is, the child class is compiled first, the static method of the subclass will override the method of the parent class.

That is, the author said that static methods can not be rewritten is wrong, as long as we declare the object to refer to the name of the subclass is good, can be rewritten.


3 You may be confused about the difference between overwriting and rewriting in the book

In fact, the simple override is the override of the member variable of the parent class, which is either a form of rewriting, or that overrides are the overrides of the method.


This post original, paste please indicate the source, is entirely their own ideas, if there are errors, please and

[Email protected] Contact




JAVA's object-oriented design features-post-book Sense-01-21-day learning java-in-depth analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.