Objective
In Java, subclasses inherit the parent class, the class implementation interface is common sense content, as a Java programmer should also be more familiar with. But the subclass inherits the parent class, the class implements the interface still has some small detail to pay attention, this article starts from the individual work, the study, summarizes some details the content, in order to further grasps the inheritance, realizes the relation. Now just write some of the current encountered content, as long as the thought of, encountered any inheritance, implementation of related issues, will remain updated this article.
Interface A has a void C () method, and interface B has an int C () method, the two interfaces cannot be implemented at the same time
Java in order to compensate for the lack of class single inheritance, introduced a class of multi-implementation interface mechanism, but many implementations of an interface is also a certain limit, such as:
Interface a{ void C ();}
Interface b{ int C ();}
It is not possible to implement both interfaces at the same time:
This error cannot be fixed. Imagine that class AB implements interface A and interface B, then the abstract method in interface A and interface B inside the abstract method parameter list are the same only the return value of the same, which class AB should be implemented? Implement interface A's "void C ()", then interface B's "int C ()" What to do? What if you implement "int C ()" for interface B, then "void C ()" For interface A? Because "void C ()", "Int C ()" are of the same method parameters, and the return values are different, these two methods are not overloaded, so it is not possible to implement both methods at the same time. Therefore, Java can only be an error here.
A is an interface, B implements A,c inherits B, then C is also a subclass of a
There is an interface, a, a interface, C inherits from Class B, C is also a subclass, look at:
Interface a{}
Implements a{}
Extends b{}
void Main (string[] args) { newinstanceof A);}
The return result is true. This is a conclusion that is not difficult to understand, the thought of proving this conclusion is because one time when studying LINKEDHASHMAP:
Class linkedhashmap<k,v> extends hashmap<k,v> implements Map<k,v>
Since Linkedhashmap has inherited the HashMap, HashMap is the implementation of the map class, then why Linkedhashmap also to implement the map, it is not superfluous it? From this thought can be because inherited HashMap does not mean that Linkedhashmap is a sub-class map, so linkedhashmap to specifically to achieve a map, did the above experiment, found that I am worried about, maybe Sun's developers want to write it, hehe.
Property with the same name in parent-child class
This is not a mistake, it only creates ambiguity, and it can result in the return of unwanted results, mainly due to the fact that the developer is not familiar with the parent code (especially in the case of a large heap of attributes defined by the parent class). As an example:
Class father{int public public Father (intthis.i =intreturn
Extends father{ int publicSon (intsuper (this.i =intreturn i;}}
void Main (string[] args) { new Son (); System.out.println ("Son getfatheri ():" + Son.getfatheri ()); System.out.println ("Son Getsoni ():" + Son.getsoni ());
The result of the operation is:
Son Getfatheri ():son Getsoni ():
This problem is raised because we worry that the parent-child class will have a property with the same name, and from the running result, there is no need to worry about this problem at all.
An implementation class inherits from an abstract class and implements multiple interfaces, so all the non-implemented abstract methods must be implemented
As an example:
Interface interfacea{ void A1 (); void A2 ();}
Interface interfaceb{ void B1 (); void B2 ();}
Implements Interfacea, interfaceb{////// I have defined an abstract method myself}
Then to define a classd inherited from ABSTRACTC, you must:
Extends abstractc{////// I must implement the C () method that is not implemented in ABSTRACTC}
Some details of class inheritance and interface implementation in Java (Long-term update)