On the difference between extends and implements in Java _java

Source: Internet
Author: User
Tags constructor inheritance

1. In the declaration of a class, create a subclass of a class by using the keyword extends. A class declares itself to use one or more interfaces through a keyword implements.
Extends is the inheritance of a class that can use the method of the parent class, or the method of the parent class; Implements is the implementation of multiple interfaces, the interface of the method is generally empty, must be overridden to use
2.extends is inheriting the parent class, so long as that class is not declared final or the class is defined as abstract, it inherits, but it can be implemented using an interface, which requires implements, and inheritance can inherit only one class. But implements can implement multiple interfaces, separated by commas.
Like what

Copy Code code as follows:

Class A extends B implements C,d,e

Learned a long time, today finally understand the implements, in fact very simple, look at the following examples on the OK ~ ~
Some concepts of interfaces
<!--[If!supportlinebreaknewline]-->

Copy Code code as follows:

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 ()
{
System.out.println ("Landanimal is Breathing");
}
}

Class Student extends person implements Runner
{
......
public void Run ()
{
System.out.println ("The student is running");
}
......
}

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");
}
}

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

Note Points for interface implementations:

A. Implementing an interface is all the methods for implementing the interface (except for 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 that class is not declared final or the class is defined as abstract and can be inherited, multiple inheritance is not supported in Java, but it can be implemented using interfaces, which means that inheritance can only inherit a class, implements, But implements can implement multiple interfaces, separated by commas.
Like what

Class A extends B implements C,d,e

A class declares itself to use one or more interfaces through a keyword implements. In the declaration of a class, you create a subclass of a class by using the keyword extends.

Copy Code code as follows:

Class subclass name extends parent class name Implenments interface name
{...
}

===========================================================

A A = new B (); Result A is an instance of Class A that can only access the method in a, then and a A = new A (); What's the difference?
==========================================================
Class B extends A
After inheritance, you usually define some members or methods that are not in the parent class.
A A = new B ();
This is possible, upload.
A is an instance of a parent class object and therefore cannot access new members or methods of the subclass definition.
==========================================================
If this is defined:

Class a{
int i;
void F () {}
}
Class B extends a{
Int J;
void F () {}//rewrite
void 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 B
A A = new B ();
A is a constructor of B, but after upcast, it becomes an instance of the parent class object and cannot access the properties and methods of the subclass. A.I,A.F () is legal, while A.J,A.G () is illegal. Access A.F () at this point is access to F () in B
==========================================================
A A = new B (); This statement, in fact, has three processes:
(1) A;
Declare a A parent class object, just a reference, unallocated space
(2) b temp = new B ();
An instance of Class B object is established by the constructor of Class B, which is the initialization
(3) A = (a) temp;
It is safe to convert the subclass object Temp to the Upcast object and assign it to a, which is the upload.
With the above 3 processes, A is a complete example of a Class A.
Subclasses tend to have more properties and methods than parent classes, and uploads are only discarded and safe, while downcast are sometimes increased and often unsafe.
===========================================================
A.F () corresponding to the B-class method F ()
After calling the constructor to establish an instance, the entry for the corresponding method has been determined.
Thus, a is uploaded to Class A, but the overridden method F () is still B's method F (). In other words, each object knows which method it should call.
A A1 = new B ();
A A2 = new C ();
A1,a2 both are Class A objects, but their respective f () are different. This is the manifestation of polymorphism in the 1 floor.

This kind of question is very clear in the Java programming idea

Implements is generally implemented interface. Extends is an inherited class. Interface is generally only the method declaration is not defined, then Java specifically pointed out that the implementation of the interface is reasonable, because inheritance has the feeling that the parent 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 has no method body. So you can understand that interfaces are subclasses that implement their method declarations rather than inherit their methods. But the general class method may have the method body, then calls the inheritance to be more reasonable. The introduction of the package can use all the implementations of the inside non interface classes. Then is not the implementation of the interface, this you decide, if you want to use so you are not implemented, you can not call this interface, because the interface is a specification, is a method of the method declaration set. Let me give you an example: interfaces can be compared to protocols, like I said a protocol is "killing" so this interface you can use a machete to achieve, as for how to kill a machete can be achieved, of course, you can also use the robbery to achieve the interface, but you do not use the killing interface to kill people, because the killing interface is only a functional description, is a protocol, Specifically how to do, but also to see his implementation class. Then a package inside if there is an interface, you can not implement. This does not affect your use of other classes.

Implements

Implements is a class that implements an interface 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 ();} However, the interface has no method body. The method body can only be implemented through a specific class. Such as Chinese this class, the implementation of the People interface. Public class Chinese implements people{public say () {System.out.println ("Hello!)". ");}}

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

Different from the extends

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

Implements, implements the parent class, which cannot overwrite the parent class's methods or variables. Even if the subclass defines the same variable or function as the parent class, it is replaced by the parent class.

The specific use of these two implementations is to see the actual situation of the project, need to implement, can not modify the implements, only the definition of interface needs to be specific implementation, or can be modified to extend good, with extends.


<!--[endif]-->

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.