Differences between extends and implements in Java

Source: Internet
Author: User

What is the difference between extends and implements in Java? 1. In the declaration of a class, create a subclass of a class with the keyword extends. A class declares itself to use one or more interfaces through the keyword implements. extends is inherited from a class, can be inherited after the use of the parent class method, you can override the parent class method, implements is to implement multiple interfaces, the method of the interface is generally empty, must be overridden to use 2.extends is inherited from the parent class, As long as the class is not declared as final or the class is defined as abstract can inherit, Java does not support multiple inheritance, but can be implemented with interfaces, so it is necessary to use implements, inheritance can inherit only one class, but implements can implement multiple interfaces, Separate them with commas, like class A extends B implements C,d,e

=========================================================== implements learned for a long time, today finally understand implements, actually very simple, Here are a few examples OK ~ ~ Some concepts of the interface public inerface Runner { int ID = 1; void Run (); }

Interface Animal extends Runner { void breathe (); }

Class Fish implements Animal {public void run () { System.out.println ("Fish is swimming"); }

public void breather (){System.out.println ("Fish is bubbing");}}
Abstract Landanimal implements Animal{public void breather (){}
class Student extends person implements Runner {   ...    public void Run ()     {          SYSTEM.OUT.PRINTLN ("The student is running");    }    ... /span>

Interface Flyer{void Fly ();
class Bird Implements Runner, Flyer {  public void Run ()        System.out.println ("The bird is Running");   }   public void Fly ()    {       System.out.println ("The Bird is Flying");   } /span>

Class Testfish {public static void Main (String args[]) { fish f = new Fish (); int j = 0; j = runner.id; j = f.id;    }

the point of attention of the interface implementation:

A. Implementing an interface is all of the methods to implement the interface (except abstract classes). B. The methods in the interface are abstract. C. Multiple unrelated classes can implement the same interface, and a class can implement multiple unrelated interfaces. The difference between =========================================================== extends and implements
Extends is inherited from the parent class, as long as the class is not declared final or the class is defined as abstract can inherit, Java does not support multiple inheritance, but can be implemented with interfaces, so it is necessary to use the implements, inheritance can inherit only one class, But implements can implement multiple interfaces, separated by commas, such as class A extends B implements C,d,e

A class declares itself to use one or more interfaces by means of the keyword implements. In the declaration of a class, a subclass of a class is created with the keyword extends. Class subclass name extends parent class name Implenments interface name {...


A = new B (); Result A is an instance of Class A that can only access the method in a, and a = new A (); What's the difference?==========================================================Class B extends AAfter inheritance, it is common to define members or methods that are not in the parent class.A = new B ();This is possible, upload.A is an instance of a parent class object and therefore cannot access a new member or method defined by the child class.==========================================================If it is defined as:Class a{int i;void F () {}}Class B extends a{Int J;void F () {}//rewritevoid G () {}}And then:b b = new B ();B is an instance of a subclass object that can access not only its own properties and methods, but also the properties and methods of the parent class. such as B.i,b.j,b.f (), B.G () are legal. At this point B.f () is the F () in the Access BA = new B ();A Although the constructor of B is used, it is upcast to become an instance of the parent object and cannot access the properties and methods of the child class. A.I,A.F () is legal, while A.J,A.G () is illegal. Access A.F () is at this point F () in Access B==========================================================A = new B (); There are actually three processes in this statement:(1) A;Declares a as a parent class object, but only a reference, unallocated space(2) b temp = new B ();An instance of Class B object is established by the constructor of Class B, that is, initialization (3) A = (a) temp; =========================================================== A.F () should be a method of Class B () f () a a1 = new B (); a a2 = new C (); a1,a2 both are Class A objects, but their respective f () are different. This is the embodiment of the polymorphism of the 1 floor.

This kind of problem is very clear in "Java programming thought"

Implements is generally implemented as an interface.

Extends is an inherited class.

Interfaces are generally only defined by method declarations,

Then Java specifically pointed out that the implementation of the interface is justified, because the inheritance has the feeling that the parent class has implemented the method, and the interface is just not the way to implement their own, there is only a declaration, that is, a method header no method body. So you can understand that an interface is a subclass that implements its method declarations rather than inheriting its methods.

But the general class method can have the method body, then is called the inheritance is more reasonable.

The introduction package can be used to implement all classes inside a non-interface. So is not the implementation of the interface, this you decide, if you want to use so you are not the implementation, you can not invoke this interface, because the interface is a specification, is a method of the method of the body of the Declaration collection. Let me give an example: the interface can be likened to a protocol, for example, I say a protocol is "kill" then this interface you can use a machete to achieve, as to how to kill a machete can be achieved, of course, you can also use the robbery to achieve the killing interface, but you can't kill with the killing interface, because the killing interface is just a function What to do, but also to see his implementation of the class.

If there is an interface inside a package, you can not implement it. This does not affect your use of other classes.


Implements is a class that implements an interface with a keyword that is used to implement an abstract method defined in an interface. For example: People is an interface, he has say this method. public Interface People () {public say ();} But the interface has no method body. The method body can be implemented only through a specific class. For example, Chinese this class, the implementation of people this interface. Public class Chinese implements people{public say () {System.out.println ("Hello! ");}}

In Java, implements represents a subclass that inherits from a parent class, such as Class A inherits class B written as Class A implements b{}

Different from the extends

Extends, you can implement a parent class, or you can call the parent class to initialize This.parent (). It also overrides the variables or functions defined by the parent class. The benefit is that the architect defines the interface so that the engineer can do it. The overall project development efficiency and development costs are greatly reduced.

Implements, implements the parent class, which cannot overwrite a method or variable of the parent class. Even if the subclass defines a variable or function that is the same as the parent class, it is replaced by the parent class.

These two implementations of the specific use, is to see the actual situation of the project, need to implement, can not modify implements, only define the interface needs to be implemented specifically, or can be modified extensibility, with extends.

Differences between extends and implements in Java

Related Article

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.