Java (2), java content

Source: Internet
Author: User

Java (2), java content

  • 1. The basic unit of object-oriented encapsulation is class. A class is an abstract logical structure, and a class object is a real physical entity; the purpose of a class is to encapsulate complexity, and there is a hidden implementation complexity mechanism inside the class. Two advantages of encapsulation: modularization: It can be compiled and maintained independently of the source code of other objects, objects can be easily transmitted in the system; hidden information: other objects can communicate through a public interface of the object without affecting other objects; inheritance inheritance is the process in which an object acquires the attributes of another object. The Inheritance mechanism is that an object becomes a specific instance of a more general class, avoiding repeated coding; polymorphism is an interface and multiple methods ", A common interface can be designed for a group of related actions. In fact, the overload of class functions is a manifestation of polymorphism. 4. The idea of abstract programming is introduced. Class encapsulation is an abstract idea.
  • In Java, except the static method and the final method (the private method is essentially the final method, because it cannot be accessed by the quilt class), all other methods are dynamically bound.This means that normally we do not have to determine whether dynamic binding should be performed-it will automatically happen.
    • The final method will make the compiler generate more effective code, which is also why the declared final method can improve the performance to a certain extent (the effect is not obvious ).
    • If a method is static, its behavior does not have polymorphism:
      class StaticSuper {    public static String staticGet() {        return "Base staticGet()";    }    public String dynamicGet() {        return "Base dynamicGet()";    }}class StaticSub extends StaticSuper {    public static String staticGet() {        return "Derived staticGet()";    }    public String dynamicGet() {        return "Derived dynamicGet()";    }}public class StaticPolymorphism {    public static void main(String[] args) {        StaticSuper sup = new StaticSub();        System.out.println(sup.staticGet());        System.out.println(sup.dynamicGet());    }}

      Output:

      Base staticGet()
      Derived dynamicGet()

  • Constructors do not have polymorphism. They are actually static methods, but the static declaration is implicit.Therefore, constructor cannot be overwritten.

  • Calling a function with multi-state behavior within the parent constructor will result in unpredictable results.Because the subclass object has not been initialized yet, calling the subclass method will not get the desired result.

    class Glyph {    void draw() {        System.out.println("Glyph.draw()");    }    Glyph() {        System.out.println("Glyph() before draw()");        draw();        System.out.println("Glyph() after draw()");    }}class RoundGlyph extends Glyph {    private int radius = 1;    RoundGlyph(int r) {        radius = r;        System.out.println("RoundGlyph.RoundGlyph(). radius = " + radius);    }    void draw() {        System.out.println("RoundGlyph.draw(). radius = " + radius);    }}public class PolyConstructors {    public static void main(String[] args) {        new RoundGlyph(5);    }}


    Output:

    Glyph() before draw()
    RoundGlyph.draw(). radius = 0
    Glyph() after draw()
    RoundGlyph.RoundGlyph(). radius = 5

Why is this output? This requires clear understandingJava constructor Call Sequence:

(1) initialize the storage space allocated to the object to binary 0 before any other thing occurs;
(2) call the base class constructor. Recursion starts from the root, because polymorphism calls the draw () method after the subclass overwrites at this time (to be called before the RoundGlyph constructor is called). Because of step 1, the radius value is 0;
(3) Call the initialization method of members in declared order;
(4) Finally, the constructor of the subclass is called.

    • Only non-private methods can be overwritten, but you need to pay close attention to the phenomenon of overwriting private methods. At this time, although the compiler will not report errors, it will not be executed as we expected, that is to say, overwriting the private method is a new method rather than an overloaded method for the subclass. Therefore,In subclass, it is best not to use the same name as the private method of the base class (although it does not matter, it is easy to misunderstand that the private method of the base class can be overwritten).

    • Access to attribute domains in Java classes is parsed by the compiler, so they are not polymorphism.The attributes with the same name of the parent class and subclass are allocated with different buckets., As follows:

      // Direct field access is determined at compile time.class Super {    public int field = 0;    public int getField() {        return field;    }}class Sub extends Super {    public int field = 1;    public int getField() {        return field;    }    public int getSuperField() {        return super.field;    }}public class FieldAccess {    public static void main(String[] args) {        Super sup = new Sub();        System.out.println("sup.filed = " + sup.field +                 ", sup.getField() = " + sup.getField());        Sub sub = new Sub();        System.out.println("sub.filed = " + sub.field +                 ", sub.getField() = " + sub.getField() +                 ", sub.getSuperField() = " + sub.getSuperField());    }}


      Output:

      sup.filed = 0, sup.getField() = 1
      sub.filed = 1, sub.getField() = 1, sub.getSuperField() = 0

      Sub subclass actuallyContains two fields called fieldHowever, the default field generated when the field in the Sub is referenced is not the field of the Super version. Therefore, to obtain the Super. field, you mustExplicitly specifySuper. field.

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.