Java omits many of the less-used, less-understood, confusing features of C + +, which in our experience brings more sadness than profit.
-----James Gosling
James Gosling This person should be very familiar with, is the original design Java language programmer, known as "The father of Java", now also more than 50 years old, also saw his video a few days ago, very approachable, a bald old man, probably the same as the old people everywhere in China's small town, No one can see that SUN's vice president, nor the Chinese businessman's special show and cunning.
The above remark was asked by someone else about why Java cannot be overloaded with operators (although it does have method overloads), multiple inheritance, and a wide range of automatic casts when answering a paragraph. He said that more inheritance would bring more sorrow, where is the sadness in the end? Here is a little analysis of my own (if there is a non-positive, I hope the great God you shot):
The advantages of multiple inheritance are obvious, a subclass can have more than one parent class properties and methods, such as the above of the Greek mythology of the Centaur, the Man and the horse, the upper body is a human torso, including the hands and head, the lower part of the horse, also including the torso and legs, both speed and thinking and grasping the ability of things.
But the disadvantage of multiple inheritance is also a bit broken:
(1) If a subclass inherits an instance variable of the same name in more than one parent class, the subclass will be ambiguous when referencing the variable, and cannot determine which parent class should be used.
Paste snippet code to see:
Public class ClassA { protected String a = "Same"; } Public class ClassB { protected String a = "Unsame"; } Public class ClassC extends ClassA, ClassB { public void PrintOut () { System.out.println (super. a); -----A critical line------- } public static void main (string[] args) { ClassC ClassC = New ClassC (); Classc.printout (); } }
(PS: The above code is not compiled, I give an example) CLASSC in the end will print same or Unsame na?
(2) If a subclass inherits multiple parent classes that have the same method, and there are no overrides for the method in the subclass, then calling the method will produce ambiguity and cannot determine which parent class should be called;
Paste snippet code to see:
Class ClassA { public void PrintOut () { System.out.println ("Same"); } } Class ClassB { public void PrintOut () { System.out.println ("Unsame"); } } Public class ClassC extends ClassA, ClassB { public static void Main ( String[] args) { ClassC ClassC = new ClassC (); Classc.printout (); -----A critical line----- } }
(PS: The above code is not compiled, I would like to give an example) CLASSC in the end will call which parent class Printour () method?
All of these are fatal ambiguities, so a class in Java is forbidden to inherit multiple parent classes;
But the white-haired old man had provided Java with an interface many years ago, and could gain many advantages of multiple inheritance through the function of the interface, but also abandoned the disadvantage of multiple inheritance of class and class.
How the hell did this happen, Turner? Well, well, let's take a detailed look at the analysis:
Java allows an interface to inherit multiple parent interfaces, and also allows a class to implement multiple interfaces, and does such a multi-inheritance have the drawbacks mentioned above?
Because there is the abstraction of the interface, so the answer is certainly no;
1) In the interface can not have instance variables, only static constants, each static constant before the program starts to run has been initialized successfully, if there is a rename, the compiler will tell you in a timely manner;
2) The interface can not have a specific method (including the method body), can only have an abstract method, the specific method can only be implemented by the implementation of the interface class, at the time of invocation will only invoke the implementation of the class method, so that there will be no ambiguity;
The centaur that opens the door can write like this:
Interface Humain { String arm = "2 can only grab something's arm"; void output ();} Interface Horse { String leg = "4 only legs to run"; void output ();} Interface half extends Humain, Horse {}public class Halfhorse implements half { //Implement the same name method above both interfaces// This class has two interface static Variable public void output () { System.out.println ("I am a centaur with" + Arm + "and" + Leg + "); } public static void Main (string[] args) { Halfhorse halfhorse = new Halfhorse (); Halfhorse.output (); }}
(PS: The above code can be compiled and passed)
Because of the introduction of interfaces, programmers have a lot of room for imagination, which is probably one of the charms that Java attracts many people.
Java Foundation "05" How much do you inherit?