Java Learning Lesson 14th (interface: Implements)

Source: Internet
Author: User
Tags define abstract

Interface:

We know that abstract classes can define abstract methods, or non-abstract methods, and when a method in an abstract class is an abstract method, we can define another expression: interface (interface), so the interface is a special kind of abstract class

The appearance of the interface will "multi-inheritance" is expressed in another form, that is, " multi-implementation ."

Note: The definition of the interface is not class, but interface, and of course the last compile-time class file

Interface demo{abstract void Show (); abstract void Show1 ();}

Common members in interfaces: (All fixed modifiers)

1. Global Constants (public static final)
2. Abstract method (public abstract)
3. Of course there are other

Interface demo{public static final int num = 4;//num lifetime is 4public int num = 4;//not written, will be automatically added to, default, preferably written on, otherwise poor reading public abstract void Show ();}

this concludes:The member in the interface is the public,-> permission Max

if it is a class, it will use inheritance, if the interface, it will not inherit, with another more intuitive way of expression, calledImplement(implements).


Why is it called implementation?

The methods in the interface are abstract, so it is necessary to fully implement, for the abstract class, there are some not abstract, sub-class to come directly with. Inheritance can be brought over some I do not need to do, directly can use the things, and implementation is all I want to use, all to overwrite, all to rewrite themselves.


So: between classes and classes is an inheritance relationship, and between classes and interfaces is the implementation relationship


Features of the interface implementation:

Import Java.util.scanner;interface demo{public abstract void Show ();p ublic abstract void Show1 ();p ublic static final int n Um = 4;} A class is an inheritance relationship between a class and an interface, which is an implementation relationship (implements)//* The interface cannot be instantiated * only by subclasses that implement the interface, and all abstract methods in the interface can be instantiated only after it has been overridden * Otherwise, this subclass is just an abstract class * */class demo1_ Imple implements demo//a class implements an interface, it must overwrite all the methods in the interface to implement the class implements interface {public void Show () {System.out.println ("show print" +num) ;} public void Show1 () {System.out.println ("Show1 print" +num);}} public class Main {public static void main (string[] args) {Scanner in = new Scanner (system.in);D emo1_imple BLF = new demo1_ Imple (); Blf.show (); Blf.show1 (); Blf.num = 5;//error, num is the final modified System.out.println (blf.num);//object with NUMSYSTEM.OUT.PRINTLN (demo.num);// Static member of Interface System.out.println (demo1_imple.num);//static member of the child class that implements the interface num}}

Advantages of the interface:

Java does not directly support multiple inheritance, because there will be the uncertainty of the call (there is a hidden danger), so Java will be the C + + inheritance mechanism has been improved, in Java has become a multi-implementation (a class implementation of multiple interfaces)

Multiple inheritance: The same is inherited from one or two parent class, a parent class has a A, a, a two function, a parent class has c,d two functions, subclass one inherits has a,b,c,d four functions
Multi-Implementation: A parent class has a A, a, a, two abstract function, a parent class has c,d two abstract function, a subclass implementation has these 4 functions
(Small difference from multiple inheritance)
We pay attention to the function of things, as to how to use the function, sub-class explicit, if the function of the parent class has been in line with the requirements of subclasses, subclasses directly use it, if not, subclasses still have this function, but to rewrite, but the function of what to do, the sub-class said that the calculation

Why is the multi-implementation of Java called improved multi-inheritance?

The disadvantage of multiple inheritance is that if the two parent class method name is the same, there will be uncertainty, and more implementation? There is no, because the method body of Show () is written by the subclass itself, and multiple inheritance, inherited is the method body, after the Java improved, there is no method body, the method body is decided by the subclass

Multiple implementations, little exercise: interface a{public abstract void Show ();} Interface b{public abstract void Show ();p ublic abstract int Add (int a,int B);} Class C implements A,b{public void Show () {System.out.println ("Hello");} public int Add (int a,int b) {return a+b+10;}} public class Main {public static void main (string[] args) {c BLF = new C (); Blf.show (); System.out.println (Blf.add (1, 3));}}

Therefore: multi-implementation, the implementation of multiple inheritance to improve

Of course, the following two kinds of test code way, is the Java unbearable

Interface a{public abstract void Show (); Interface B{public abstract int show ();} Test 1:class C implements a,b{public void Show ()//If this write is an abstract class, because there is no overwrite so the method {System.out.println ("Hello");}} Test 2:class C implements A,b{public void Show () {    System.out.println ("Hello");} public int Show () {    System.out.println ("Hello, World"); show is already defined in//c, call show, return value cannot be determined, int or void}}

Of course, the function of the interface is far from the only such

One class inherits, the other class is implemented in multiple implementations (that is, a parent class is first found for the class, and then these functions are extended)

See Code:

Interface a{public abstract void Show (); Interface b{public abstract void Show ();p ublic abstract int Add (int a,int B);} Class d{public void  print () {System.out.println ("You are Good");}} Very fierce!!! Class E extends D implements A,B//E inherits D, implements a and B, so that the function of E is much richer {//e single inherits the print function in D, but also wants to use the function in a, B, but these functions do not pass through inheritance,// Then the way to implement the interface at the same time, to extend the function in e public void Show () {System.out.println ("Hello");} public int Add (int a,int b) {return a+b+10;} public void Test () {System.out.println ("Test");}} /* The presence of the interface avoids the limitations of single inheritance */public class main {public static void Main (string[] args) {e BLF = new E (); Blf.show (); System.out.println (Blf.add (1, 3)); Blf.test (); Blf.print ();}}

Between classes and classes is an inheritance relationship between a class and an interface, which is the implementation relationship between the interface and the interface?

The reason why Java does not support multiple inheritance is that the method body


Interface a{public abstract void Show (); Interface b{public abstract void Show ();p ublic abstract int Add (int a,int B);} Interface E extends a,b//inheritance relationship, and can inherit more, more fierce!!! {//Multiple inheritance, poor on the method body, interface no method body, so you can inherit public abstract void test more;} Class G implements E{public void Show () {System.out.println ("Hello Good");} public int Add (int a,int b) {return 250;} public void Test () {System.out.println ("You are not good");}} public class Main {public static void Main (string[] args) {g BLF = new G (); System.out.println (Blf.add (3, 4)); Blf.show ();}}


Man can only say that Java is really very fierce!!!
The above is the basic implementation of the interface ...

Not finished ...


Java Learning Lesson 14th (interface: 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.