The difference between inheritance in Java and inheritance in OC

Source: Internet
Author: User

Why use inheritance?

Benefits of Inheritance:

(1) Duplicate code is extracted to make the code more flexible

(2) establishing a link between classes and classes

Disadvantages of Inheritance:

Coupling is too strong

The inheritance in OC is:

1. The name of the member variable with the same name is not allowed in OC and the parent class; (yes in Java)

2, the compiler executes from top to bottom, so there should be at least a declaration of the parent class in front of the subclass;

@interface Worker:person @end

3, the sub-class in OC can have the same name as the parent class method, in the subclass call, take precedence of their own internal search, if the parent class does not define the method, then continue to find on the inheritance chain, until found, if found nsobject still not found, then error;

4. Subclasses are allowed to override the methods of the parent class, and when the overriding method of the subclass object is called, the overridden method is called (the same as the non-static method in the Java subclass overriding the parent class)

5. If you need to invoke the function of a parent class in a subclass, you can use the Super keyword (as in Java).

The inheritance in Java is:

Note: Static blocks of code, no matter how many objects are created, are executed only once, and non-static blocks of code are executed as the object is created and always before the construction method.

1, static code block of the parent class--the main method of the subclass--the master method, which program executes which program, and the parent class, non-static code block----the parent class, non-static code block The parameterless constructor of the subclass (if the actual subclass executes a parameter constructor, does not execute the parameterless constructor), the member function (refers to a non-static method) (specifies which member function executes, and if the parent class member function is overridden, only the member function of the subclass is executed )

Why is it that overriding a static method of a parent class at polymorphic Times does not call a static method of the parent class, and the subclass overrides the non-static method of the parent class when the child class is called the non-static methods of the subclass?

Look at the code first:

1 classsuperclass{2     //Static Methods3      Public Static voidStaticmethod () {4System.out.println ("Superclass:inside Staticmethod");5       }6       //non-static methods7        Public voidmethod () {8System.out.println ("Superclass:inside method"));9       }Ten } One   A  classSubclassextendssuperclass{ -  -     //overriding the static method the      Public Static voidStaticmethod () { -System.out.println ("Subclass:inside Staticmethod"); -    } -     //overriding the method +     Public voidmethod () { -System.out.println ("Subclass:inside method")); +       } A  } at  Public  classa - { -  Public Static voidMain (String []args) { -    -Superclass superclass =NewSuperclass ();//Parent class itself -Superclass SubClass1 =NewSubclass ();// polymorphic inSubclass SubClass2 =NewSubclass ();//Subclass itself -   to Superclass.staticmethod (); + Subclass1.staticmethod (); - Subclass1.method (); the Subclass2.staticmethod (); *  $     } Panax Notoginseng}

Operation Result:

Note the output of the second row, and if you override the static method, the result of the second row and the third row is the same. Even if you rewrite the static method, the compiler will not error, in essence, this is not a rewrite, you personally think that the rewrite, that is, if you try to override the static method, Java will not prevent you to do so, but you do not have the expected results (overrides are only useful for non-static methods). Overrides refer to which method is called depending on the type of the run-time object, not the type at compile time. Let's guess why the static method is more special? Because they are methods of classes, they are bound with compiled types at compile time. Using object references to access static methods is only the Java designer's freedom to programmers. Instead of using object references to access static methods, we should use the class name directly. The non-static method of the parent class is the same as the non-static method name of the subclass (including parameters), which is called rewriting because they are accessing the same memory space, and the static memory space is shown below.

We need to know how static is stored in memory.

The static and static variables of a class are used, and the JVM reads the static and static variables of the class into memory (in fact, the method area) in the process of loading the class, equivalent to the resident memory. Also called class loading, the static modifier is loaded into memory when the JVM is running, so no instance class is required
As you know, any variable or code in the program is stored automatically by the system when it is run, and the so-called static means that the allocated memory will persist after the first allocation of memory, until the program exits the memory to release the space, that is, the memory will persist as long as the program is running. What is the point of doing this? In a Java program, everything is an object, and an object's abstraction is a class, and for a class, if you want to use his members, you would normally have to instantiate the object before you can access those members by reference to the object, but there is an exception. It is that the member is declared with static (the access control of the class is excluded here), because the static modified method is statically bound at compile time, each static method is in a different memory space, so it is still called the static method of the parent class. Conclusion: 1, OC in the inheritance is first to the sub-category to find, not found in the parent class to find. 2. Inheritance in Java is first found in the parent class, not found in the sub-class.

The difference between inheritance in Java and inheritance in OC

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.