The difference between Java extend and implements

Source: Internet
Author: User

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, you can use the parent class method after inheritance, or you can override the parent class method; Implements is to implement multiple interfaces, the method of the interface is generally empty and must be rewritten to use
2.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 implements, inheritance can inherit only one class, But implements can implement multiple interfaces, separated by commas.
Like class A extends B implements C,d,e

Some concepts of interfaces


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

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.
Like what

Class A extends B implements C,d,e

A class declares itself to use one or more interfaces through 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 A
After 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 () {}//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 = 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;
The Subclass object Temp is converted to the non-parent class object and assigned to a, which is the upload (upcast), which is safe.
After the above 3 processes, A has become an example of a class.
Subclasses tend to have more properties and methods than the parent class, uploads are only discarded, are safe, and the downcast sometimes increases and is usually unsafe.

A.F () corresponds to the method F () of Class B.
After invoking the constructor to establish an instance, the entry for the corresponding method has been determined.
In this case, A has been uploaded as Class A, but the overridden method F () is still the method F () of B. 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 embodiment of the polymorphism of the 1 floor.


Implements is generally implemented as an 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 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

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.

The difference between Java extend and implements

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.