Java Neutron class inherits the parent class and implements the interface has the same name method how to deal with __java learning

Source: Internet
Author: User

Java is a single inheritance language, but in the inheritance relationship of subclasses, there are cases where the parent class and the interface have the same method.

Let's take a step-by-step validation:

1. Subclass inherits the parent class, implements the interface, and the parent class and interface have the same method

Public interface MyInterface {
	void Test ();
}
public class Father {public
	void Test () {
		System.out.println ("Parent class method");
	}
public class Son extends Father implements myinterface{}

Subclass directly inherits the parent class and implements the interface without error, which means that the default parent class implements the method

2. We comment out the methods in the parent class and see the results

Public interface MyInterface {
	void Test ();
}
public class Father {/
/public	void Test () {
//		System.out.println ("Parent class method");
//	}
}



Obviously, the subclass has an error in the inheritance process and needs to implement the methods in the interface

3. Uncomment the method in the parent class, and the subclass implements the method with the following code:

Public interface MyInterface {
	void Test ();
}
public class Father {public
	void Test () {
		System.out.println ("Parent class method");
	}


As you can see, the subclass method hint is the overridden parent class method.

At this point, the verification is complete.



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.