Java-object-oriented (2), java-object-oriented

Source: Internet
Author: User

Java-object-oriented (2), java-object-oriented

When I was reading HeadFirst over the past few days, I suddenly felt that some of the characteristics of object-oriented language were vague, so I would like to review it again to deepen my impression.

The previous blog (http://blog.csdn.net/u010539352/article/details/39500455) has explained the characteristics of the three object-oriented, the following explains the rest.

Polymorphism: if a person wants to keep a pet and a dog, how can he achieve this? Then the polymorphism is used, which can be switched randomly without modifying the code in the "person" class, which is dynamic and flexible. The following is an example:

Class Animal {private String name; Animal (String name) {this. name = name;} public void enjoy () {System. out. println ("cry ...... ") ;}} class Cat extends Animal {private String eyesColor; Cat (String n, String c) {super (n); eyesColor = c;} public void enjoy () {System. out. println ("cat call ...... ") ;}} class Dog extends Animal {private String furColor; Dog (String n, String c) {super (n); furColor = c ;} public void enjoy () {System. out. println ("dog call ...... ") ;}} class Lady {private String name; private Animal pet; // defines a parent pet, which can be a dog pet or a cat pet, dynamic, most flexible Lady (String name, Animal pet) {this. name = name; this. pet = pet;} public void myPetEnjoy () {pet. enjoy () ;}} public class Test {public static void main (String args []) {Cat c = new Cat ("catname", "blue "); dog d = new Dog ("dogname", "black"); Lady l1 = new Lady ("l1", c); Lady l2 = new Lady ("l2 ", d); l1.myPetEnjoy (); // polymorphism, the method of the New object, so it is the cat's enjoy () method l2.myPetEnjoy (); // dog's enjoy () method }}
Display result:

Explanation: when the New object is generated during the runtime, the method of the New object is called. During the running, the New enjoy method is used for dogs and cats, so the enjoy Method for cats and dogs is called. Dynamic. So the girl's cat is happy to run out, so the cat's enjoy method runs. The result is: the cat's cry ..... The girl's dog is happy, so the cat's enjoy method runs. The result is: the dog's cry ..... Now, if you add another bird, you don't need to change the original code. You can simply add a class to achieve scalability.

To sum up the following: when a method is called using polymorphism, first check whether the method exists in the parent class. If not, the compilation is incorrect. If yes, the method with the same name of the subclass is called. Therefore, polymorphism must have three conditions: inheritance, rewriting, and parent class references pointing to subclass objects.

From the example of polymorphism, we can see that the enjoy method of the parent class does not need to be implemented, because the subclass must be rewritten, and the parent class is redundant, so abstraction is used.

Abstract: Use the abstract keyword to modify. The following is an example:

Abstract class Animal {// abstract class private String name; Animal (String name) {this. name = name;} public abstract void enjoy (); // abstract method, only the definition is not implemented .} Class Cat extends Animal {private String eyesColor; Cat (String n, String c) {super (n); eyesColor = c;} public void enjoy () {System. out. println ("cat call ...... ") ;}} class Dog extends Animal {private String furColor; Dog (String n, String c) {super (n); furColor = c ;} public void enjoy () {System. out. println ("dog call ...... ") ;}} class Lady {private String name; private Animal pet; Lady (String name, Animal pet) {this. name = name; this. pet = pet;} public void myPetEnjoy () {pet. enjoy () ;}} public class Test {public static void main (String args []) {Cat c = new Cat ("catname", "blue "); dog d = new Dog ("dogname", "black"); Lady l1 = new Lady ("l1", c); Lady l2 = new Lady ("l2 ", d); l1.myPetEnjoy (); l2.myPetEnjoy ();}}

Explanation: classes with abstract keywords are defined as abstract classes and methods are defined as abstract methods. Only definitions are not implemented. In the subclass.

Therefore, abstract classes must be inherited and abstract methods must be overwritten.

As mentioned above, inheritance, rewriting, abstraction, and polymorphism can be used in combination to implement perfect programming. But what is lacking in the United States is that Java cannot implement multi-inheritance, the interface is used here.

Interface: It is a special abstract class that only contains the definition of constants and methods, without the implementation of variables and methods .. Several unrelated classes can implement the same interface. A class can implement multiple interfaces. It is equivalent to multi-inheritance. Example:

Interface Animal {// interface class public void call ();} interface Animal1 {// interface class public void run ();} class Dog implements Animal {// Dog class implementation interface private String name; Dog (String name) {this. name = name;} public void call () {// implemented the interface method System. out. println ("dog call ...... ");} public String getName () {return name;} public void run () {// implemented the interface method System. out. println ("dog run ...... ") ;}} class Cat implements Animal {// Cat class implementation interface private String name; Cat (String name) {this. name = name;} public void call () {// implemented the interface method System. out. println ("cat call ...... ");} public String getName () {return name;} public void run () {// implemented the interface method System. out. println ("cat run ...... ") ;}} public class Interface {public static void main (String args []) {Cat c = new Cat (" cat "); dog d = new Dog ("dog"); c. call (); c. run (); d. call (); d. run ();}}
Display result:

Explanation: There is no write method implementation in the interface class, but the common class must overwrite all the methods of the interface.

Summary: rewriting must have inheritance.

Polymorphism must be inherited and rewritten.

Abstract must be inherited and rewritten. Use abstraction to achieve polymorphism.

The interface must be implemented.

Once summarized in this way, the idea is much clearer. There is no harm in summary.



What Is Object-Oriented in Java?

The target object is:
Put data and data operation methods together as an interdependent whole-object. Abstract The commonalities of similar objects to form classes. Most of the data in the class can only be processed using the method of this class. A simple external interface is used to communicate with an object through a message. The procedure is determined by the user in use.
Objects are an abstract concept of various objects. People need to access various objects every day, such as mobile phones.
In object-oriented programming, objects have multiple features, such as the height, width, thickness, color, and weight of mobile phones. These features are called object attributes. Objects also have many functions, such as listening to music, making phone calls, sending messages, watching movies, etc. These functions are called object methods, which are actually a function. The objects are not isolated, but have parent-child relationships. For example, mobile phones belong to electronic products and electronic products belong to objects. Such parent-child relationships are called the inheritance of objects. In the compilation process, all problems are taken as objects and solved in the way of human thinking. This method is very user-friendly. objects are actually a set of data, and the data has been named. The data is the property of the object and can be accessed by the program. Objects also include many functions. These functions are called object methods and can be accessed by programs. However, attributes or methods in the external access object must first reference the object, and then access the attributes and methods of the object with the point number.

The process orientation is:
The program structure is divided into several basic modules by function. These modules form a tree structure. The relationship between modules is as simple as possible, the functions are relatively independent. Each module is composed of three basic structures: sequence, selection, and loop. The specific method of modularization is to use subprograms. The program process is determined when writing the program.

Why is Java object-oriented?

Process-oriented and object-oriented are two different ways for computers to solve practical problems.

The first step is what to do and the second step is what to do... The ultimate goal is to make every step more efficient,

Object-oriented is what the first part is and what the second part is... The ultimate goal is to divide the problem into several objects, but it is not concerned about how to implement it.
Calculate the area of a circle. first define the variable, then write the calculation method and output the result. This is the process-oriented idea, and object-oriented is to define an object circle, which provides a method to calculate the area

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.