Special implementation methods of java interfaces and abstract classes

Source: Internet
Author: User

1. interfaces in java are essentially constrained abstract classes // abstract classes public abstract class AExample {public abstract int add (int x, int y); public abstract int sub (int x, int y);} // interface public interface IExample {public int add (int x, int y); public int sub (int x, int y );} the common usage is to create a new class, which implements an interface or derives from an abstract class. This new class must implement all methods defined in the interface or all abstract methods in the abstract class. 1. Implementation of the abstract class: public class MyClass extends AExample {// other member definitions: // implement the abstract method public int add (int x, int y) in the abstract class) {return x + y;} public int sub (int x, int y) {return x-y;} // other part: omitted} usage: AExample AE = new MyClass (); int s = AE. add (4, 2); int t = AE. sub (); 2. Interface implementation method: public class MyClass implements IExample {// other member definitions: slightly // implement abstract methods in abstract classes public int add (int x, int y) {return x + y;} public int sub (int x, int y) {retur N x-y;} // other part: omitted} usage: IExample AE = new MyClass (); int s = AE. add (4, 2); int t = AE. sub (); it can be seen that the two are essentially the same and use methods are very similar. Ii. Special implementation methods here do not use the methods for deriving new classes from abstract classes and implementing new classes using interfaces. 1. Let's take a look at the array initialization example: int [] array; after defining the array, array is just a reference. array [0], array [1] is not implemented, that is, it does not exist. However, if the initialization is: int [] array = new int [] {1, 2}; 1 and 2 are considered as two "member instances" of the array ". In this case, array [0] and array [1] are implemented, that is, array is implemented. 2. interfaces and abstract classes can be implemented in a similar way: AExample AE = new AExample () {public int add (int x, int y) {return x + y ;} public int sub (int x, int y) {return x-y ;}}; int s = AE. add (4, 2); int t = AE. sub (); the so-called "member instance" here is the implementation of the add and sub methods. And IExample AE = new IExample () {public int add (int x, int y) {return x + y;} public int sub (int x, int y) {return x-y ;}}; int s = AE. add (4, 2); int t = AE. sub (4, 2); amazing! Iii. Summary In fact, this method is rarely used in general and mainly used in event handling. And mainly uses interfaces. The above is a question I encountered when learning java's event processing programming: Why is the java event listener registered in the form of this: // Person is a class designed by myself to handle events. PersonListener is an interface-listener interface Person p = new Person ("Tong", 53 ); //////////////////////////////////////// //////////////////////////////////////// //////////////// // create an instance of the event listener interface and register p. addPersonListener (new PersonListener () {public void OnNameChanged (PersonEventObject e) {// custom event processing code System. out. println ("Name:" + (Perso N) (e. getSource ())). getName ();} public void OnAgeChanged (PersonEventObject e) {// custom event processing code System. out. println ("Name:" + (Person) (e. getSource ())). getName ());}}); //////////////////////////////////////// ////////////////////////////////////////. addPersonListener (...) the two steps are clear: PersonListener pl = new PersonListener () {public void OnNameChanged (PersonEventObject e) {www.2cto. Com // custom event processing code System. out. println ("Name:" + (Person) (e. getSource ())). getName ();} public void OnAgeChanged (PersonEventObject e) {// custom event processing code System. out. println ("Name:" + (Person) (e. getSource ())). getName () ;}}; p. addPersonListener (pl); let's take a look at the problem we discussed above. Through this example, we hope that many beginners of java will get the result.

Related Article

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.