Some details on class inheritance and interface implementation in Java

Source: Internet
Author: User
Tags map class

1. Interface A and interface B have the same method, but the return value is different, the implementing class cannot implement the methods in both interfaces at the same time.

Interface A has a void C () method, and interface B has an int C () method, and 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:

public interface a{    void C ();
public 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.

2.A is an interface, B implements A,c inherits B, 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:

public Interface a{}
public class B implements a{}
public class C extends b{}
public static void Main (string[] args) {    c c = new C ();    System.out.println (c instanceof 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:

public 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.

3. A parent-child class has a variable with the same name and a method with the same name.

Subclasses do not overwrite variables with the same name as the parent class.

Class Base {    int count = 2;    public void display () {        System.out.println ("Base:" + This.count);}    } Class Derived extends Base {    int count =;    @Override public    void display () {        System.out.println ("Derived:" + This.count);}    } public class Main {public    static void Main (string[] args) {        Base bd = new Derived ();        System.out.println (bd.count);        Bd.display ();        Derived d = new Derived ();        Base d2b = D;        System.out.println (D2b.count);    }}

The result of the operation is:

2derived:202

  

The reasons are as follows:

The compiler has a rule that the compile-time type is determined by the type used when declaring the variable, and the run-time type is determined by the object that is actually assigned to the variable.


The compiler has a difference in handling methods and member variables . When a subclass does not override a method of the parent class, the compiler copies the methods in the parent class to the subclass, and if the subclass is replicated, it cannot copy. There is no such thing as an instance variable, and a variable of the same name defined by the subclass cannot overwrite a variable of the same name in the parent class, as in the case of "2 and 20" above, and two are stored, because there is such a difference between the variable and the method, so for a variable of a reference type:

    • When you access a variable, follow the type (compile-time type) when declaring the variable
    • When accessing a method, the type of the object that is actually referenced (run-time type)

Therefore, for the purposes of the System.out.println(bd.count); BD, the compile-time type is base, so the count of the base is output, which is 2, and for the bd.display(); purpose of calling the method, the BD's run-time type is derived, so the display () method of the subclass is called. In the same System.out.println(d2b.count); vein, the count of compile-time type base is output

4.Overload of the situation

The above example modifies a little bit:

Class Father {public    void foo (Object o) {        System.out.println ("Father.foo ()");}    } Class Son extends father{public    void foo (String s) {        System.out.println ("Son.foo ()");}    } public class Main {public    static void Main (string[] args) {        son son = new Son ();        Son.foo (New Object ());}    }

Operation Result:

Father.foo ()

Note that this is a overload (method overload), where the method parameter differs, and the subclass does not override the parent class's method.

Unlike the example above, son inherits the Foo (Object O) method from the parent class, and son decides which method to use depending on the parameters passed in. For this example, object is passed in, so son uses the inherited Foo method instead of his own.

5. An abstract class inherits the interface and can not implement the method of the interface.

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: public interface interfacea{    void A1 ();    void A2 ();} public interface interfaceb{    void B1 ();    void B2 ();} Public abstract class ABSTRACTC implements Interfacea, interfaceb{public    void A1 () {}//I implemented Interfacea's A1 () method    public void B2 () {}//I implemented the INTERFACEB B2 () method        abstract void C ();//I have defined an abstract method myself}

Then to define a classd inherited from ABSTRACTC, you must:

public class CLASSD extends abstractc{public   void A2 () {}//I must implement the Interfacea () method that is not implemented in A2 public   void B1 () {}//I must Implement the B1 () method that is not implemented in Interfaceb   void C () {}///I must implement the C () method that is not implemented in ABSTRACTC}

  

Some details on class inheritance and interface implementation in Java

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.