Dynamic/static bindings and shadowing involved in Java inheritance

Source: Internet
Author: User

The project often uses the Java polymorphism this feature, before only a few fur, now found that it does not have a systematic understanding of it, want to re-comb their own base library.

Read the introduction to the object of Java programming, the description of inheritance: Thetypes in Java not only describe the constraints that work on a collection of objects, but also the relationships with other types . you can create a base class to represent the core concepts of some objects in the system, and derive other types from the base class to represent the different ways this core can be implemented . So we know that there is an inheritance relationship between classes and classes.

Question: What is polymorphism? What's the relationship between polymorphism and inheritance?

Suppose I write a base class that has multiple export classes, and each time you create our own objects because of the way we call the exported class, it is possible for n multiple export classes to create N export class objects.

The advantage of polymorphism in Java is that it is possible not to create specific export class objects, but rather to call the respective methods of the base class objects. This situation is described in the book: when dealing with a type hierarchy, you often want to treat an object as a base class object instead of treating it as the particular type it belongs to.

For example: Sao is a base class, Alisao and Weixinsao are their export classes, all have an identical Hello method

Follow your old routine:

Alisao Alisao = new Alisao ();

Alisao. Hello ();

... ...

If there are n export classes, there will be n different types of objects

... ...

Now we are going to change the type of the exported class to the base class and leave it to Java, so that we can write a code that doesn't depend on a particular type.

SAO Alisao = new Alisao ();
SAO Weixinsao = new Weixinsao ();

Alisao.hello () and Weixinsao.hello ()

Alisao and Weixinsao objects can be unified with the Sao type

This code is less affected by the newly added code.

But this begs the question: how can the object itself be determined to execute the correct method when the exported class object is treated as a generalized base class type Object?

A non-object-oriented compiler produces a function call that causes so-called pre-binding, and the compiler produces a call to a specific function name that is parsed to the absolute address of the specific code at run time. There is no Nondeterministic method call for a generalization object like Java.

In the case of private, static, final, or constructor, the compiler explicitly knows which method to invoke, which is called a "static call." Dynamic binding is only a non-private,static,final method for an object.

In object-oriented programming, this scenario is not able to determine the exact invocation of that method during compilation, and the Sao type object (Alisao and Weixinsao) cannot determine its specific type when it is not running (some people think sao Alisao = new Alisao (); Do not know is the Alisao type generalization, at this time is in the compilation, the object is not created, the virtual machine does not know, like your own mind is very clear, you want to let others know, first you have to say, this say is running), hello () Method does not know whether the calling own Sao object is a base class or an export class.

In order to solve this problem the object-oriented programming language puts forward the concept of late binding:Java uses a small piece of code instead of an absolute address call, which uses the information stored in the object to calculate the address of the method.

In this way, each object can behave differently depending on the content of the code, and when a message is sent to an object, the object can respond accordingly.

All of the above are examples of methods, if the static properties of the class and static methods can inherit?

The answer is yes. Just this inheritance has a feature called hiding. When the subclass properties and methods are the same as the parent class, a hidden behavior occurs. For example:

The reality of Java polymorphism is dependent on parent-child class inheritance, interface implementations, overrides, and overloads. With inheritance, you can point to the parent class reference by using a subclass object, and you can override the method by using the subclass object referenced by the parent class, instead of the method of the parent class, because the subclass of the child class is "overridden" The priority of the method is higher than the precedence of the parent class. Instead of hiding this property , the parent PS = new Son ();p s.getname () is a non-static method, which is called a subclass-overridden method, Ps.getage () is a static method, only the method of the parent class can be called!

Remember that the export class inherits the overrides of the base class method, and the non-static method of the exported class takes precedence, if it is a property?

If you notice the following:

Parent PS = new Son ();
System.out.println (ps.age);//Returns the age=30 of the parent class

System.out.println (Ps.money);//Returns the money=500 of the parent class

System.err.println (ps.name);//' Jack '

Where age and money are static variables in both the base class and the exported class. Name is a non-static variable.

You will know that the property overrides of the base class and the exported class have no precedence, and all get the property values of the base class. It is not related to the object type, only the object reference type!

Overrides occur only in the methods inherited by the parent-child class, and the attribute is not rewritten. When the export class has properties that have the same name as the base class (even types can be different), the properties of the base class are hidden

For subclasses, the properties of the parent class are not accessible to the Class object reference, but are accessed through a reference to its parent class object, which is generally not recommended because it makes the code difficult to read;

As can be seen from the above definition, a member property cannot be overridden as a method, when a subclass defines a member property that has the same name as its parent, the subclass simply declares a new property, and the property of its parent class is hidden, which is not a rewrite. In fact, a super. Property name or a non-private property of the parent class can be obtained, so it cannot be accessed in polymorphic form.

Upward transformation: The process of seeing an exported class as a base class is an upward transformation , the previously mentioned SAO Alisao = new Alisao (), which is an upward transformation whose actual object is Alisao the object of the exported class, but the base class is referenced.

You can only export class objects to the base class object, and vice versa (if the base class object can be transformed into an exported class object, the object loses the behavior or properties of the exported class, after all, the properties and methods of the exported class are equal to or more than the base class).

Dynamic/static bindings and shadowing involved in Java inheritance

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.