Overload override inherited polymorphism encapsulation Abstract

Four basic features of java: Abstract encapsulation inheritance polymorphism

First, the property package
data 1. class [hide] access to the main properties of the control
principle: the hidden attribute, if the need to access a property, providing public access to its methods.   
public any object can be accessed   
private access only in the current class
protected current class and child objects can be accessed
if the property plus a private modification, then the property in other classes can access
the package 2. Methods: The methods of the class in order to improve the concrete realization of hidden code reuse
3. how to obtain private property?
int Age Private;
// encapsulate the private attribute set method.
void the setAge public (int Age) {
    this.age = Age;
}
// get access to the private property by the method of the value of
public int getAge () {
    return Age;
}

two, abstract
1. abstraction: a class does not contain sufficient the information to describe a specific object, based on this abstract class
with modified abstract class is an abstract class
characteristics 2. abstract classes:
(1) can not be instantiated (an object can not be new)
(2) is not necessarily abstract class abstract methods comprise, but contains a class abstract method, but it certainly is an abstract class  
(3) method abstract class merely declared abstract does not have a body.
All abstract methods (4) sub-class inherits the abstract class if not an abstract class must override the parent class
(5) construction method, class method (a modified static method) can not be declared abstract methods.
3. The difference between abstract classes and interfaces:
1) The method focuses on the interface, the abstract class focused on property.
2) interface with multiple inheritance only abstract no constructor method
3) is configured with a single inheritance abstract class may have non-abstract methods abstract methods and

the same point: could not be instantiated abstract method comprises
an interface Detailed: public interface A {} Inheritance Interface keyword implements
the interface: member variable: public static final type must be explicitly initialized
              members: there is no method body
                                default is public abstract
 can not have any form of code blocks
non-abstract method 4. the abstract class how to call?
If staitc modified directly by the class name. Method name
if a non-static modification, there must be a succession of non-abstract class, method name to call if the object name of the subclass.

Third, inheritance: subclass inherits behavior and characteristics of the parent class
1. Single inheritance.
2. What will be the parent class inheritance:
1) similar package father and son, the child class inherits the parent class public, protected, and default modifier can be inherited.
2) different packages, the default modifier can not be inherited.
3) constructor can not be inherited.
4) methods and instance variables can be inherited.
5) a subclass constructor implicitly call the parent class's constructor with no arguments is, if the parent class constructor with no arguments is not defined, it has subclasses must call the displayed argument constructor. (By Super (...))
Example: public class Animal () {
       Private String name;
       public Animal (String name) {name = this.name;}}
      public class Animal Dog the extends () {
      public Dog () {Super ( "dog");} // have explicitly call argument constructor
          }
. 6) and the this super () in the constructor must be the first statement
7) constructor class instance object, the object comprising a child All class instance variable and the parent class, instance method also includes all instances of subclasses and methods of the parent class. Be sure to visit the subclass constructor of the parent class.
Private methods of the parent class 3. How subclass indirect call?
Public method in the parent class, this. The method name to call private methods of the parent class, subclass object and then call this method the public can have access to this private method

4. child the parent class attributes such as access to property the same name, see Object is the parent class or subclass, class objects to see the position of the public methods is called
9) subclass private method parent private methods have the same name, such as calling the method, see the private methods position is called, as in the parent class, called a parent class the method, as in the subclass, the method is called a subclass;
public class Test2 {
public static void main (String [] args) throws Exception {
    new new a () printPerson ();. // a
    B new new () printPerson ();. A //
}
}
class A {
    public void printPerson () {System.out.println (getInfo ());}
    Private getInfo String () {return "A";}
}
class B A {the extends
    public String getInfo () {return "B";}}




10) sub-class parent class with static methods of the same name static method, a static method of the parent class subclasses static shielding methods. The call the static method, the type of object is instantiated when viewed declared as parent class declarations, call the parent class is a static method, on the contrary is a subclass of the static method. (Static methods can not be rewritten)
.

Fourth, the rewriting
1. Method parent child exactly. Rewrite occurs in a child-parent class.
2. Features: method name, parameter list are identical, the abnormality can not be enlarged, rights can not be reduced.
3.static override modification method exists, a static method with the same name can define a subclass of the parent class. (Not covered)
4. parent class static methods can not be covered with a non-static method subclasses
5. The non-static methods of the parent class you can not be overridden by subclasses to be static;
static methods only when the class definition of the static methods already loaded class is loaded at
6, abstract methods:
      a method of abstract parent class can be subclassed to cover a non-abstract methods: sub. abstract parent class implementation method;
      B abstract parent class can be subclassed to cover the abstract methods: abstract method declarations re parent class;
      . C non-abstract parent class can be subclassed to cover the abstract method;


five, overloading
the same class, the same method name, a plurality of different parameter list does not discuss methods return values, called overloaded methods.
Benefits: the user calls and use functions provide great flexibility

over six states
have different manifestations of the same behavior of
the three necessary conditions for the existence of polymorphism: 1 2 override the inherited reference point to the parent class 3 subtypes object
reference pointing object parent class subclass

polymorphism occurs between the two classes of objects have a hierarchical relationship,
    the syntax: variable name = new parent class subclasses ();
             the interface implementation class variable name = new (); parent references to objects that implement the interface class

 using an object method calls polymorphic:
        compile: see if there is a parent class in this method whether there is / interface method of this
        run: performing the method of implementation class









Guess you like

Origin www.cnblogs.com/yxj808/p/11953272.html