Java object-oriented it's enough to know that.

Source: Internet
Author: User
Tags modifier

Object oriented

Object-oriented is a way of thinking, relative to process-oriented.
Process-oriented attention to every detail of action execution in a process-do it yourself
Object-oriented focus on finding this object, so long as the object is found, then the function of the object can be used-to find someone else to do
Class-to-object relationships
Based on the extraction and summary of a class of objects, the characteristics of such objects are extracted into attributes, the behavior of such objects is extracted into a method, and classes are used to represent such objects--class is the process of extracting and summarizing objects.

Object-oriented features


Package
Extract and summarize the code into different forms-encapsulated
Embodiment: Methods, classes (privatization of properties-restricting properties to private, and then providing external settings and methods for obtaining them, and then restricting the data to make the information more common)
Advantage: Improve the reusability of the code, ensure the legitimacy of the data
Permission modifiers
Refers to the keyword used in Java to qualify the scope of use
In other classes in the same package class in this class of neutron class
Public can be used to
Protected can not be able to
The default can be the same as bun class can not be
Private can not do not can not be
Inheritance
If some classes contain the same code, you can extract the same code into a new class, and then let the original class and the new class have a relationship-inheritance through the extends keyword. By inheritance, the original class is called the subclass (derived class), and the new class becomes the parent class (superclass/base class).
Subclasses can use a subset of the methods and properties in the parent class by inheriting the parent class
Note: Subclasses inherit all data fields (method + properties) of the parent class, but only a subset of the data fields are visible to the child class after inheritance
In Java, a subclass of single inheritance between classes and classes can inherit only one parent class, but a parent class can have multiple subclasses
Is single inheritance necessarily better than multiple inheritance? -No
Multiple inheritance is better than single inheritance to improve the reusability of code

1 classA {2      Public intM () {return 9;}3 }4 classB {5      PublicBoolean m () {return false;}6 }7 classC extends A, b{}8c C =NewC ();9C.M ();//can the return value type be determined? 

Multiple inheritance can cause ambiguity when calling a method
Advantage: Improve the reusability of code, avoid ambiguity of method call
Super keyword
In a subclass that represents a reference to a parent class object, you can call the properties of a method in the parent class in a subclass.
Super statement-Subclass after inheriting the parent class, the child class will have a super statement in its construction method. If you do not specify the super statement manually, the parent class is invoked by default with super (), and if the parent class only provides a parameter-only construct, the subclass must manually provide the corresponding form of the SUPER Statement-super statement must be in the first row of the subclass construction method
override/Overwrite of method
There is a non-static method with the same method signature in the parent-child class. Follow the "two two small One big" principle:
1. Method signatures are the same
2. If the return value type of a method in the parent class is the base type/void, then the return value type of the method overridden by the subclass is consistent with the parent class

1 class A {2      Public void m () {} 3 }4classextends  A {5public       void  m () {}6 }

3. If the return value type of a method in the parent class is a reference type, the child class returns a value type that is either consistent with the parent class or a subclass of the parent class method return value type when overriding the method

1 classA {}2 classBextendsA {}3 classC {4      PublicA m () {return NULL;}5 }6 classDextendsC {7      PublicB m () {return NULL;}8}

4. The scope of the permission modifier for a method overridden by a subclass is greater than or equal to the scope of the permission modifier for the corresponding method in the parent class

1 class A {2      Public void m () {} 3 }4classextends  A {5public       void  m () {}6 }

Note: If a method in the parent class is decorated with private, the method is not visible to the child class, so the methods in the subclass are not rewritten

Polymorphic

compile-time polymorphism: Overloading of methods
Add (2, 4), add (int, int)
Add (3, 1, 7), add (int, int, int)
run-time polymorphism : up styling and rewriting of methods-based on inherited
Note: If you use up styling to create an object, the method that this object can invoke looks at the declaration in the parent class, and how the method executes is the implementation of the sub-class.
Rewriting the understanding
1. The scope of the permission modifier for a method overridden by a subclass is greater than or equal to the scope of the permission modifier for the corresponding method in the parent class
2. If the return value type of a method in the parent class is a reference type, the child class returns a value type that is either consistent with the parent class or a subclass of the parent class method return value type when overriding the method
Note: There is no inheritance relationship between all the basic types in Java and can be promoted automatically because the represented range can contain

Java object-oriented it's enough to know that.

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.