java ----- rights modifier, rewrite, super and final keywords, polymorphism, transformation and abstract classes

Permissions modifier

   java in 4 modifiers are public, protect, default, private, which they explain the object-oriented encapsulation, so we have to use them as much as possible so that rights to a minimum, thereby improving safety. (The same class, in addition to the class of all internal modifiers are accessible, so the following exclude this case.)

Permissions modifier This class With bags Different classes buns Other types of different packages
private T F F F
default T T F F
protected T T T F
public T T T T

note:

  1. More authority modifier can only be modified members (static members can only be modified), members of the modifier (member variables | member method).
  2. private content can not be inherited, because after private modifications represent private, so it can not be inherited.
  3. Only public and default class can be modified, and default appears by default (that is, do not write default, if you write a will complain).
  4. protected access:
    • 1. package at the same class.
    • 2. The subclasses of different packages, and can only be accessed by the parent child relationship, only subclasses can be used.

Rewrite override

The difference between the rewrite and overloading:

  1. It refers to rewrite and reload method.
  2. Overload:
      a plurality of a method of the same class.
      2. The method of the same name.
      3. List the different parameters | different method signature.
  3. Override:
      1. Different classes.
      2. inheritance | realization relationship.
      The method of the same signature (method name + parameter list).
    Note: If you override methods exist, called in when the call is overridden method in a subclass.
       Overwriting methods have been rewritten in the superclass shield.

How to check the method of rewriting:
  1.eclipse codes have left an upward triangle.
  2. Mandatory inspection @Override.

class SiCong extends JianLin{
	//重写定义父类中某个功能的实现-->方法的重写
	//@Override强制检查是否重写
	static JianLin words(){
		System.out.println("我不在乎我的朋友有钱没钱,反正都没我有钱...");
		return null;
	}
}

Rewrite features

  • (==): method signature
  • (<=): Return Value Type: basic data types are identical, but the reference data types: a subclass <= superclass
  • (> =): Permission modifier: subclass> = permissions of the parent class modifiers

Can not be overridden:
  1. modified private methods can not be overridden.
  2. the final modified method can not be overridden.
  3. be modified static methods can not be overridden.
  If the same method on the child the parent class, parent class is static, the same method on the subclass of static requirements have been modified.

super keyword

  1. the difference between this and the super:
    this refers to the current object new
    super refers to the parent object
    super line used in the first call the parent class's constructor constructor
  2. The use of super
    super (parameter list)
    must be used first line again, If you do not call the other parent class constructor, the default constructor calls the parent class empty.
  3. members of the parent molecule of the same name super area
    if members of the same name, the problem child in the parent class does not exist, can be omitted, the default look for a subclass of class, find the parent in the parent class.
    If there is a parent child class members with the same name, and you want to call the members of the parent class, use super represents the parent class object, otherwise the default subclass (principle of proximity).
  • The object is to create a subclass of class after my late father
  • After the first static member

final (keyword) final

  • final modification of variables constant.
  • The final modification method can not be overridden.
  • final modified class can not be inherited (eunuch class).
  • The final modification is a reference data type, address of the object can not be changed, but you can change the member objects.

Object class (ancestors)

  java object is according to the class of all classes, java classes are all directly or indirectly inherit from the Object class.
toString () method
  toString () is one of the object's methods when printing an object reference when the default call the object's toString (), to obtain a reference address. But the general printing an object, in fact, want to see the values of all the properties of an object, this time on the need to override toString () method, customized content output object properties, property values to achieve the object of printing, do not print addresses. The default will print the address of the object, because the default call type Object toString () method.
equals () method
  equals and == difference:

  • == comparison is the same address.
  • equals if the subclass does not override the default equals method calls the Object class, more is the address, but can be overridden to customize compare rules, in accordance with the contents of comparison are the same.

Polymorphism

One of the three characteristics of object-oriented ------ polymorphism: a thing in a variety of forms, multiple representations.

Polymorphic premise:

  1. Class inheritance.
  2. Implementation of the interface.

Polymorphic ultimate expression:
  reference to the parent class of pointing object subclass.

Polymorphic object:

  1. A reference to the parent class to call if the child has a parent class method call is overridden method in a subclass.
  2. New subclass of parent class reference is not visible.

Use of polymorphic members:

  1. Member variable: Compile and run to see the parent class.
  2. Member method: Compile look parent | Type | left, run to see subclass | find objects | right.

Transformation cast

  java is a strongly typed language.

  1. Lifting upward transition :( automatic type)
      Confucius father mounted Case:
      Confucius class class KongZi {int age = 25; void teach () { Analects}; void play () {}} chicken
      Confucius father class class KongZiDie {int age = 45 ; void teach () {}} business
      there is a father for someone who taught Confucius Confucius father outings, loaded into the cosmetic appearance Confucius Confucius father to teach.
      KongZiDie zi = new KongZi (); - Makeup -> upward transition, small type -> subclass, a wide range of types -> parent
      zi.teach (); true to who is Confucius, Analects or so to talk about.
  2. Downcast :( cast)
      Objective: students play together and eat chicken, use a subclass of unique features.
      Cleansing: father type into Confucius Confucius type, range (parent) -> small (subclass)
      small type reference = (small type) wide by reference;
      KongZi Kong = (KongZi) zi and;
      kong.play ();
      after application downward transition into subclasses, subclasses can add method calls.

ClassCastException type conversion abnormality

  • instanceof operator
  • A reference to the instanceof A B determines whether the object is of type B | whether the object is a subclass of B type, if it is returns true, if not it returns false. Compile time checking only if A and B in a succession of even.

Abstract class

  Abstract class: abstract class is an abstract class modified.
  Abstract method: use the abstract key modification method is the idea of pumping method.
  Abstract features of the method:
    No method body
    must exist in the abstract class

note:

  1. An abstract class can not be instantiated. (You can not create an object)
  2. Abstract method of the abstract class must exist.
  3. Abstract methods must be rewritten to use.
  4. There may be an abstract class abstract method, there may be instances method.
  5. SUMMARY abstract class:
      need to subclass
      concrete subclasses: pumping want to override all the abstract methods of the parent class + new methods on demand.
      Abstract subclass: demand override abstract method + new demand method.
  6. An abstract method is overridden if you do not need to be rewritten again, but can also be rewritten.
  7. abstract and not private, final, static and native can not be used together.

Guess you like

Origin blog.csdn.net/qq_41899248/article/details/90757646