Composition VS inheritance (inheritace) combination and inheritance is two ways to achieve "repeated use of classes", each has its own advantages and disadvantages, how to flexibly and efficiently use these two methods in the design of the program It is a question worth studying; in "Tij" to write this: "When you want to use existing Class's features in the new Class, not its interface, usually you choose a combination is correct, that is Embed an object, so that you can achieve the features you want in new Class. But the new Class user can only see the interface you defined for the new Class, will not see the embedded one The interface of the object. But if this is what you want, you should embed the existing Classes object in the new class " ---- Composition Example ---------- ------------------------------ Class A {Private INT I; Private Void Test (INT I) {System.out. Println ("i =" i);} public static void main (string [] args) {system.out.println ("a");}} public class b {private static a a = new a (); / / Embedded member object public static void main (string [] args) {b = new b () //b.test (3) -------> Can't call, A.Test (3); / / Test the class () system.out.println ("b");}} -------------------------------------------------------------------- ----------------------- Class A {Private INT I; Private Void Test (INT I) {System.out.Println ("i =" i);} public static void main (string [] args) {system.out.println ("a");}} public class b extends a {private stat A A = new A (); // Embed member object public static void main (string [] args) {b = new b () b.test (3) // can call Base Class directly with the object of DeriveD Class by inheritance Method A.Test (3); // Call the class in the class () system.out.println ("b");}} ------------------------------------------------------------------------ ------------------------------------------------ END- - Inherit is the most important thing is not to provide functions for new Class, but inheritance can achieve upcasting. In the learning process of OOP, it often emphasizes inheritance, but does not mean that you should use it everywhere. There is a detailed description of the disadvantages of Extends in "why extends is harmful", "Most good designers like to avoid plague, avoid using real inheritance (Extends). Write, no specific base class.
"Someone asked Jams Gosling:" If you re-construct java, what do you want to change? "I want to abandon classes" he answered him explained: The real problem is not due to the Class itself, but to achieve the inheritance (Implements relationship) is better. You should avoid inheritance as much as possible ((For the comparison of Implements and Extends, it will not be found now. Back to the combination and inheritance) For the use of inheritance, it must be able to produce significant value. If you have to transform the new Class up to Base Class, you You should use inheritance, if you do not have to be required, then you should carefully consider whether you want to use inheritance. Tell the upward transformation, the problem is coming, why do you want to transform? The most competitive answer is ---- - Polymorphism: Data Abstract, inheritance and polymorphism are three core nature of object-oriented spur language.
Why do you want to transition to see the example below: ----------------------- Upcast why ------------ -------------------------------- First: Polymorphism.javaclass Animal {Void Breathe () {System.out.Println ("Animal.Breathe");}} class human extends Animal {void Breathe () {system.out.println ("human.breathe");}} Class dog extends animal {void breth () {system.out.println ("Dog.Breathe");}} class cat extends Animal {void Breathe () {system.out.println ("cat.breathe");}}} public class polymorphism {public static void upcast (animal a) {///// <---- notice a.breathe (); public static void main (string args []) {human h = new human (); DOG D = New DOG (); CAT C = New cat (); // --Upcasting --- Upcast (h); Upcast (D); Upcast (c);}} // end --------------------- Second PolyMorphism_1. JavaClass Animal {void Breathe () {system.out.println ("Animal.Breathe");}}} class human // Extensity Animal {void Breathe () {system.out.println ("human.breathe");}} Class Dog // Extends Animal {void Breathe () {System.out.println ("Dog.Breathe");}} Class Cat // EXTENDS Animal {Void Breathe () {system.out.println ("Cat.Breathe");}}} public class polymorphism_1 {public static void upcast (human a) {// <---- notice a.breathe (); } Public static void Upcast (DOG A) {// <---- NOTICE A.BREATHE ();} public static void upcast (Cat A) {// <---- NOTICE A.BREATHE ();} PUBLIC Static void main (string args []) {human h = new human (); DOG D = new DOG (); CAT C = new cat (); // - no upcasting --- Upcast (h); Upcast (Upcast); Upcast D); Upcast (c);}} // end ------------------------------------- ---------------------------- End If you do not transform, you have to write a corresponding Upcast () for every new class. , This is not only when starting writing code, it is very spent.