Differences between extends and implements in Java

Source: Internet
Author: User

The difference between the inheritance and interface there are many explanations, I believe that there are a few preliminary understanding of the time some confusion, here published a personal understanding of the explanation, if you can understand it better, you are already a big God, please ignore this understanding incomplete explanation!

First, take a look at his two concepts, and then explain them with the familiar words of saliva.

[extends: Inheriting class]. We define a class in which there is a method body (which is the implementation of some functions), and in another class there is a need for the function in the class we defined earlier, then we can use the extends keyword to inherit the function of the former class. We can refer to the preceding class as the parent class, and the later class is called a subclass, like a son inheriting something from his father, such as property. Represents the extends parent class for subclasses.

Build a class a{//member variable where num is an argument int num=0;//member method, where I is the type parameter public A (int i) {//input int file output below System.out.println (" Aaaaaaaaaaaaaaaaaaa "); System.out.println ("i=" +i);//Assign the input I to the member variable Numnum=i;}} b Inherits AClass B extends A{int num=0;//member method B.public B () {//Inherits the method of Class A. Since B inherits a, it inevitably inherits the characteristics of a. So the input int value 10 is implemented by the method. Super (10); System.out.println ("bbbbbbbbbbbbbbbbbbbb");//The num here is Class B's. System.out.println ("num=" +num);// If you want to display num for Class A, you need to use the following super.num. Note here that NUM needs to be a member variable in Class A. System.out.println ("super.num=" +super.num);}} Build test class Cpublic class C{//mainpublic static void Main (String aa[]) {//new an Object B () new B ();}}

  

[Implements: Generally means implementation interface]. The keyword used by a class to implement an interface class. Object implements object such as the interface defined in Java Animal, the class that implements the interface is defined as follows: public class Tiger implements Animal

{}

Differences between extends and implements 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.