Java Object-Oriented understanding

Source: Internet
Author: User

Do a long time to do the test, the university did not seriously learn Java, now each enterprise to the test personnel more and more high, master the programming language has become the necessary skills; I also learned a lot of Java, but did not have to learn Java well, In the face of job-hopping, always feel that their resumes are often less bright spots. Finally make up my mind, take 2 minutes a day to learn Java, in order to write in the future resume to understand Java.

First, object-oriented

In the real world, a thing that is everywhere is object, object is the entity that things exist, such as human, desk, computer, tall buildings and so on. By simplifying the way the problem is solved, you will think about which parts of the object are composed. Usually the object is divided into two parts: static and dynamic parts. Static, that is, the part that is not moving, this part is called "attribute", any object will have its own properties, such as: a person, height, fat and thin gender, age and other attributes. However, those who have these attributes will perform what actions are also a part of the discussion, the person can cry, smile, talk, walk, these are the behavior of this person (dynamic part), the human by exploring the properties of objects and observe the behavior of objects to understand the object.

Second, class

A class is a vector that encapsulates the properties and behavior of an object, whereas a class of entities that have the same properties and behavior are called classes. For example a bird, a bird encapsulates the common properties of all birds and shall have the behavior of its structure:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/7F/wKiom1W4pf2xXa74AAELgtOY8Ho185.jpg "title=" 0f3o67pcm}jmg_cbh2h{b~b.jpg "alt=" Wkiom1w4pf2xxa74aaelgtoy8ho185.jpg "/>

Three, Package

1, encapsulation is the core idea of object-oriented programming, encapsulates the properties and behaviors of objects, and the carrier that encapsulates the properties and behaviors of objects is the class, the class usually hides its implementation details to the customer, which is the encapsulation idea.

2. 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.

Iv. inheritance

One of the main functions of object-oriented programming (OOP) language is "inheritance". Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class.

New classes created through inheritance are called "subclasses" or "derived classes."

The inherited class is called the base class, the parent class, or the superclass.

The process of inheritance is from the general to the special process.

To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition).

In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can have only one base class, and to implement multiple inheritance, it can be implemented by multilevel inheritance.

Inheritance concepts are implemented in three categories: implementation inheritance, interface inheritance, and visual inheritance.

V. Polymorphism

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.

1, compile-time polymorphism: In the compilation phase, specifically called which overloaded method, the compiler will statically determine the corresponding method according to the different parameters.

2. Runtime polymorphism: Subclass objects can be used as parent objects because subclasses inherit all of the properties of the parent class (except for 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.

Vi. Method 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.

Vii. methods of rewriting

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.


This article is from the "Test Road" blog, be sure to keep this source http://yangliqin.blog.51cto.com/8836122/1679745

Java object-oriented understanding

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.