Java basics 10-interfaces, Java basics-interfaces

Source: Internet
Author: User
Tags define abstract

Java basics 10-interfaces, Java basics-interfaces
Java basics 10-interface 1, interface definition and instance

1/* 2 abstract class AbsDemo 3 {4 abstract void show1 (); 5 abstract void show2 (); 6} 7 8 when the methods in an abstract class are abstract, in this case, the abstract class can be defined and expressed in another form of 9, that is, the interface. 10 */11 12 // define the interface. The keyword is not a class, but an interface. 13 // After the interface is compiled. the class file is actually a class. 14 15/* 16 interfaces and classes are implemented. interfaces and interfaces are inherited from each other. Why can they be inherited because there is no method body, whether it can inherit more depends on whether there is a method body 17 */18 19/* 20 for common members in the interface: And these Members have fixed modifiers. 21 1, global constant: public static final 22 23 2, abstract method. Public abstract 24 25 modifier add to you if you do not write it yourself. So we all write, so that reading is strong. 26 27 According to the conclusion, all members in the interface have public permissions. 28 29 inheritance does not implement member variables, methods are also abstract methods, and there is no function body method, so the implementation interface and the inheritance class do not conflict, interface Demo 32 {33 public static final int NUM = 4; // static, final, and public. If you do not write a program, 34 35 public abstract void show1 () is added by default (); // if you do not write a program to public and abstract, 36 public abstract void show2 (); 37} 38 will be added by default. // There is an inheritance relationship between the class and the class, classes and interfaces are directly implemented. The 39/* 40 interface cannot be instantiated. 41 42 This subclass can be instantiated only after the subclass that implements the interface and overwrites all the abstract methods of the interface. 43 otherwise, this subclass is an abstract class. 44 45 */46 47 48 class DemoImpl implements/* Implementation */Demo 49 {50 public void show1 () 51 {} 52 53 public void show2 () 54 {55 56} 57} 58 59 60/* 61 does not directly support multi-inheritance in java, because of the call uncertainty. 62. Therefore, java improved the multi-Inheritance Mechanism and changed to multi-implementation in java. 63 64 A class can implement multiple interfaces. 65 66 */67 68 interface A 69 {70 public void show (); 71} 72 73 interface Z 74 {75 public int add (int a, int B ); 76} 77 78 79 80 81 class Test implements A, Z // implement 82 {83 84 public int add (int a, int B) 85 {86 return a + B + 3; 87} 88/**/89 public void show () {} 90 91} 92 93 94 95 96 97/* 98 a class can implement multiple interfaces while inheriting another class. 99 100 101 102 */103 class Q104 {105 public void method () 106 {} 107} 108 109 abstract class Test2 extends Q implements, the Z110 {111 112} 113/* 114 interface avoids the limitation of single inheritance. 115 116 */117 118 119 interface CC120 {121 void show (); 122} 123 interface MM124 {125 void method (); 126} 127 128 interface QQ extends CC, MM // The interface and interface are inherited, and the interface can be inherited. 129 {130 void function (); 131} 132 133 class WW implements QQ134 {135 // cover 3 methods. 136 public void show () {} 137 public void method () {} 138 public void function () {} 139} 140 141 142 class InterfaceDemo 143 {144 public static void main (String [] args) 145 {146 147 Test t = new Test (); 148 t. show (); 149 150 // DemoImpl d = new DemoImpl (); 151 // System. out. println (d. NUM); 152 // System. out. println (DemoImpl. NUM); 153 // System. out. println (Demo. NUM), 154} 155}

 

Ii. Differences between interfaces and abstract classes
1/* 2 similarities and differences between abstract classes and interfaces: 3 4 similarities: 5 are constantly extracted from the top. 6 7 8 differences: 9 1. abstract classes must be inherited and can only be inherited separately. 10 interfaces need to be implemented and can be implemented in multiple ways. 11 2. abstract classes can define abstract and non-Abstract METHODS. After subclass inheritance, you can directly use non-abstract methods. 12 The interface can only define abstract methods and must be implemented by sub-classes. 13 3. the inheritance of abstract classes is the is a relation, which defines the basic commonalities of the system. 14. The implementation of the interface is like a relation, which has additional functions in the definition system. 15 16 17 18 19 dogs by function: guide dogs, search for dogs. 20 21 22 abstract class dog 23 {24 abstract void roar (); 25} 26 27 // abstract class blind guide 28 interface blind guide 29 {30 abstract void blind Guide (); 31} 32 33 34 class guide dog extends dog implements guide blind 35 {36 public void roar () 37 {38} 39 public void guide blind () {} 40} 41 42 43 44 // different analysis methods are available in different problem fields. 45 students: 46. 47 smoking students 48 49 50 51 52 smokers. 53 54 55 56 57 58 59 60 61 */62 63 class InterfaceDemo 64 {65 public static void main (String [] args) 66 {67 System. out. println ("Hello World! "); 68} 69}

 

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.