Three Basic Characteristics of java

three basic features of object-oriented java are: [ Package], [ inheritance], [ polymorphism]

Package: Object have a clear boundary; division border (objects perform their duties, the reusability of object size, object)

  • Property (bean, pojo): private private, have set and get methods
  • Methods: public or private, public / private
  • Method declaration and implement (interface, implements)

Inheritance: Common in a parent class characteristics into subclasses; parent -> subclass  -> General

  • Keyword: extends
  • java class can have a maximum of one direct superclass, i.e. single inheritance (with simplicity, tree)             
  • tip: java in order to achieve multiple inheritance to achieve through the interface.
  • Parent class can inherit all the properties and methods of subclasses; parent class's private methods are not inherited by subclasses.
  • access modifiers in java
    Access modifier access permission inherit
    private This class Can not inherit
    default This same package type + With buns class can inherit
    protected This type + + different sub-packets with the packet You can inherit
    public public You can inherit

 

    • Construct the object process

     ( 1) allocation of space
       (2) configured to recursively parent object
              a. Initialization properties parent
              b. The parent class constructor
      (3) initialize the property
      (4) call the constructor

  • super
          super () constructor calls the parent class constructor can only appear in the first row
          . super super object that represents the method name of the parent class, the method by which to call the parent class
          Note: In writing class, be sure to write default constructor with no arguments, if a constructor of
                   the first sentence is neither this (), nor is it super () time, where it will implicitly call
                   the constructor of the parent class he no arguments, namely there are implicit super ().

 

Polymorphism: polymorphism Runtime (subclasses of the object referenced in the parent class, for example, Animal a = new Dog, subclass object as a parent class object is used.)

 

      • Polymorphic principles:
               (1) the same type of object
               (2) method of the type defined which reference can only be called by reference
               (3) is running, then to find a method of subclasses based on the actual type of the object covered
                     Examples:
                          There Animal class there are two methods eat () and sleep (), sleep () in 8 hours sleep; there is a subclass of Dog
                          eat () method, sLEEP () method, six hours of sleep, there wangwang () method.
                          Now create Animal a = new Dog (); can not call a.wangwang (), call a.sleep () outputs six hours of sleep.
      • Cast objects:                    Format: instanceof reference type                         reference is referred to as the class objects match the value returned boolean value.                  Usage:                        Animal a = new new Cat ();                        IF (the instanceof a Dog)                        {                           Dog d = (Dog) a;                           d.wangwang ();                         }                  Note: If only Dog d = (Dog) a; run-time error, because a Cat Dog is not (first principle polymorphism)









      • Flexible polymorphic transformation
               (1) for a list of parameters:
                                       public void m (A A) {} class object can be any child class A as a parameter
                used in the return value (2):
                                      public m A () {} this method may return any child class object class a

Guess you like

Origin www.cnblogs.com/codebj/p/10992788.html