Encapsulation, inheritance and polymorphism, rewriting, and other basic review of overloading

Overloading Overloading

1. The same method name, parameters different (different parameters including the order of, different types and number);

The method returns the value of the same or different types, it is not used to distinguish the type of the return value of overloaded functions;

3. The method is overloaded and overloaded methods may be different throw exception;

4. Overload polymorphism reflects the characteristics of: different specific call that is determined according to the method of overload parameters passed when calling the method, which is polymorphism;

public class Dog {
    Dog() {
        this.bark();
    }
    // Bark () is overloaded method 
    void Bark () {
        System.out.println (\ "! Barking NO \");
          the this .bark (\ "FEMALE \", 3.4);
     }
     // Note: the return value are the same overloaded methods, 
    void Bark (String m , Double L) {
        System.out.println (\ 'Barking Dog A \! ");
         The this .bark (. 5, \" China \ ");
     }
     // can not distinguish overloaded methods return values, but only to" Parameter Type "and "class name" to distinguish 
    void Bark ( int A, n-String) {
        System.out.println(\"a howling dog\");
    }

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.bark();
        dog.bark(\"male\", \"yellow\");
        dog.bark(5, \"China\");
}               

 

Rewrite overriding

Subclasses may not want heavy intact inherited methods of the parent class, but to make certain changes, which requires the method of - 1. A method inherited from the parent class to redefine (override) write;

2. The method overridden method must have the same name and is overridden method, the parameter list and return type, otherwise it is overloaded instead of rewriting;

3. The method of rewriting only exist in the inheritance of methods and can override the parent non-proprietary;

4. The method does not override the use of more stringent than overwriting the access method (public> protected> default> private);

5. The method of rewriting a new check must not throw exceptions or broader than the method of overwriting checked exceptions (Exception> IOException);

1.     class Animal {  
 2 . String name;  
 3.         void EAT () {  
 4. System.out.println ( "eat" );  
 . 5 .}  
 6. The         void Shout () {  
 7. The System.out.println ( " my animal " );  
 . 8 .}  
 . 9 .}  
 10 .      
 11. The     class dog the extends animal {  
 12. The         void EAT () {  
 13. System.out.println (" dog gnaws bone " );  
 14 .}  
 15. a         void shout(){  
16.            System.out.println("旺旺叫!");  
17.        }  
18.        void eat(String food){  
19.            System.out.println("吃:"+food);  
20.        }  
21.    }  
22.    class Demo{  
23.        public static void main(String[] args){  
24.            Dog d = new Dog();  
25.            d.shout();  
26.            d.eat();  
27.       }
28.    }

Note: overloading and rewrite nothing to do.

 

Object-oriented three characteristics: inheritance, polymorphism and encapsulation.

 

Inheritance extends

1, sub-class parent class can be obtained in addition to all the properties and methods of construction method;

2, java classes only single inheritance, no multiple inheritance, java in multiple inheritance can be implemented through an interface;

3, inherited the essence of abstraction: the class is an abstract object, inheritance is a group of abstract class;

4, when the definition of a class if there is no call extends, it is the parent java.long.Object;

5, a different name: super class, a parent class, the base class, subclass, derived class;

6, you could use a combination of inheritance, the combination can still achieve code reuse. difference:

                                       is-a relationship: Using inheritance;

                                       has-a relationship: combination;

 

Package encapsulation

1, the package is also called hidden; good package easy to modify the internal code, data integrity may be detected.

2, hiding the internal complexity of the subject, open to the public only simple interfaces for external calls, thus improving system scalability, maintainability.

3, high cohesion: internal data operation details of a class yourself, does not allow external interference;

      Low Coupling: A method to expose only a small amount of external use;

4, a process attribute class:

           1. Unless private-- generally used will determine the subclass inherits attributes;

           2. Provide the corresponding get / set methods to access the associated property. The method is usually public, thereby providing a read operation on the property.

      Hope that other method invocation, of course, it is public;

      Access control character used to achieve encapsulation:

 

The same class

The same package

Subclass

All categories

private

#

 

 

 

Default ie without modifiers

#

#

 

 

protected

#

#

#

 

public

#

#

#

#

 

Polymorphism

1. Polymorphism is the same operation (method) is applied to the different objects can have different interpretations, produce different execution result;

2. A multi-state technique: dynamic binding : determining during the execution of the actual type of the referenced object, according to the corresponding type of the actual method invocation; plainly is implemented by polymorphic interface inherit the parent classes, override method , for the same class method overloading.

3. The method of polymorphic manifestation there are three necessary conditions: very important .

           1 ) have inherited; 2) The method must be rewritten; 3) references to the parent class subclass object;

public class A {

      public String show(D obj){

           return "A and D";

      }

      public String show(A obj){

           return "A and A";

      }

}

class B extends A{

      public String show(B obj){

           return "B and B";

      }

      public String show(A obj){

           return "B and A";

      }

}

public class Test {

      public static void main(String[] args) {

            A a1 = new A(); 

        A a2 = new B(); 

        B b = new B(); 

        D d = new D();  

        System.out.println (a1.show (B)); // Results: A and A    

        System.out.println (a1.show (D)); // Results: A and D     

        System.out.println (A2. Show (B)); // results: A and B     

        System.out.println (a2.show (D)); // results: A and D    

        System.out.println (b.show (B)); // results: B and B      

        System.out.println (b.show (D)); // results: A and D    

      }

}

 

Object-oriented five principles:

1. The single responsibility principle : each type (including interfaces and abstract classes) function requires a single, responsible for only one thing;

2. Open Closed Principle : a module in terms of change should be closed, in terms of the expansion should be open. For example, a network module, originally only server functionality, and now I want to join client functionality.

3. Replace principle : subclass can replace the parent class, and appear anywhere in the parent class that can occur;

4. Dependency Inversion principle : to rely on abstract and do not depend on the specific. Simply put: that is to abstract-oriented, programming to interfaces, not to achieve the program, or reduce the degree of coupling between the client and the implementation module;

      In the process for the development, dependent on the lower layer an upper layer, the lower layer when dramatic changes, but also changes in the upper layer, thus resulting in a high coupling between modules, reducing reusability.

      Object-oriented solve the problem: Under normal circumstances, the chances of abstract change is very small, so that the user program is dependent on the abstract, also depends on the abstract implementation details, even if the implementation details of constant change, constant as long as the abstract, the client program will not have to Variety;

5. Interface segregation principle : the different modules to be isolated by the interface, rather than by the specific type strongly coupled;

 

Guess you like

Origin www.cnblogs.com/panweiwei/p/11909252.html