Understanding of several confusing keywords in Java

Source: Internet
Author: User
Tags abstract define garbage collection inheritance
Behavior regulates the requests you can make to objects. Your class, that is, the object, that is, MM, you design her out, and you are very bt, only to her design two behavior: Love Me () and make Love with Me (). Then she would not be able to accept other client class (a handsome guy?) The request, if in a class, you write the MM. Love F4 (), then the compiler will make an error.
You naturally set the attributes of mm to beauty, you do not want others to change this fact, then, you have to define this attribute private, so that MM will not wake up the next day to become the legendary KL. This is the title in the first chapter: hidden implementation Details. An attribute with four modifiers, public,private,protected, null (default, friendly). The meaning of the distinction is that any object can be accessed, within which the inheritors of this object (class) can access, and other objects of the same package (package) can be accessed.
We always want to make our code as concise as possible, which requires the implementation code to be reused. Java provides this approach in two ways: composition and inheritance, assuming that there is a class for a,a there is a behavior dosomething (), and a class B, do not also want to do something, then you can generate a method in B Dosomethingtoo ( A.dosomething (); )。 This is called a combination. Inheritance is another way, you can directly use the keyword extends, let b inherit from a, then you do not have special additional expression, B in the outside world is also capable of dosomething. So we're going to ask, when are we going to mix and when do we inherit? Bruce says we use inheritance when B is a A. This is not very good understanding, we give another example of MM. MM have to eat (eatyou (eat; digestion)) behavior, mm derived from a lustful mm, love fart mm, but they are mm, is a relationship, this time you want to design a lustful mm when the use of inheritance, with extends. and GG is not mm, but the GG is like a mm, because GG can also eat. This time we do not have to write eatyou () This method, directly in the GG class write Eatyou (Mm.eatyou ();) Then, GG also has the function of eating and digesting. In fact, we have to use the combination, in the program, with the inheritance of the place is relatively small.
Of the inheritance, there are two special behaviors that require our attention: overwrite (overriding) and overload (overloading). Now all you have to remember is that if a method name of the base class and derive class is the same and the arguments are the same, the overrides (overriding), the same name, the same argument, the overload (overloading).

A significant advantage of object-oriented is the polymorphism (polymorphism). I did not want to write the code in this first part, but it seems that the problem is not intuitive enough to express the language, so I wrote the simplest way to illustrate the problem of polymorphism. Look at the code first ...
Love.java
Class mm{
public void Toseegg () {
SYSTEM.OUT.PRINTLN ("xxx");
}
}

Class Haosemm extends mm{
public void Toseegg () {
System.out.println ("Temptation GG!!!");
}
}

Class BENFENMM extends mm{
public void Toseegg () {
System.out.println ("Good shy Oh ...");
}
}

public class love{
public static void Lovegg (MM i) {
I.toseegg ();
}
public static void Main (string[] args) {
MM mm1 = new Haosemm ();
MM mm2 = new benfenmm ();
Lovegg (MM1);
Lovegg (mm2);
}
}
The results of this code run are:

Temptation GG!!!
So shy oh ....

We see from the mm derived from a lust mm (HAOSEMM), this is mm (BENMM), MM are likely to see handsome men, but lust mm and this mm look handsome boy's eyes is not the same, such as the program definition. In love this class, we define a method Lovegg, we pass it a parameter, is the base class (base class) MM, and then Toseegg (). Through the first trace of this article, we know that the relationship between Haosemm and benmm and MM is the relationship between is a, so we use the Lovegg (MM1) and Lovegg (mm2) when the compiler does not go wrong. We see that the program automatically executes the HAOSEMM and benfenmm Toseegg () instead of printing out "xxx". This is the form of polymorphism. This is so magical because the Java Runtime Environment provides dynamic binding technology. Dynamic binding will allow you to produce the MM in the Java Runtime environment in accordance with your instructions to act separately. OK, we don't need to know how dynamic bindings are shipped, we just have to know what this is because we're still in chapter I. The concept of tracing back (upcasting) is also presented here. In Lovegg (mm i) This method, the method accepts is MM, but Lovegg also accept Haosemm and benfenmm, this characteristic is called trace back modelling.

We see in the above program is not used in the code, is in the MM class System.out.println ("xxx"); No one cares about the base class mm is how to see handsome boy, because it is just a template, so we simply do not want this code, and we do not even {}, directly rewrite this method for public abstract void Toseegg (); So this method is called abstract method. Base class MM is not necessary for us to achieve, so we put class mm{...} Rewrite to abstract class mm{...}, then this class is called abstract classes (abstract class). We can't help but wonder if the abstract class can contain a function that is not abstract. Answer Yue: Yes. But this is not useful in practice, the only place to use is: 1,main () function, used to test your class; 2, in the exam. So let's also ask that subclasses can not overwrite abstract methods in the parent class. Answer Yue: If the subclass is also abstract, so can, otherwise not.

More thoroughly than abstract classes are interfaces (note that the interface here is the real interface in Java, not the interface at the beginning of the above, but also behavior). The interface is designed to allow you to inherit. And so on ..., this is the first chapter, just let people know some concepts on the line, above we say too much.

Bruce was a complete NN master, and we saw that he took Java's inefficiency as a matter of course, so that when we read it, we couldn't help but say:
Well, Java should do this, let C + + hell go. Object-oriented, so all Dongdong are objects, there are objects will be the generation and destruction of objects. When the program is running, the object is generated in memory. There are three strategies for allocating memory, namely static, stack (stack), and heap (heap). C + + uses the first two strategies, and Java uses only the latter. What's the difference between the two? Like a inquisitive friend. Please refer to the difference between stack storage (stacks) and heap storage (heap) on the Jsp/java of this site, as long as we keep in mind that the latter's addressing in memory takes a long time, so Java is inefficient. But Java provides a garbage collection mechanism that is based on heap characteristics. The garbage collection mechanism completes such tasks and automatically perceives and eliminates the object when it is not in use. You don't have to worry about a memory leak anymore. And that's what every C + + programmer has the most headaches. So, you choose whether to be safe or efficient.

In fact, the first chapter of the next thing is not important for beginners, the above your knowledge you understand, in the next study you will be much easier. I suggest you not to see it so clearly. I think we should get started and go to chapter two now.

Related Article

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.