Combination and inheritance

Source: Internet
Author: User

Concepts of inheritance and combination
Create 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 simply reuse the code features.

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 programming, "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 ).

A combination means that the object of a class is a member of another class. Generally, the program has a combination, but the basic data type is a member variable. See the specific example below.

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 is composed of the Head and Body. The object of the Head and Body is part of the Person.
{
H = new Head ();
B = new Body ();
}

}

Inheritance, as one of the three important features 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 inheritance.
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. The subclass cannot access the private variables and functions of the parent class.

Both combination 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.
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 left = 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 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. Just imagine that you cannot combine a car with a vehicle object.
-- An automobile 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.

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 basic implementation details 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 ).

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.

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.