1. In the declaration of the class, use the keyword extends to create a subclass of the class. A class declares that it uses one or more interfaces through the keyword implements.
Extends means inheriting a certain class. After inheritance, you can use the methods of the parent class, or you can override the methods of the parent class; implements means implementing multiple interfaces. Interface methods are generally empty and must be rewritten before they can be used.
2. Extends is to inherit the parent class. As long as the class is not declared as final or the class is defined as abstract, it can be inherited. JAVA does not support multiple inheritance, but it can be implemented using interfaces. In this case, implements must be used. Inheritance can only Inherit a class, but implements can implement multiple interfaces, just separate them with commas. For example
Copy the code code as follows:
class A extends B implements C,D,E
After studying for a long time, I finally understood the implementation today. It is actually very simple. Just look at the following examples~~
Some concepts of interfaces
<!--[if !supportLineBreakNewLine]-->
Copy the code code as follows:
public innerface 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 bubbling");
}
}
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;
}
}
Notes on interface implementation:
a. To implement an interface is to implement all the methods of the 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.
================================================== =========
The difference between extends and implements
extends is to inherit the parent class. As long as the class is not declared as final or the class is defined as abstract, it can be inherited. JAVA does not support multiple inheritance, but it can be implemented using interfaces. In this case, implements must be used. Inheritance can only inherit one Class, but implements can implement multiple interfaces, just separate them with commas. For example
class A extends B implements C,D,E
A class declares that it uses one or more interfaces through the keyword implements. In the class declaration, use the keyword extends to create a subclass of the class.
Copy the code code as follows:
class subclass name extends parent class name implements interface name
{...
}
================================================== =========
A a = new B(); As a result, a is an instance of class A and can only access methods in A. So what is the difference from A a = new A();?
================================================== ========
class B extends A
After inheritance, some members or methods that are not found in the parent class are usually defined.
A a = new B();
That's ok, upload it.
a is an instance of a parent class object, and therefore cannot access new members or methods defined by the subclass.
================================================== ========
If defined like this:
class A{
int i;
void f(){}
}
class B extends A{
int j;
void f(){}//Rewrite
void g(){}
}
Then:
B b = new B();
b is an instance of a subclass object. It can not only access its own properties and methods, but also the properties and methods of the parent class. Things like bi, bj, bf(), bg() are all legal. At this time bf() is f() in B accessed
A a = new B();
Although a uses the constructor of B, it becomes an instance of the parent class object after upcast and cannot access the properties and methods of the subclass. ai,af() are legal, but aj,ag() are illegal. At this time, accessing af() is accessing f() in B
================================================== ========
A a = new B(); This statement actually has three processes:
(1) A a;
Declare a as a parent class object, just a reference, no space allocated
(2) B temp = new B();
An instance of a class B object is established through the constructor of class B, that is, initialized
(3) a = (A)temp;
Convert the subclass object temp into an unparent class object and assign it to a. This is an upload (upcast), which is safe.
After the above three processes, a has completely become an instance of class A.
Subclasses often have more properties and methods than parent classes. Uploading only discards them, which is safe; while downcasting sometimes increases them, which is usually unsafe.
================================================== =========
af() should correspond to the method f() of class B
After calling the constructor to create an instance, the entrance to the corresponding method has been determined.
Since then, although a has been uploaded as class A, the overridden method f() is still the method f() of B. That is, each object knows which method it should call.
A a1 = new B();
A a2 = new C();
Although a1 and a2 are both class A objects, their respective f() are different. This is exactly the embodiment of polymorphism mentioned on the first floor.
Such issues are explained very clearly in "Java Programming Thoughts"
Implements generally implements the interface. extends is an inherited class. Interfaces generally only have method declarations but no definitions, so it makes sense for Java to specifically point out the implementation of interfaces, because inheritance means that the parent class has implemented methods, while interfaces do not implement their own methods, only declarations, that is A method header has no method body. Therefore, you can understand that an interface is a subclass that implements its method declarations rather than inherits its methods. However, general class methods can have method bodies, so it is more reasonable to call them inheritance. Importing a package can use all classes that implement non-interfaces in it. Then you decide whether to implement the interface or not. If you want to use it, you cannot call the interface without implementing it, because the interface is a specification, a collection of method declarations without a method body. Let me give you an example: an interface can be compared to a protocol. For example, if I say a protocol is "killing", then you can use a machete to implement this interface. As for how to kill, the machete can implement it. Of course, you can also use grab to implement the killing interface. , but you cannot use the killing interface to kill people, because the killing interface is just a function description and a protocol. How to do it depends on its implementation class. So if there is 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 used by a class to implement an interface. It is used to implement the abstract methods defined in the interface. For example: people is an interface, and it has the say method. public interface people(){ public say();} But the interface has no method body. The method body can only be implemented through a specific class. For example, the Chinese class implements the people interface. public class chinese implements people{ public say() {System.out.println("Hello!");}}
In Java, implements means that a subclass inherits a parent class. For example, class A inherits class B and is written as class A implements B{}
Difference from Extends
extends, you can implement the parent class, or you can call the parent class to initialize this.parent(). And it will overwrite variables or functions defined by the parent class. The advantage of this is that the architect can define the interface and let the engineer implement it. The development efficiency and development costs of the entire project are greatly reduced.
implements, implements the parent class, and subclasses cannot override the methods or variables of the parent class. Even if the subclass defines the same variables or functions as the parent class, they will be replaced by the parent class.
The specific use of these two implementations depends on the actual situation of the project and needs to be implemented. Implements cannot be modified. Only the defined interface needs to be implemented specifically, or it can be modified for good scalability. Use extends.
<!--[endif]-->