A brief introduction to the difference between extends and implements in Java

  • 2020-04-01 01:27:45
  • OfStack

1. In the class declaration, create a subclass of the class with the keyword extends. A class declares that it USES one or more interfaces via the keyword implements.
Extends is an inheritance from a class that can either use the parent's methods or override them. Implements implements multiple interfaces, whose methods are typically empty and must be overridden to be used
Extends is an inheritance from a parent class, as long as that class isn't declared final or that class is defined as abstract. JAVA doesn't support multiple inheritance, but you can implement it using interfaces, which means implements
Such as


class A extends B implements C,D,E

Now that I've learned a lot about implements, it's actually pretty simple. Just look at a few examples
Some concepts of interfaces
< ! - [if! SupportLineBreakNewLine] - >


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

Attention points of interface implementation:

A. To implement an interface is to implement all the methods of that interface (except abstract classes).
B. The methods in the interface are abstract.
C. Multiple unrelated classes can implement the same interface, and one class can implement multiple unrelated interfaces.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Extends is different from implements

  Extends is an inheritance from the parent class, as long as that class is not declared final or that class is defined as abstract. JAVA doesn't support multiple inheritance, but you can implement it using interfaces, which means implements
Such as

Class A extends B implements C,D,E

A class declares that it USES one or more interfaces via the keyword implements. In the declaration of the class, a subclass of the class is created with the keyword extends.


class  A subclass of  extends  The parent class name  implenments  The interface name 
{...
}

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

A A = new B(); The result a is an instance of class a, can only access the method in a, and a a = new a (); What's the difference?
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Class B extends A
Inheritance is often followed by the definition of some member or method that the parent class does not have.
A A = new B();
That's ok. Upload.
A is an instance of a superclass object and therefore cannot access new members or methods defined by subclasses.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Suppose you define it this way:

Class A {
Int I;
Void f () {}
}
Class B extends A {
Int j;
The 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 those of its parent class. Examples such as b.i.,b.j.,b.f(), and b.g() are legal. In this case, b.f() is f() in the accessed B
A A = new B();
Although a is the constructor of B, it becomes an instance of the parent object through upcast and cannot access the properties and methods of the subclass. A I,a f() is legal, while a j,a g() is not legal. So accessing a.f() is accessing f() in B
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
A A = new B(); This statement actually has three procedures:
(1) A, A.
Declare a as a parent object, just a reference, no space allocated
(2) B temp = new B();
An instance of a class B object is created by the class B constructor, that is, initialization
(3) a = temp (a);
It is safe to convert the subclass object temp into an unsuperclass object and assign it to a, which is the upload (upcast).
After the above three processes, a completely becomes an instance of class a.
Subclasses tend to have more properties and methods than the parent class, upload is just discarded, is safe; Downcast is sometimes increased and is usually unsafe.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
A.f () should correspond to the method f() of class B.
After the constructor is invoked to establish the instance, the entry to the corresponding method is determined.
In this way, a is uploaded as class a, but the overridden method f() is still the method f() of B. That is, each object knows which method it should call.
A1 = new B();
A a2 = new C();
A1 and a2 are both class A objects, but their f() is different. This is exactly the embodiment of the polymorphism of the 1st floor.

This kind of problem is all very clear in Java programming ideas

Implements is generally an implementation interface. Extends is an inheritance class. Interface is generally only method declaration is not defined, then Java specifically pointed out that it is reasonable to implement the interface, because inheritance has the feeling that the parent class has implemented the method, and the interface is exactly not to implement its own method, only has the declaration, that is, a method header without the method body. So you can think of an interface as a subclass that implements its method declaration rather than inheriting its methods. But general class methods can have method body, so called inheritance is more reasonable. The import package can use all the non-interface implementation classes inside. So whether you implement the interface, that's up to you, if you want to use it then you're not the implementation, you can't call the interface, because the interface is just a specification, a collection of method declarations without a method body. I'll give you an example: the interface can be compared to, for example, I said a deal was "kill" the interface you can use a machete to achieve, as for how to kill a machete to realize, of course you also can be used to implement interface to kill, but you can't use interface to kill, to kill as a murderer interface is just a function that is a deal, specific how to do, to see his implementation class. So if there's an interface in a package, you don't have to implement it. This does not affect your use of other classes.

implements

Implements is a keyword that a class USES to implement an interface, and it's used to implement an abstract method defined in the interface. For example, "people" is an interface that contains the "say" method. Public interface people(){public say(); } but the interface has no method body. The method body can only be implemented by a concrete class. For example, the Chinese class implements the interface of people. Public class Chinese implements people{public say() {system.out.println (" hello!" ); }}

Implements in Java means that A subclass inherits A parent class, such as class A inherits class B as class A implements B{}

Unlike Extends

Extends, which either implements the parent class or calls the parent class to initialize this.parent(). It also overrides the variables or functions defined by the parent class. The advantage is that the architect defines the interface and the engineer implements it. The overall project development efficiency and development costs are greatly reduced.

Implements implements a parent class, and subclasses cannot override methods or variables of a parent class. Even if a subclass defines the same variable or function as the parent class, it will be replaced by the parent class.

So the specific use of these two implementations, depending on the actual project, you need to implement, you can't change implements, you just define the interface you need to implement, or you can modify it to be extensible, with extends.


< ! -- [endif] -- >


Related articles: