java object-oriented knowledge expansion

Upcast and downcast:

In JAVA, inheritance is an important feature, extends through keywords, subclasses can reuse functionality of the parent class method of the parent class if the parent can not meet the current needs of the subclass, the subclass can override to be extension.

In the application there are two ways transformation, namely: upward transition and the downward transition .

For example: parent Parent, Child subclass

  1. Upcast: references to subclass object Parent p = new Child () of the parent;
  2. Description: When the upward transition, subclass object as a parent object, the function can only be called the parent class, if the child class overrides the parent method to call based on this reference points to subclass override method.
  3. Downward transition (less): subclass object reference points to subclass, the process must take into casts.
Parent  p = new Child();//向上转型,此时,p是Parent类型

Child c = (Child)p;//此时,把Parent类型的p转成小类型Child
  • // In fact, the equivalent of creating a subclass of objects, you can use the parent class, you can also use your own
  • Description: When downcast, for the convenience of using a special method subclasses, that is when the subclass method does function expansion, can be used directly subclass function.

The difference between static variables and instance variables

Differences in syntax definition: former static variable to add the static keyword, but not before adding instance variables.

The program runs difference:

  • Instance variables belonging to the properties of an object, you must create an instance of an object, in which instance variables are allocated space in order to use this instance variable.
  • Static variables do not belong to an instance of an object, but belong to the class, it is also known as class variables, as long as the program is loaded bytecode class, do not create any instance of an object, static variables will be assigned space, static variables can be used.
  • In short, after the instance variables must create an object can be used by this object, you can use static variables directly reference the class name.
Published 36 original articles · won praise 13 · views 1075

Guess you like

Origin blog.csdn.net/weixin_44598691/article/details/104751742