Inheritance and combination in Java

Source: Internet
Author: User
1. The concept of inheritance and combination creates objects of the original class in the new class. We call this method "Combination" because the new class is composed of the objects of the existing class. We just reuse it. Code Instead of using it.
The second method is to create a new class and use it as a "type" of the existing class ". We can take the form of an existing class as it is, and add new code to it without affecting the existing class. This magic behavior is called "inheritance", and most of the work involved is done by the compiler. For object-oriented Program Design, "inheritance" is one of the most important basic concepts. For the combination and inheritance methods, most syntaxes and actions are similar (because they all generate new types based on existing types ). 2. A combination means that the object of a class is a member of another class. Generally, a program has a combination, but the basic data type is a member variable, the following shows an example of class head {head () {system. out. println ("head") ;}} class body {body () {system. out. println ("body") ;}} class person () {head H = NULL; body B = NULL; person () // a person consists of the head and body, the object of head and body is a part of person {H = new head (); B = new body () ;}} 3. inheritance, as one of the three important characteristics of object-oriented, plays an important role in the Object-Oriented field. It seems that I have not heard of any object-oriented language that does not support inheriting class person.
{
Private string name = NULL;
Private int age = 0;
Public Person (string N, int)
{
Name = N;
Age =;
}
Int getage ()
{
Return age;
}
String getname ()
{
Return name;
}
Void getdescription ()
{
System. Out. println ("name:" + name + "/t" + "Age:" + age );
}
} Class student extends person
{
Private string studno = NULL;
Public student (string N, string no, int)
{
Super (n, );
Studno = no;
} Note: the student class has three member variables: name, age, studno, And the getdescription () method. Note: The subclass inherits all the variables and functions of the parent class, only the subclass cannot access the private type variables and functions of the parent class. In fact, the privae type variables still inherit from the subclass 4. both inheritance and inheritance allow us to place sub-objects in our new class. You may wonder the differences between the two and how to choose them.
If you want to use the features of an existing class in the new class instead of using its interfaces, you should select a combination. That is to say, we can embed an object so that we can use it to implement the features of the new class. However, users of the new class will see the interface we have defined, rather than the interface from the embedded object. Considering this effect, we need to embed the private object of the existing class into the new class.
Sometimes, we want class users to directly access the combination of new classes. That is to say, the attribute of the member object must be changed to public. Member objects are hidden, so this is a safe approach. The interface is easier to understand when users know that we are preparing to synthesize a series of components. Car objects are a good example:
Class engine {public void start () {}public void Rev () {}public void stop () {}} Class Wheel {public void inflate (INT psi) {}} class window {public void rollup () {}public void rolldown () {}} class door {public window = new window (); Public void open () {} public void close () {}} public class car {public engine = new engine (); Public wheel [] wheel = new wheel [4]; Public door l EFT = new door (), Right = new door (); // 2-door car () {for (INT I = 0; I <4; I ++) wheel [I] = new wheel ();} public static void main (string [] ARGs) {car = new car (); car. left. window. rollup (); car. wheel [0]. inflate (72 );}}///:~

Because automobile assembly is a factor that needs to be considered in Fault Analysis (not just a simple part of the basic design), it helps the customer programmers understand how to use classes, in addition, the programming complexity of class creators is greatly reduced.
If you select inheritance, You need to obtain a ready-made class and create a special version of it. Generally, this means that we are going to use a class for general purposes and customize it according to specific needs. You just need to imagine that you cannot use a vehicle object to combine a car. A car does not "include" a vehicle. On the contrary, it "belongs to" a type of vehicle. The "belonging" relationship is expressed by inheritance, while the "include" relationship is expressed by combination.

5. Protected
Now that we have understood the concept of inheritance, the keyword protected finally makes sense. Under ideal conditions, Private Members are "private" at any time and cannot be accessed by anyone. However, in practical applications, we often want to hide some things deeply, but at the same time allow access to members of the category class. The protected keyword helps us do this. It means "it is private, but it can be accessed by anything inherited from this class or anything in the same package ". That is to say, protected in Java will become "friendly.
The best practice we take is to maintain the private status of Members-in any case, the right to modify the implementation details of the Foundation should be retained. On this premise, you can use the protected method to allow the class's successors to Perform controlled access:

Import Java. util. *; Class villain {private int I; protected int read () {return I;} protected void set (int ii) {I = II;} public villain (INT II) {I = II;} public int value (INT m) {return M * I;} public class Orc extends villain {private Int J; Public ORC (INT JJ) {super (jj); j = JJ;} public void change (int x) {set (x );}}///:~

As you can see, change () has the permission to access set () because its attribute is protected (protected ). 6. Synthesis and inheritance
In object-oriented programming, one of the most likely ways to create and use code is to encapsulate data and methods into a class and use the class objects. Sometimes, you need to use the "Combination" technique to construct new classes with ready-made classes. Inheritance is the least common practice. Therefore, although inheritance has been heavily emphasized in the process of learning Oop, it does not mean that it should be used everywhere as much as possible. On the contrary, exercise caution when using it. It can be considered only when it is clear that inheritance is the most effective in all methods. To determine whether a combination or inheritance should be selected, the simplest way is to consider whether the new class needs to be traced back to the basic class. If it must be traced back, it must be inherited. However, if you do not need to roll back the style, you should remind yourself to prevent the abuse of inheritance. However, as long as you remember to always ask yourself "Do I really need to roll back the shape?", the choice of combination or inheritance should not be too big.

 

From: http://blog.csdn.net/Cpp_Java_Man/article/details/705279
 

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.