Java Object-oriented explanation

Source: Internet
Author: User

Java Object-oriented explanation

Introduction: Contact project development also has a long time, and recently began to have to go back to write about the basic knowledge learned before the idea. First is the beginning of learning contact programming, a person stumbled and groped to go forward, beginners when a lot of things understand also Mengmengdongdong, later practice more, some things only slowly clear; second, after a certain practice, back to learn some basic things to understand more thoroughly Third, some things are basic but very important, it is worth doing a good work.

1. Object-oriented

Object oriented is an emerging program design method, or a new program design specification (paradigm), its basic idea is to use object, class, inheritance, encapsulation, polymorphism and other basic concepts to program design. The software system is constructed from the objective object in the real world, and the natural way of thinking is used as much as possible in the system construction.

2. Objects

object is an entity used to describe objective things in the system, which is a basic unit of the system. An object consists of a set of properties and a set of services that operate on that set of properties.

Classes are instantiated to generate objects, and the life cycle of an object consists of three phases: build, use, and eliminate.

When there is no reference to an object, the object becomes a useless object. The Java garbage collector automatically scans the dynamic memory area of an object, collecting non-referenced objects as garbage and releasing them. When system memory is exhausted or a call to System.GC () requires garbage collection, the garbage collection thread runs synchronously with the system.

3. Class

A class is a collection of sets of objects with the same properties and methods that provide a uniform abstract description of all objects that belong to the class, including the properties and methods two main parts. In an object-oriented programming language, a class is a stand-alone program unit that should have a class name and include two main parts of properties and methods.

The class implementation in Java consists of two parts: class declaration and class body.

  class declaration

1 [public][abstract|final] classclassName [extends superclassName] [implementsinterfaceNameList]{……}

Where the modifier public,abstract,final describes the class's properties, classname the class name, superclassname the name of the class's parent class, and interfacenamelist the list of interfaces that the class implements.

  Class Body

123456 class className{    [public | protected | private ] [static] [final] [transient] [volatile] type variableName;//成员变量    [public | protected | private ] [static] [final | abstract] [native] [synchronized] returnType methodName([paramList]) [throws exceptionList]{        statements    }//成员方法}

The meaning of a member variable qualifier:

    • Static variables (class variables)
    • Final: constant; transient: Transient variable for object archiving for serialization of objects
    • Volatile: Contribution variable, for concurrent thread sharing

The implementation of the method also includes two parts: the method declaration and the method body.

  Method declaration

The meaning of a qualifier in a method declaration:

    • Static: Class method, which can be called directly from the class name
    • Abstract: Abstraction method, no method body
    • Final: Method cannot be overridden
    • Native: Integrating code in other languages
    • Synchronized: Controlling access to multiple concurrent threads

Method declarations include method names, return types, and external parameters. The type of the parameter can be a simple data type, or it can be a composite data type (also known as a reference data type).
For simple data types, Java implements value passing, the method receives the value of the parameter, but cannot change the value of these parameters. If you want to change the value of a parameter, use a reference data type, because the reference data type is passed to the method by the address of the data in memory, and the operation of the data in the method can change the value of the data.

  Method body

The method body is the implementation of the method, which includes the declaration of the local variable and all legitimate Java directives. The scope of the local variable declared in the method body is inside the method. If the local variable has the same name as the member variable of the class, the member variable of the class is hidden.
In order to distinguish between parameters and member variables of a class , we must use this. This is used to refer to the current object in a method whose value is the object that called the method. The return value must be identical to the return type, or exactly the same, or its subclasses. When the return type is an interface, the return value must implement the interface.

  Construction method

    • The construction method is a special method. Each class in Java has a constructor method that initializes an object of the class.
    • The constructor method has the same name as the class name and does not return any data types.
    • Overloads are often used to construct methods.
    • The constructor method can only be called by the new operator

4. Object-oriented basic features

  Packaging

Encapsulation is to hide the inner details of the object as much as possible, to form a boundary, and to keep only the limited interfaces and methods to interact with the outside world. The principle of encapsulation is to prevent parts of the object from being arbitrarily accessed and manipulated by the internal properties of the object, thus avoiding the external destruction of the object's internal properties.

You can implement information hiding for members of a class by setting certain access permissions on the members of the class.

    • Private: A member of a class that is limited to private and can only be accessed by the class itself. If the constructor method of a class is declared private, other classes cannot generate an instance of the class.
    • Default: A member of a class that does not have any access permission is the default (default) Access state and can be accessed by the class itself and the classes in the same package.
    • Protected: A member of a class qualified as protected, which can be accessed by the class itself, its subclasses (including the same package and subclasses in different packages), and all other classes in the same package.
    • Public: A member of a class that is qualified as public and can be accessed by all classes.

  Inherited

The object of the subclass has all the properties and methods of the parent class, called the child class inheritance of the parent class.

    • In Java, a parent class can have more than one subclass, but a subclass may inherit only a single parent class, called one-inheritance.
    • Inheritance implements the reuse of code.
    • All classes in Java are obtained by inheriting the Java.lang.Object class directly or indirectly.
    • Subclasses cannot inherit member variables and methods in the parent class that have access rights private.
    • Subclasses can override the parent class's method, which is to name a member variable with the same name as the parent class.

Java uses Super to access the members of the parent class, and super is used to refer to the parent class of the current object. There are three ways to use super:

    • Accesses a member variable that is hidden by the parent class, such as: super.variable;
    • Call the overridden method in the parent class, such as: Super. Method ([Paramlist]), super () calls the parent class construction methods;
    • Call the constructor of the parent class, for example: Super ([paramlist]);

  Polymorphic

An object's polymorphism refers to a property or method defined in the parent class that, after the quilt class inherits, can have different data types or behave differently. This makes the same property or method have different semantics in the parent class and its subclasses. For example, the drawing method for geometry, ellipse and polygon are subclasses of geometry, and the drawing method functions differently.

The polymorphism of Java is embodied in two aspects: the static polymorphism implemented by the method overloads (compile-time polymorphism) and the dynamic polymorphism (run-time polymorphism) implemented by the method overrides.

    • Compile-time polymorphism: In the compile phase, specifically called which overloaded method, the compiler will statically determine the invocation of the corresponding method according to the different parameters.
    • Run-time polymorphism: Subclass objects can be used as parent objects because they inherit all of the properties of the parent class (except private). In a program where the parent object is used, the subclass object can be used instead. An object can invoke a method of a subclass by referencing an instance of the child class.

  Overloading (overloading)

    • Method overloading is a means for a class to handle different data types in a uniform manner.
    • You can create multiple methods in a class that have the same name but have different parameters and different definitions. Methods are invoked by the number of different arguments and parameter types passed to them to determine which method to use.
    • The return value type can be the same or different, and cannot be used as a distinguishing criterion for overloaded functions with a return type.

  Rewrite (overriding)

    • Subclasses to rewrite the methods of the parent class. If a method in a subclass has the same method name, return type, and parameter table as its parent class, we say that the method is overridden (overriding).
    • If you need a method from the parent class, you can use the Super keyword, which references the parent class of the current class.
    • The access adornment permission for a subclass function cannot be lower than the parent class.

Java Object-oriented explanation

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.