The relationship between object-oriented and class in Java

Source: Internet
Author: User

1. Object-oriented
A. What classes and objects are needed to analyze the problem
B. Analyze what properties and methods should be in the above class or object
c, analysis of the relationship between classes
(The appropriate method should appear in the appropriate class)
2. Example: template-Physical
A, Whiteboard pen
B. Employee (age, Name, gender, job)
(Show name, age, change name, change age, change job position)
(Object-sensitive: The object can be distinguished by a property by means of a method.)
3. Classes and objects
A, class is an abstraction of a kind of thing with common characteristics
B. An object is a specific instance of this class


4.4 kinds of relationships between classes (objects)
A, dependency (Dependency)
The so-called dependency is that the function of an object depends on another object, and the object being relied on is used only as a tool, and does not hold a reference to it.

For example: A person's self-creation needs to keep breathing, and the human breath function to maintain life is that the gas sucked in to play a role, so that air is just a human tool, and people do not hold the reference to it.
B, Association Relationship (Association)
An object will hold a reference to another object for a long time, and the associations of the two are often reciprocal.
The associated two objects do not have any mandatory constraints on each other, and as long as they agree, they can be removed or associated at any time.
They have no agreement on life-time issues. The associated object can also be associated with another object, so the association can be shared.

For example: People from life to death are constantly making friends, but there is no reason to think that the life and death of friends and I have an inevitable connection, so their lives are not related, my friend can be someone else's friend, so friends can be shared.
C, aggregation relationship (Aggregation)
Aggregations are associations of strong versions.
It implies a relationship of affiliation as well as a life-time relationship.
Objects that are aggregated can also be associated with other objects, so the aggregated objects can be shared.
Although it is shared, aggregation represents a more intimate relationship.

For example: There is a strong relationship between my home and me,
My home can be shared, and there are two kinds of sharing here.
One is the sharing of the aggregation, as you and your daughter-in-law have the same strong connection to the family;
The second is the sharing of aggregation and association, if your friend comes to the house to eat a potluck, it is estimated that you will not give him a key.
D, combinatorial relationship (compostion)
A combination is the strongest version of a relationship that directly requires the owning of an object to the contained object and the relationship between the containing object and the lifetime of the contained object.
The contained object can also be associated with another object,
So the contained objects can be shared,
However, there is no more than two shares that contain objects to the same contained object.

For example, the combination of relationships is the whole and part of the relationship, part of the whole,
The whole does not exist, partly certainly does not exist, however the part does not exist that the whole is can exist,
What is more clear is that the part must be created after the whole creation, and destroyed before the whole destruction.

5, the embodiment of object-oriented in Java
A, Package
B. Inheritance
C, polymorphic
Compile-time polymorphism
Run-time polymorphism

  6, member variable, local variable
  7, construction method
  8, permission modifier renewed:
    When overriding the parent class member function, The member function of a subclass cannot be smaller than the member function permissions of the parent class, and the general override defines the permissions as the same.

Scope Current class same package descendant class other package
Public√√√√
Protected√√√x
Friendly√√xx
Private√xxx

The relationship between object-oriented and class in Java

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.