Java-java Object-Oriented programming

Source: Internet
Author: User
Tags modifiers

2017-10-09 17:23:52

In object-oriented technology, consider a thing in the objective world as an object, such as a Mr. Zhang, who is an object. Each object has its own properties and behavior. Mr. Zhang's attributes include name, gender, height, etc., and his behavior involves driving, reading, running and so on. From the point of view of program design, the properties of things can be described by variables, and behavior is reflected by methods.

First, the core technology of object-oriented design

The core technology of object-oriented programming is encapsulation, inheritance and polymorphism.

    • Packaging

Class is the basic unit of encapsulation. Encapsulation enables you to hide the implementation details of a class, or to avoid direct manipulation of attributes in a class. Variables in a class can be accessed through the public interface of the class without having to know how the interface is implemented. As long as the name and parameters of this method are not changed, even if the variables in the class are redefined, or the code of the method is modified, access to the variables in the class will not be affected. For example, if you have a sound card installed on your computer, you do not need to know the internal structure of the sound card, and you do not need to know how it is implemented, because the inheritance circuitry that implements the sound card function is encapsulated. You just need to know where the sound card should be installed on the computer motherboard, other devices such as speakers, optical drive How to connect with the sound card can be.

    • Inherited

Inheritance is the reuse and expansion of the functionality of a class. By the inheritance of a class, a new subclass is created, and the subclass has the function of inheriting from a class, and can define its own variables and methods to produce new functions.

    • Polymorphism

Polymorphism refers to the same name of the method, but the implementation is different. That is, "same interface, multiple methods." If there are three methods for calculating the area of circles, rectangles, and triangles, their names can all be called areas.

II. structure of the class

Java is a fully object-oriented programming language. All of his property types and methods are encapsulated in classes, Java inherits a bit of C + +, but discards those vague, complex, and error-prone features. The object-oriented features implemented by Java can reduce the complexity of the program, realize the reusability of code and improve the efficiency of operation.

    • Declaration format of the class

[Modifier]class class name [extends superclass name][implements interface Name]

Class Body

Modifiers contain two types of access rights, either default or public. If there are multiple classes in a source file, only one class can have permissions that are public, and the permissions of the remaining classes must be the default. The public class should have the same name as the file, and the main function should be placed in the class. The public permission in Java means that all classes are accessible, while the default access is to allow access only to classes of the same package.

final:final-Modified classes are the final class, and this class cannot be inherited.

Abstract: Abstraction class, cannot instantiate

    • Member variables

[Modifier] [Static] [Final] [transient] variable type variable name;

Modifier: Default, Public,protected,private

Static: Statically variable

Final: constant

Transient: Temp variable

    • Member Methods

[Modifier] Return type method name (parameter table) [throws < exception class;]

{

Method body;

}

The modifiers here contain access to four types, namely: Default, Public,protected,private.

Static: Statically method

Abstract: Abstraction method (method without method body)

Final: The End method (methods that cannot be overridden by the class)

Throws: Throwing Exceptions

    • Description of access rights

Public: All classes have access to the

Protected: Subclasses of different packages can access

Default: The same package can be accessed

Private: The same class can access

Iii. Construction and initialization of objects and destruction of objects

    • Construction method

The construction method does not return a value, and the constructor name should be the same as this class.

For example: Date (int m,int n) {...}

The corresponding construction method is automatically called when the object is created.

    • Destruction of objects

The life cycle of an object refers to the process of creating, using, and destroying objects. Object destruction refers to releasing the resources that the object consumes when the object is finished using it.

Java can automatically determine whether an object is being used, and automatically destroys objects that are not in use, and reclaims the resources occupied by the object. In this mode we do not know the exact time of object destruction.

In addition, we can define the Finalize () method in the program so that the object executes the method before it is destroyed. The basic format of the method is as follows:

[Modifier]void Finalize () {}

Java provides the Finalize() method, which is called Finalize () when the garbage collector prepares to release memory.

(1) . Objects are not necessarily recycled.

(2). Garbage collection is not a destructor.

(3). Garbage collection is only related to memory.

(4). Garbage collection and Finalize () are unreliable, as long as the JVM is not running out of memory to the point where it will not waste time on garbage collection.

Iv. Static Members

There is a special member in the class that does not belong to a single object, but belongs to the entire class. In addition,static in Java cannot be used to modify local variables . Static member functions can only access static member variables.

V. Inheritance of classes

If you want to extend the functionality of the original program, you can modify the original class or add a new class, but this does not conform to the open and closed principle. It also affects the code of the original class or causes the code to be duplicated.

A good approach is to reuse existing code through inheritance, while adding new code to extend the functionality.

Inheritance is an important method of object-oriented design, the inherited class is called a superclass, and a new class derived from a superclass is called a subclass. The subclass contains two parts, one is the variables and methods inherited from the superclass, and one is its own new added method and variable.

Only single-inheritance is supported in Java, and multiple inheritance is not supported, so a class can have only one superclass.

    • Declaring a subclass

[Modifier]class Subclass name extends superclass name

{...}

The object of the subclass is also an object of the superclass, and the object of the superclass is not necessarily the object of the child class.

Inheritance rules for member variables: subclasses inherit only the non-private part of the superclass;

Hide principle (hiding): The member variables of a subclass are hidden when they have the same name as the member variable of the superclass.

Inheritance rules for member methods: subclasses inherit only the non-private part of the superclass;

overriding principle (Override): A member method of a subclass and a member method of a superclass when the same name is used, the members of the superclass method are overridden by the member methods of the Quilt class.

    • How to construct subclasses

When calling the constructor of a subclass, the constructor of the superclass needs to be called, and the first of the parameters of the constructor of the subclass is used for the construction method of the superclass. In the constructor body of the subclass, use super to display the constructor of the superclass, and place the statement at the top of the construction method body. If there is a default constructor in the superclass, then in the constructor method of the subclass, you can not display the constructor method that calls the superclass.

    • Null,this,super

Null: Represents an empty object, that is, no instance of the class was created.

This: Refers to the current object, which is a reference to the object. Access the members of this class,this.< variable name >,this.< method name, and call the constructor method of this class this (parameter table);

Super: A superclass reference, you can use super to refer to the members of the superclass class mask,super.< variable name >,super.< method name, and call the Super Class construction method super (parameter table);

Vi. final classes and abstract classes

    • Final class and final method

Final-decorated classes and methods are called final classes and final methods, which cannot be inherited and are not overwritten in subclasses, meaning that the subclass cannot have a method with the same name as the method.

Final Class last{...} ;

Public final void Printsuper (parameter table) {...} ;

    • Abstract classes and abstract methods

An abstract method is a method that must be overridden in a subclass, which, when declared, uses the keyword abstract to describe the method as an abstract method and does not set the method body .

Classes that contain abstract methods are abstract classes, and they need to be decorated with an abstract, which is a class that cannot be instantiated.

Abstract class < class name >{...abstract method}

proected abstract void write ();

Note: For member methods, static and abstract descriptions cannot be used at the same time, because static can be called without instantiation, and abstract must be instantiated for implementation.

For classes, you cannot use both final and abstract to decorate, because the two approaches are obviously the opposite.

Vii. polymorphism of the class

Polymorphism refers to the same name, multiple implementations . The implementation of polymorphism is mainly done by overloading (overloading) and overwriting (override) .

    • Overload

The name of the method is the same, but the parameters are different (note that only the return value differs from overloading), called overloading . At execution time, depending on the parameters, you decide which method to execute.

    • Covered

Subclasses and superclass have methods with the same name, and parameters are the same, and methods in subclasses override methods of the superclass.

* Concepts of early binding and late binding

For the overloading of the method, when the program compiles, according to the parameters given in the call statement, it can decide the version that is called when executing in the program, this is called the compile-time binding, also called the early binding;

For method overrides, the version of the method with the same name can be determined when the program executes, which is called the runtime binding, also called late binding.

Eight, interface

In Java, the concept of an interface is introduced because multiple inheritance is not supported. A class can have a superclass and multiple interfaces. Object-oriented Programming in Java has also been dubbed interface-oriented programming. The importance of the concept of the interface.

    • Declaration of the interface

An interface is a special class that consists of static constants and abstract methods , and does not contain the implementation of variables and methods .

[Modifier] Interface interface Name {...}

Where modifiers can be public or the default access control.

The variable in the interface is static final by default and must be assigned an initial value. If the interface is public, the members in the interface are implicitly public access rights.

    • Implementation of the interface

An interface can be implemented by a class or multiple classes. When a class implements an interface, it must implement all of the methods in this interface, which are described as public. Implement the interface using the keyword implements.

Class name implements interface 1, interface 2 ...

Java-java Object-Oriented programming

Related Article

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.