Java Object-oriented

Source: Internet
Author: User

Object-oriented is the transformation of things or objects in the real world into things or objects in the computer, which abstracts things or things from the real world into classes, and the object is a specific thing or something. Simply put, a class is a template that abstracts things from the real world. The object is an instantiation of the class, materialized, to load the data. Three main features of object-oriented: inheritance, encapsulation, polymorphism. 1. Encapsulation: A, formally organize the data and behavior of the object in a class, realize the hiding of information B, can not let the methods in the class access other classes of data, the program through the object of the method and the object of the interactive C, the object of "black box" characteristics, improve the object reusability and reliability. means that even if a class completely changes the way data is stored, other objects will not know or mind the changes as long as the data is still manipulated using the same method. 2. Inheritance: Based on encapsulation, the reuse of code is realized, and inheritance fully shows the reusability of code. Inheritance is implemented through the extends keyword. The parent class of all non-object classes is the object class, and the default parent class that does not inherit is the object class. Java only supports single inheritance, the benefit is that the relationship between the parent class and the subclass is easy to manage, but it also increases the coupling between classes and classes, violates software design principles (cohesion, low coupling) such as C + + supports multiple inheritance, and the relationship between parent and child classes is difficult to manage. The downside is that sometimes some subclasses need to inherit multiple classes, but Java does not support, in order to compensate for this shortcoming, Java introduced the interface, polymorphic to a certain extent can also solve this problem. Inheritance can only inherit objects that are not private to the parent class (properties and methods) Note that the subclass is not something that has a parent class, but only has access to something that the parent class has. The parent class is loaded first when the class is loaded, so the parent class object is created before the child class object is created, and the parent class construct is called first when the subclass is called. 3, polymorphic: As the name implies is the object of a variety of forms. Polymorphic criteria: Polymorphism is based on inheritance, subclasses override methods of the parent class, and then assign a reference to the child class to the parent class, calling the method of the subclass from the reference of the parent class. ① is divided into multiple states: reference polymorphism and Method polymorphism. ② Reference polymorphism: A reference to the parent class can point to the object of the parent class, the reference to the parent class can point to the object of the child class. ③ method Polymorphism: When a parent class object is created, the method that is called is the method of the parent class, and the method that is called when the subclass object is created is either a method that is overridden by the subclass or an inherited method. ④ There are conditions for polymorphism: inheritance, rewriting. The role of ⑤ polymorphism is to eliminate the coupling relationship between types. In the actual project development, Class A inherits Class B, if the class B method is not rewritten in Class A, the output is still the information in the class B method (b b=new A ()), and if the class B method is overridden in Class A, the output is the information in the Class A method (b b=new A ()). A simple example of polymorphism: Package com.tedu.oop;pUblic class Person {private string name;private int age;public person (String Name,int age) {this.name=name;this.age=age;} public void Eat () {System.out.println ("eat");}} Package Com.tedu.oop;public class Student extends Person{private string stuno;public Student (string name, int age,string s Tuno) {Super (name, age); this.stuno=stuno;//TODO auto-generated constructor stub}public void Eat () {System.out.println ( "The student is Eating");}} Package Com.tedu.oop;public class Duotaitest {public static void main (string[] args) {person p=new Student ("Zhang San", 20, "123" );p. Eat ();}} Running results: Students at dinner the most important thing about polymorphism is inheritance and rewriting, and for different subclass methods, different parent classes can show different states by invoking the same method. The relationship between classes and classes inherits the Is-a relationship combination has--a relationship, where a reference to another class in one class relies on the use--a relationship, and another class is referenced in the method of one class. One of the seven Principles of software development: multi-use combination, less combination, because the relationship between the inheriting class and the class coupling increases, and the combination can reduce the coupling between classes and classes.

Java Object-oriented

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.