Java organization (2) Object-oriented and java organization object-oriented

Source: Internet
Author: User

Java organization (2) Object-oriented and java organization object-oriented
Java object-oriented

Static block: use static statement. It is executed only once when the JVM loads the class.

Constructor block: defined directly by {} in the class. It is executed every time an object is created.

Execution sequence priority: static block> main ()> construction block> Construction Method

 

All Java programs are organized by class.

Object-oriented features: encapsulation, inheritance, and Polymorphism

Encapsulation

Prevent the code and data of this class from being randomly accessed by the Code defined by the external class.

The member variables are generally set to private and accessed using setter and getter.

This keyword is used for the difference between a member variable and a local variable. this keyword can only be used within a method.

Inheritance

Upward Transformation: Generally, the base class is on the top, while the export class is scattered in the lower part. Therefore, to transform to a base class is to move up in the inheritance diagram, that is, to transform up and to generalize the type.

Downward Transformation: Same as above. Convert down to a more specific type.

Upward transformation is safe, and downward transformation is almost insecure. runtime check is required, which can be solved through the parameterized type mechanism, that is, generic type.

 

The extends keyword can be used to declare that a class is inherited from another class.

The implements keyword can be used to make java have the multi-inheritance feature in disguise. When the scope is the class inheritance interface, multiple interfaces can be inherited at the same time (interfaces and interfaces are separated by commas ).

Class subclass extends parent class {}

All classes are inherited from java. lang. Object.

Super Keyword: implements access to the parent class members to reference the parent class of the current object.

This Keyword: point to your own reference.

The final keyword Declaration class can define the class as non-inherited

 

The subclass cannot inherit the constructor (constructor or constructor) of the parent class, but the constructor of the parent class has parameters, you must explicitly call the constructor of the parent class using the super keyword in the constructor of the subclass and configure it with an appropriate parameter list.

If the parent class has no parameter constructor, it is not necessary to call the parent class constructor with super in the constructor of the subclass. If the super keyword is not used, the system automatically calls the non-argument constructor of the parent class.

If the class does not include the public or protected constructor, it cannot be inherited.

 

Polymorphism

Polymorphism is the ability of a single behavior to have multiple different forms or forms.

There are three necessary conditions for Polymorphism: inheritance, rewriting, parent class reference pointing to subclass object

Implementation of polymorphism: rewrite, interface, abstract class, and abstract Method

Rewrite and reload

Override)Is the implementation process of the method that the subclass allows access to the parent class. The return value and the form parameter cannot be changed. That is, the shell remains unchanged and the core is overwritten!

The return type, method name, parameter type, and number must be the same as the parent class.

The override method cannot throw a new check exception or a broader exception than the declared by the override method. For example, a method of the parent class declares an IOException check, but an Exception cannot be thrown when this method is rewritten because Exception is the parent class of IOException, only subclass exceptions with IOException can be thrown.

The access permission cannot be lower than the access permission of the override method in the parent class.

When the override method of the parent class needs to be called in the subclass, the super keyword is used.

Overloading)Is in a class, the method name is the same, and the parameters are different. The return types can be the same or different.

Each overloaded method (or constructor) must have a unique list of parameter types.

The most common part is the overload of the constructor.

 

Overriding and Overloading are different manifestations of java polymorphism. Rewriting is a manifestation of polymorphism between parent class and Child class, overloading can be understood as the specific manifestation of polymorphism.

Abstract class

Use abstract Modifier

Abstract classes can not instantiate objects, but other functions of the class still exist. member variables, member methods, and constructor methods are accessed in the same way as normal classes.

Abstract classes cannot instantiate objects, so they must be inherited before they can be used.

Abstract keywords can also be used to declare abstract methods. abstract METHODS only contain one method name, but no method body. The abstract method is not defined. The method name is followed by a semicolon instead of curly brackets.

If a class contains abstract methods, the class must be an abstract class.

Any subclass must override the abstract method of the parent class or declare itself as an abstract class.

Constructor. Class methods (Methods modified using static) cannot be declared as abstract methods.

Interface

Interface name [extends parent interface 1, parent interface 2,…] // Multi-inheritance allowed

{0 ~ Multiple constants are defined // The variable is implicitly public staticfinal.

0 ~ Multiple abstract method definitions... // Only public abstract can be used for modification.

}

Class name extends parent class imlements interface 1, interface 2 ,... {...}

No constructor for the interface

Interface supports multi-Inheritance

The interface cannot contain member variables except static final variables.

The interface type can be used to declare a variable. They can be a null pointer or bound to an object implemented using this interface.

Mark Interface

The most common inherited interface is an interface that does not contain any methods.

An identifier interface is an interface that does not have any methods and attributes. It only indicates that its class belongs to a specific type for other code to test and allow some operations.

Identity interface function: Simply put, an object is labeled (stamped) so that the object has some or some privileges.

Differences between abstract classes and interfaces

1. The methods in the abstract class can have the method body, which is the specific function that can implement the method, but the methods in the interface cannot.

2. The member variables in the abstract class can be of various types, while the member variables in the interface can only be of the public static final type.

3. The interface cannot contain static code blocks and static methods (modified using static methods). abstract classes can contain static code blocks and static methods.

4. A class can inherit only one abstract class, but a class can implement multiple interfaces.

Java 1.8 supports defining static methods in interfaces and default methods.


C ++

Java

Virtual functions (can be referenced by base classes)

Common functions

Pure virtual functions

Abstract Functions

Abstract class

Abstract class

Virtual base class

Interface

 

Packet

Java uses a package mechanism to prevent naming conflicts and access control. It provides search and locating classes, interfaces, enumerations, and annotation.

Package pkg1 [. pkg2 [. pkg3…]; // Create

Import package1 [. package2…]. (Classname | *); // use

Packages in the system

Java. (function). Class

Java. lang. (class) contains the basic class of the java language

Java. util. (class) contains various tool classes in java

Java. io. (class) contains function classes related to input and output.

Construction and destruction

L The non-argument constructor is the default constructor, and the system will not automatically create the default constructor.

L you can use this to call a constructor (two constructor cannot be called, similar to super to call the parent constructor). You must place the call at the starting position.

L finalize () method. This method is automatically called before the Virtual Machine releases the object space. It cannot be called directly. It is not equal to the destructor. Garbage collection is only related to the memory. Try not to use the finalize method.

L static block: the static block is declared. It is executed only once when the JVM loads the class.

Constructor block: defined directly by {} in the class. It is executed every time an object is created.

Execution sequence priority: static block> main ()> construction block> Construction Method

Parent class static members and static blocks, subclass static members and static blocks, parent class constructor, subclass Constructor

 

View comments

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.