Classes and objects, encapsulation, inheritance, polymorphism

Source: Internet
Author: User

?

    1. Classes and objects

?

    1. Concepts of classes and objects

Class: An objective, abstract, conceptual thing. A class can be defined as a template describing the behavior;

Objects: Objects are concrete, practical, and represent a thing. The object has state and behavior.

A class is a template for an object that is a concrete entity of a class.

  1. Defining classes in Java

    A class can contain any of the following types of variables.

    A) Local variables: Inside a method, a variable defined in a constructor or block is called a local variable. The variable will be declared and initialized in the method, and when the method finishes the variable will be destroyed.

    b) Instance variables: Instance variables are in one class, but outside of any method variables. These variables are instantiated in the class being loaded. An instance variable can be accessed from within any method, constructor, or block of a specific class.

    c) class variables: Class variables are declared in a class, variables outside any method, with the static keyword.

    The class can have any number of methods to access the values of the various methods.

  2. Creating objects

    In Java, the keyword new is used to create a new object.

    There are three steps when creating an object from a class:

    Declaration: Variable Declaration, object type of a variable name.

    Example: the "new" keyword is used to create an object.

    Initialize: The keyword new is followed by a call to a constructor. This call initializes the new object.

  3. Rules for declaring classes, import statements, and package declarations when they are in a source file

    There can only be one with the public class in each source file.

    The source file can have multiple non-public classes.

    The public class name should be the source file and the name that should be appended by. Java at the end. For example, the class name is public class employee{} The source file should be Employee.java.

    If the class is defined in a package, then the packages statement should be the first statement in the source file.

    If the import statements are present, they must be written between the package statement and the class declaration. If there is no package statement, then the import statement should be the first line in the source file.

    The import and package statements will mean the classes in all existing source files. It is not possible to declare different import and/or package statements in different classes of source files.

    The class has several access levels, with different types of classes, abstract classes, final classes, and so on that will be interpreted in the access adornment Fu Zhang.

    In addition to the classes described above, Java also has some special classes called inner classes and anonymous classes.

    Java Package:

    Simply, it is the way of classifying classes and interfaces. In the development of Java applications, hundreds of classes and interfaces will be written, so it is necessary to classify these classes.

    Import statement:

    In Java, the compiler can easily find the source code or class if a fully qualified name, including the package and class name, is a variable. The import statement is a method that gives the appropriate location to the compiler to discover the particular class.

  4. member variables and local variables

    Member variables: defined in the class to describe what the object will be.

    Local variables: defined in a method of a class to temporarily save data in a method.

    Difference: scope is different; initial value is different; priority

    The scope of a local variable is limited to the method that defines it, and the scope of the member variable is visible within the entire class.

    Java gives the member variable an initial value and does not give the local variable the initial value

    When two types of variables have the same name, local variables have higher precedence.

  5. Construction method

    Create a new object using the new+ construction method

    The constructor method is defined as a method in the Java class to initialize the object, with the same constructor as the class name and no return value.

    Public constructor Method name () {

    ???? code block;

    }

    When no construction method is specified, the system automatically generates an parameterless construction method (the default constructor method).

    When there is a specified construction method, no argument, no parameter construction method will automatically add the lunch construction method.

    Constructor method overloading: Multiple methods with the same method name and different parameters.

    The construction method can not only assign values to the properties of the object, but also guarantee a reasonable value to the property of the object.

  6. Static use----statically variable

    Members in Java that are decorated statically are called static members or class members. It is owned by the entire class, not by an object, which is shared by all objects of the class. Static members can be accessed directly by using the class name or by using the object name. Given the particularity of his function, it is more recommended to access it by class name.

    Use static to modify variables, methods, and blocks of code.

    Note: Static members belong to the entire class, and when the system first uses the class, it allocates memory space until the class is unloaded for resource recycling!

  7. static method

    As with static variables, you can also use the static decoration method, which is referred to as a stationary method or a class method. In fact, the main method we've been writing about is static methods.

    A static method can directly invoke a static member in the same class, but not a non-static member directly. ;

    If you want to invoke a fee static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object.

    in normal method members , the same non-static variables and static variables can be accessed directly.

  8. Static use--statically initialize block

    In Java, data can be assigned by initializing blocks.

    public class helloworld{

    ???? String name;//defines a member variable

    ???? Assigning a value to a member variable by initializing a block

    ???? {

    ???????? Name = "Stephen Chow Chi";

    }

}

In the declaration of a class, you can include multiple initialization blocks, which are executed sequentially when you create an instance of the class. If you initialize a block with the static adornment, it is called a static initialization block.

Note: Static initialization blocks are executed only once when the class is loaded, and static initialization blocks can only assign values to static variables and cannot initialize normal member variables.

Execution order: Static initialization block-normal initialization block-constructor method. Static initialization blocks are only executed once when the class is loaded.

    1. Packaging

?

  1. Oop

    Encapsulation, inheritance, polymorphism, and abstraction.

  2. Concept

    Hiding some information about a class inside a class, not allowing direct access to external programs, but using the methods provided by the class to manipulate and access the hidden information.

  3. Benefits

    Data can only be accessed through a prescribed method;

    Hide the strength of the class details, easy to modify and implement.

  4. Implementation steps

    Modify the visibility of the property (set to private);

    Create a Getter/setter method (for reading and writing properties);

    Attribute control statements are added to the Getter/setter method (which determines the legitimacy of the property).

    You can also assign a value (non-standard) to a property using the normal method.

  5. Using packages to manage classes in Java

    The role of the package: Managing Java files, resolving file conflicts of the same name.

    Define packages: Package name.

    Note: The first line of the Java source program must be placed, and the "." can be used between package names. Separated.

    Packages in the system:

    Java. (function). (Class)

    Java.lang. (Class) class containing the Java language Foundation

    Java.util. (Class) contains various tool classes

    java.io. (class) I/O related functions

    Use of the package:

    By using the Import keyword, it is like using a class from another file in a file;

    The naming rules for packages are all lowercase;

    Not only can you load all the files under a package, but you can also load all the files under a specific child package.

  6. Access modifiers

    There are 4 access modifiers in the Java language--the scope of access to properties and methods can be decorated.

    Package (default), private, public, and protected.

    The package is the default protected mode, also known as packet Access, which is used when there are no modifiers. Package access allows domains and methods to be accessed by any method of any class within the same package. (In-package access).

    Private identifies access patterns that represent private domains and methods that can only be accessed by other methods in the same class, implement data hiding, and, if necessary, access private variables through methods. (In-class access).

    The public modifier is used to expose domains and methods so that they can be accessed outside of the package defined by the class. This level is also required for the necessary interface elements in the package and class, and the main () method must be public, and the ToString () method must also be public. It is not common to expose a domain with public unless the domain has been declared final. (Cross-package access).

    The protected modifier provides a way to access the package (with restrictions) from outside the package. Adding protected modifiers before fields and methods does not affect access to them by methods of other classes within the same package. To access the package from outside the package, which contains the protected member's class, you must ensure that the class being accessed is a subclass with the protected member class. That is, you can use this level when you want a class in the package to be reused by classes outside of the package. Should be used with caution in general. (The class in the package is used with caution in the outer class inheritance).

  7. this keyword

    The This keyword represents the current object

    The This keyword is often used when encapsulating the properties of an object

  8. Inner classes in Java

    Definition: A class defined in another class. Classes that contain inner classes are called external classes.

    Function: a) The inner class provides a better encapsulation, so that the inner class is hidden within the outer class, the class is not allowed to be accessed by other classes in the same package, and the method of the inner class can directly access all the data of the external class, including private data; c) The functions implemented by the inner class can be used as a solid line. It is more convenient to use internal classes.

    Including:

    member Inner class;

    static inner class;

    Method inner class;

    Anonymous inner class.

  9. member Inner class

    1. The Inner class is defined inside the Outer class and is equivalent to the position of a member variable of the Outer class, and the Inner class can use any access control, such as public, protected, private, etc.

    2,? The methods defined in the Inner class can directly access data in the Outer class without being affected by the access control, such as direct access to private property A in the Outer class

    3. After defining the member inner class, you must use an external class object to create the inner class object, instead of going directly to the new inner class object, i.e. the inner class object name = Outer class object. New inner class ();

    4. After compiling the program, you will find that two. class files have been generated

    Outer$inner.class

    Outer.class

    Where the second is the. class file for the outer class, the first is the. class file for the inner class, that is, the. class file for the member's inner class is always the case: the outer class name $ internal class name. class

    1. External classes are members and methods that cannot use the inner class directly

    You can first create an object of an inner class and then access its member variables and methods through the objects of the inner class.

    2. If the external class and the inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variable of the external class, you can use the This keyword.

  10. Static Inner class

    Static inner classes are internal classes of static adornments, which are characterized by:

    1. Static inner classes cannot directly access non-static members of an external class, but can be accessed through the new external class (). Members

    2. If the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. static member; If the static member of the outer class is not the same as the member name of the inner class, the static member of the outer class can be called directly through the member name

    3. When creating an object of a static inner class, you do not need an object of an external class, you can directly create an inner class object name = New inner class ();

  11. Method Inner Class

    The inner class of the method is the inner class defined in the method of the outer class, and the inner class of the method is visible only within the method, that is, it can be used only inside the method.

    Note: because the method inner class cannot be used outside the methods of the outer class, the method inner class cannot use the access control and the static modifier.

    1. Inherited

?

    1. Concepts, Benefits

      Class A relationship to a class, "is a" relationship. The

      Subclass has all the properties and methods of the parent class (code reuse)

      Syntax: Class Subclass extends Parent class {

      ???????? Code block

    2. method override

      Definition: If the subclass is not satisfied with the method that inherits the parent class, override the parent class's method, Calls take precedence over the subclass's drop-down method.

      Syntax: The return value type, method name, parameter type, and number are the same as the method inherited by the parent class.

    3. inherits the initialization order

      1, first initializes the parent object

      2, first initializes the properties in the object, and initializes the constructor in the execution. The

      Final keyword uses

      To decorate classes, methods, properties, and variables

      Decorated classes: The class is not allowed to be inherited;

      Decoration Method: The method does not allow overwriting (overriding)

      Modifier Properties: The properties of the class are not implicitly initialized (the class's initialization property must have a value), or it can be assigned in a constructed method (only one option)

      Decorated variable: The variable can only be assigned once, or become a constant. The

    4. Super keyword

      is used inside an object and can represent the parent class object, similar to the This keyword. The constructor of its parent class must be called during the construction of the

      Subclass (implicitly using the Super keyword).

      If the constructor method of the parent class is not explicitly called in the subclass construction method, and the parent class does not have an argument-free constructor, the compilation error occurs.

      Object class

      1, ToString () method

      Returns the hash code (object address string) of the object when the ToString () method is defined in the object class (

      The object's properties can be represented by overriding the ToString () method

      2, and the Equals () method

      Compares whether the object's reference points to the same memory address. The

      3, GetClass () method gets the class object

      Class object that describes the class code information (the class has those attributes, members, and so on) and determines whether the two object types are the same.

    1. Polymorphic

?

  1. Multiple forms of an object

    Referencing polymorphic

    References to the parent class can point to objects of this class;

    A reference to the parent class can point to the object of the child class.

    Polymorphism of the method

    When creating this type of object, the method called is the method of this class;

    When you create an object of a subclass, the method that is called is the method that the subclass overrides or the inherited method.

    Note: A parent class reference cannot reference a unique method of a subclass.

  2. Reference type Conversions

    Up type conversion (implicit/automatic), small type to large type--no risk

    Down type conversion (forced), large type to small type--risky

    The instanceof operator is adopted to solve the type of drinking object and avoid the security problem of type conversion.

  3. Abstract class

    Use the abstract keyword modifier.

    Restriction subclasses must implement some methods, but do not pay attention to implementation details.

    Use rule: a) abstract defines an abstraction class, B) abstract defines an abstraction method, only a declaration, does not need to be implemented; c) A class containing abstract methods is an abstract class; d) Abstract classes can contain ordinary methods; e) Abstract classes cannot be created directly, and reference variables can be defined.

  4. Interface

    1, Concept: Special class, composed of global constants and public abstract methods.

    A class is a concrete implementation, an interface that defines the specifications that a batch needs to follow, an excuse not to care about the internal data of these classes, or the implementation details of the methods in these classes, which stipulate that certain methods must be provided in these classes.

    2, definition: interface keyword.

    3. Basic syntax: [modifier] Interface Interface Name [extends parent interface 1, parent Interface 2 ...]

    {

    ???? 0 to more constant definitions ...

    ???? 0 to the definition of multiple abstract methods ...

    }

    4. Constants/methods: The attributes in the interface are constants, even if the public static final modifier is not added to the definition, the system is automatically added.

    5, using interface: A class can implement one or more interface (implements) keywords. A class in Java can inherit only one parent class, not flexible enough, and can be supplemented by implementing multiple interfaces.

    [modifier] Class name extends parent class implements interface 1, interface 2 ...

    {

    ???? Class Body//If you inherit an abstract class, you need to implement an abstract method of inheritance, to implement an abstract method in an interface.

    }

    If you want to inherit the parent class, the inherited parent class must precede the implementation of the interface.

    6, the interface in the use of the process, but also often with anonymous internal classes with the use.

    Anonymous inner classes are internal classes that do not have names, and are used to focus on implementations rather than on the name of the implementation class.

    Interface I = new Interface () {

    ???? public void Method () {

    ???????? System.out.println ("niming");

    ????}

    }

Classes and objects, encapsulation, inheritance, polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.