java-Object-Oriented summary

Source: Internet
Author: User

Here is a summary of the learning object-oriented knowledge points:

Object Oriented thinking:

To meet the requirements, first go to find out whether there are ready-made classes to implement this function, create objects to invoke, to combine into new classes to implement their own requirements.

in Java , in the class, a class includes member variables, member methods, and construction methods

Construction Method: When creating an object, it is automatically called by the JVM, where the function is to assign a value to the member variable, and you cannot use the static keyword to modify the construction method.

There is also a return statement in the constructor, except that it is generally not written , and all of the methods in Java have a return statement for the stack.

Anonymous objects:
public class demo{public    void Test () {}}//Use an anonymous object to invoke the Demo class public class testdemo{public   static void main (String ar Gs[]) {      

Using the Anonymous object usage scenario: This object is only used once, and this is a very convenient scenario.

Packaging:

The purpose of encapsulation is to hide;

For data security, or to hide the internal implementation process of the method, to provide unique methods to access;

The following is the hiding of member variables, set and set to make the data settings and read:

Package Com.xiaoshitou.demo1;public class Person {private string name;private int age;public string getName () {return name ;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

  

Inherited:

Inheritance: Subclasses inherit non-private members of the parent class (member variables and member methods), note that the construction method cannot be inherited.

Class A{}class B extends a{}

  

The inheritance of a class is only a single inheritance: That is, a subclass can inherit only one parent class at a time;

Interfaces can perform multiple inheritance: an interface can inherit multiple interfaces at the same time

A class can perform multiple inheritance: that is, the parent class can inherit other classes

When using inheritance: The subclass is a special case of the parent class when the inheritance is used; Apple, for example, is one of the fruits, all of which can inherit fruit from the Apple class;

when there are member variables and methods in the subclass that have the same name as the parent class, you can use Super to invoke the members of the parent class

Super can only call members of the immediate parent class and cannot call the parent class's members

Override of Method:

Scenario used: Method overrides are required when the parent class's methods do not meet the requirements of the subclass

Subclasses are the same way as the parent class, and when executed, the method of the subclass is called, which is called rewriting

Only generic methods can be overridden, and construction methods cannot be overridden.

Overridden Format:

1. The method name and argument list must be the same

2, the return value is generally the same, or the return value type of the subclass is a subclass of the parent class method return value type

3. When the method of the parent class has static modification, the subclass method must also be modified with static

4. The permissions of the subclass method cannot be lower than the permissions of the parent class method

The process of instantiating a subclass:

When the JVM executes, once it encounters the new keyword, it opens up space in the heap memory to get the memory address of the space;

Next, according to the members of the class, allocate space for the member variables in the heap, assign the default values, and if the class has a parent class, allocate a space to hold the member variables in the parent class;

Then, the corresponding constructor is invoked to execute on the stack, (that is, if the child class object is created, the constructor of the subclass is called at the time of the stack);

Once the constructor is in the stack, an implicit three-step is performed first:

1, call the parent class constructor, (manual write, is super(parameter list)) is called by default is the parent class no parameter constructor;

2.display initialization for the member variables in the object;

3, if have constructs the code block, executes constructs the code block;

The implicit three-step execution completes before the code in the constructor is executed;

When the code in the constructor finishes executing the stack, the object is created;

Precautions:

1, subclass instantiation, will only create an object (that is, child class object), but also for the parent class member variable allocation space;

2.when the subclass constructor executes, the parent class constructor is executed to assign a value to the parent class member variable, and the subclass constructor is executed after the parent class constructor is executed ;

Because only the object of a subclass is created, if the calling method has an override, it must be called a method that the subclass has overridden.

When a subclass is created, the reason for calling the parent class's constructor is to initialize the member variable for the parent class.

Thisand theSuperKey Words:

This represents the object that is currently being called, and all non-static member methods and variable calls require dependent objects

The role of this:

1. Distinguish local variables from member variables

2. Call other constructor methods in constructor method

The This ( parameter ) must be placed in the first row when other constructor methods are called in the constructor method

Super represents the direct parent class

When a member in a subclass has the same name as a parent class, you can use Super to invoke the members of the parent class

You can use super ( parameter ) to invoke the constructor of the parent class in the constructor method of the subclass, or the parameterless constructor of the parent class if you do not write by default.

This and Super Call construction methods cannot exist at the same time, because all two must be placed in the first row , and when this is used to invoke other constructor methods, the is to take advantage of the indirect invocation of the parent class's construction method

final keyword:

Inal Modified class: The class cannot be inherited

Final Modification Method: The method cannot be overridden

Final Modified variable: The base data type, which becomes a constant and cannot be changed

Reference data type, corresponding to the address of the object cannot be changed

Final cannot modify the construction method

Abstract class:

Abstract Classes: Abstract classes cannot create objects, that is, they cannot be newand must be inherited by subclasses

A class inherits abstract classes that inherit an abstract class, and must all override the abstract method, or the class is abstract

An abstract method exists in a class, and the class must be declared as an abstract class

Abstract classes can exist for all Members ( common variables, member methods, construction methods )

Usage scenarios:

1. There are abstract methods in class

2. Do not want to be created object

Polymorphic :

Polymorphism is: a reference to a parent class to an object of a subclass, or an interface reference to an object that implements a class

Polymorphism Usage Prerequisites: Two classes have an inheritance relationship, or interface and interface implementation class relationships

Benefits: Improved program extensibility, code reuse

Multi-State compilation, all look at the parent class

Polymorphic runtime, in addition to the existence of overridden non-static methods, runs a subclass of overridden methods, and all other members of the parent class run.

Type conversions:

Auto-Transition up: polymorphic is automatic upward transformation

forced downward transformation: You can use instanceof to judge and then cast again, otherwise the type conversion exception is prone to occur

Interface:

Only member variables and member methods in an interface

the member variable defaults to:public static final

the default member method is : Public abstract

These modifiers can be written or not written, and will be automatically added when compiling

An interface can inherit multiple

A class can implement multiple interfaces at the same time

class inheritance and implementation can be done at the same time, but the implementation interface should be placed at the end

Attention:

If a class implements multiple interfaces, or if an interface inherits multiple interfaces, the method name and parameter name cannot be present, and the return value type is different.

Usage scenarios for interfaces and abstract classes:

    1. Abstract class: Two classes have an inheritance relationship
    2. Interface: Two things do not have an inheritance relationship, mainly used to define rules, add functionality
static keyword:

The process of class loading:

Package Packages :

Permission modifiers:

Inner class:

java-Object-Oriented summary

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.