Java-Object Oriented

Source: Internet
Author: User

object class:

 11 methods:
  First class method: ToString, equals, hashcode;
  the second type of method: Clone, Finalize, GetClass;
  Third class method: 3 Wait, notify, Notifyall;
  method:
  ToString: Used to describe information about the current object; it is generally overridden; When you print a class object, the ToString method of the object is automatically called;
  equals: Compares whether an object's reference points to the same memory address, and generally compares two objects to see if their values are consistent. So you want to rewrite it;

transformation:

  downward transformation: Big turn small; the parent class type (reference, or object) variable is assigned to a variable of subclass type, you need to enforce type conversion; A variable of subclass type can use properties and methods peculiar to subclasses;
  can use a reference variable instanceof the class name to determine whether the object pointed to by that reference variable belongs to that class or subclass of that class;
 -down transition possible type conversion error condition:
  Compile times Error: Two classes that do not have an inheritance relationship are converted to and from each other;
  Run times Error: When down transformation, The reference variable of the parent class does not refer to the subclass object first, in which case the compilation does not error and the runtime throws java.lang.ClassCastException;

Dynamic binding:
1, explanation: During the run (but not the compilation period) to determine the actual type of the referenced object, according to its actual type call its corresponding method;
2, the role: to increase the scalability of the program;
3, through the multi-state to achieve the scalability of the program;

Construction Method:
1, Function: Create and initialize the object;
2. Syntax:
the method name must be the same as the class name;
no return type;
cannot be modified by static, final, synchronized, abstract, and native, but only by four kinds of access limiters;
3, the construction method of overloading:
When an object is created through the new statement, the object may have different initialization behavior under different conditions, and multiple initialization behavior of the object can be expressed by overloading the construction method;
When you use the this statement to invoke other construction methods, you must adhere to the following syntax rules:
must be called in the construction method of this class;
must be placed in the first row of the construction method;
must be explicitly called with the This keyword;
4, the default construction method:
in two ways: implicit default construction method, the default construction method explicitly defined by the program (can be any access level);
If you write a class without explicitly writing a constructor, the system provides a default (parameterless, nothing-specific) constructor, which sets all instance fields to default values, and if you explicitly write a constructor when you write a class, you can do so. The system will no longer provide a default parameterless constructor, and if you need a parameterless constructor at this point, you must manually add another parameterless constructor;
5. The subclass calls the constructor of the parent class:
The constructor method of the subclass must be called during the construction of its parent class;
If the constructor method for calling the parent class is not displayed in the constructor of the subclass, the system defaults to calling the parent class's non-parametric construction method;
If the construction method in the subclass does not explicitly call the constructor of the parent class, and the parent class does not have an argument-free constructor, then the compilation error occurs;
when you invoke the constructor method of a parent class with a super statement, you must adhere to the following syntax rules:
The construction method of the parent class must be called in the constructor of the subclass;
must be placed in the first row of the subclass construction method;
The Parent class construction method must be explicitly called using the Super keyword;
When you create an object of a subclass, the Java Virtual machine first executes the constructor of the parent class, and then executes the constructor of the subclass. In the case of multilevel inheritance, starting from the topmost parent of the inheritance tree, the constructor of each class is executed in turn, which guarantees that the instance variables inherited from all direct or indirect parent classes are properly initialized by the child class object;
The construction method of the subclass must be called by the constructor of the parent class (either implicitly or explicitly);
6, the scope of the construction method:
The constructor method can only be called in the following ways:
other construction methods of the current class call it through the this statement;
The construction method of the subclass of the current class is called by the Super statement;
call it in the program through the new statement;
7. Private Modified construction Method:
when the constructor method is private, it means that it can only be accessed in the current class: it is called through the this statement in other constructor methods of the current class, and it can be called through the new statement in the member method of the current class .
in one of the following situations, you can declare all the constructor methods of a class as private types:
This class contains only static methods that are called by other programs, and there are no instance methods;
prohibit this class from being inherited (but it can still be instantiated);

Final keyword:
1, the role: to ensure that the modified part of the content can no longer be changed;
2. Objects that can be modified: class, method, member variable, local variable (local variable);
3. Final modified variable:
any member variable or local variable declared final is called a final variable; the final variable is often used with the static keyword as a constant;
Syntax Rules:
A variable of the final type must be explicitly initialized and can only be initialized once;
The final member variable must be initialized at the time of declaration or in the constructor, and cannot be initialized elsewhere;
final member variables are initialized at the time of Declaration, Declaration and initialization must be completed at the same time (together) and cannot be separated (divided into two statements); But local variables can be written like this;
4. Final Modification Method:
The final modified method can not be overridden by the method of the quilt class, that is, the subclass is not able to exist and the parent class is identical to the method;
5. Final Modified class:
the final modified class cannot be inherited;
There are many classes in Java that are final, such as String, Interger, and other wrapper classes;
A final decorated class whose member variables can be final or non-final; The method defaults to final (the method is forced to final, because the class cannot be inherited, so the method is not overridden);

Interface:
1, Function: is used to be inherited, is implemented; therefore, modifiers are generally recommended to use public or default; You cannot use private and protected to modify interfaces;
2. Syntax format:
is a collection of abstract methods and constant values;
Static constants: Use the public static final modifier variable, you can not explicitly write (default plus) these three modifiers;
Abstract Method: Use the public abstract modification method, you can not explicitly write (default plus) these two modifiers;
3. Characteristics:
interfaces can be multiple implementations;
interface can be multiple inheritance;
You can modify variables by using public static final (which is not explicitly written out by default), public only, final only, and static in several ways;
You can use public abstract (which is not explicitly written out by default), only abstract, public only, and only several ways to modify the method;
4, the implementation of the interface:
when a class implements an interface, the class implements all the methods in the interface, otherwise the class must be declared as an abstract class;
A class cannot throw a mandatory exception when implementing an interface's method, only in an interface, or in an abstract class that inherits an interface .
The class should maintain a consistent method name when overriding the method, and should maintain the same or compatible return value type (usually not changed, adding only the method body);
5. Marking interface:
The identity interface is an interface that does not have any methods and properties, it merely indicates that its class belongs to a particular type; The simple image is to mark an object so that the object has some or some privilege;
the tag interface is mainly used for the following two purposes:
establish a common parent interface: You can use a markup interface to establish a parent interface for a set of interfaces;
to add a data type to a class: The class that implements the markup interface does not need to define any interface methods, but the class becomes an interface type through polymorphism;
6, the difference between the interface and the class:
interfaces cannot be used to instantiate objects (nor to construct methods);
all the methods in the interface must be abstract,(this is now the wrong word)
an interface cannot contain member variables, except static and final variables, which appear to be defined as normal member variables, but final static is not explicitly written out.
The interface supports multiple inheritance, and the class only supports single inheritance;
7. The difference between an interface and an abstract class:

Abstract class:
1, Introduction: In the object-oriented concept, all objects are depicted by the class, but in turn, not all classes are used to depict objects, if a class does not contain enough information to depict a specific object, such a class is abstract class;
2, Function: Abstract class can not instantiate objects, so the abstract class must be inherited, can be used (to achieve expansion and polymorphism);
3, the use of the scene:
in some cases, a parent class simply knows how its subclasses should be, but does not know exactly how these classes implement these methods;
Abstract an abstract class is abstracted from multiple classes with the same characteristics, and this abstract class is used as a template for subclasses, thus avoiding the accessibility of sub-class design;
4, the characteristics of abstract class:
the class containing the abstract method must be (to be declared) an abstract class;
a class without an abstract method can also be declared as an abstract class;
Abstract classes can contain specific data and specific methods (methods can be implemented);
Abstract classes cannot be created directly (instantiated), but can define reference variables (non-abstract subclass objects), i.e. rely on subclasses to accomplish certain functions;
inherit the subclass of the abstract class:
If you implement only the abstract method of the base class part, the subclass must also be declared as an abstract class;
If you implement all of the abstract methods of the base class, then the subclass does not have to be declared abstract class;

A class can only be decorated with the following three modifiers:public abstract final;


1, Introduction: Polymorphism is the same behavior has a number of different manifestations or forms of ability; Polymorphism is the embodiment of multiple forms of objects (to achieve a certain function, different subclasses have different implementations);
2, three necessary conditions to achieve polymorphism: inheritance, override, parent class reference subclass object;
3, Reference type conversions involved in polymorphism: upward and downward transformation;

Package:
1, the package declaration should be in the first line of the source file;
2, each source file can only have one package declaration, each type of this file is applied to it;
3. If a package declaration is not used in a source file, then the classes, functions, enumerations, annotations, etc. will be placed in an unnamed package (unnamed);
4, in the Java source file should be located in the import statement after the package statement, all classes before the definition, can not, or can have more than one;
5, if in a package, a class wants to use another class in this package, then the package name can be omitted (do not need to be introduced through the import statement);
6, usually, a company uses its Internet domain name in reverse form as its package name;

override (Override):
1, Function: If the subclass is not satisfied with the method of inheriting the parent class, it can realize its own requirement by overriding the method of inheriting the parent class;
2. Grammar rules:
The return value type, method name, parameter type and number and order of the methods in the subclass are the same as the methods inherited by the parent class;
the access permission of a subclass method cannot be higher than the access permission of the overridden method in the parent class;
meet the above two can be called rewrite;
3. Precautions:
a method declared as final cannot be overridden;
a method declared as static cannot be overridden, but can be declared again (as if it were a static method of the parent class, but it has no relationship, hiding the static methods inherited by the parent class directly);
The construction method cannot be overridden;
If you cannot inherit a method, you cannot override this method;
subclasses and parent classes are in the same package, then subclasses can override all methods of the parent class except for the method declared private and final;
subclasses and parent classes are not in the same package, then subclasses can only override non-final methods that are declared public and protected by the parent class;
overridden methods can throw any non-mandatory exception, regardless of whether the overridden method throws an exception, but the overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than the overridden method declaration, or vice versa;
subclasses can invoke the method of the parent class through the Super keyword;

Overload (Overload):
1. Overloading rules:
In a class, the method name (must) is the same, and the parameter (must) is different; the return type and the access qualifier can be the same or different;
Overloaded methods can declare new or broader check exceptions;
Method can be overloaded in the same class or in a subclass;

Overrides and overloads for comparison:

Local variables cannot be added with access qualifiers;

Inner class:
definition: A class defined in another class;
reasons for using internal classes:
The inner class method can access data in the scope of the class (where the class should refer to an external class), including private data;
inner classes can be hidden from other classes in the same package;
using anonymous inner classes is convenient when you want to define a callback function and don't want to write a lot of code.
Grammar Summary:
The inner class can access both its own data field and the data field of the external class object that created it .
an object of an inner class always has an implicit reference to the outer class object that created it, which is not visible in the definition of the inner class;
The inner class does not need to access the private properties of the external class through the public method of the external class, which can be accessed directly;
Classification of inner classes:
member Inner class:
method Inner Class (Local inner Class): The first is a member inner class, but this inner class is used only once in one method of an outer class, in which case the inner class can be defined in a method, that is, the method inner class, and the method inner class cannot use public or private (protected and Not default) Access restrictions (can only be declared using abstract or final), whose scope is limited to this method; advantages: The outside world can be completely hidden, only the external class of this method can access it, no other class and no way to know its existence ; You can access not only the external classes that contain them, but also local variables, but this local variable must be declared final;
static inner class:
Merit (function): Just to hide a class inside another class, you do not need an inner class to refer to an external class object; To do this, you can declare an inner class as static in order to cancel the resulting reference;
anonymous inner class:
meaning: You only need to create an object of the inner class, without knowing the name of the inner class;
premise: Must inherit a parent class or implement an interface;
Advantage (function): can only be used once, it is usually used to simplify code writing;
application: The most common situation is in the implementation of multithreading, because to implement multithreading must inherit the thread class or inherit the Runnable interface;
Syntax Restrictions:
The constructor can be passed to the constructor of the superclass, but not any constructor parameters when the inner class implements the interface .
Anonymous inner classes can be defined directly in external classes or in methods of external classes.

callback function:
You go to a shop to buy things, just the things you want to have no goods, so you left your phone in the shop clerk, after a few days the store has goods, the clerk called your phone,
Then you get the call and go to the store to pick up the goods. In this example, your phone number is called a callback function, you leave the phone to the clerk called the registration callback function,
There was a shipment later. Called the event that triggered the callback association, the clerk called you called the callback function, and you went to the store to fetch the goods called the response callback event;

When defining a variable in a method, either the modifier (including the access qualifier, static modifier) or only the final modifier is not used;

Java-Object Oriented

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.