Object-oriented advanced--the basic concept of interface and the polymorphism of objects

Source: Internet
Author: User
Tags define abstract

Interfaceneed to master the definition format and use of the interfaceMastering the relationship between interfaces and abstract classesSpecific content:interfaces are the most important concepts in Java, and interfaces can be understood as a special class, all of which are made up of global constants and common abstract methods. if a class consists entirely of a global constant (static Final declaration) and an abstract method, it can be defined as an interface. the definition format of the interface: Interface Interface Name { Global constants; abstract methods; }  
Interface a{//defines the interface apublic static final String AUTHOR = "刘勋";//global constant public abstract void print ();//abstract method public abstract String getInfo ();//Abstract method}
As for interfaces, because they are defined in terms of their composition as global constants and abstract methods, it is often possible to simplify their definition in development
Interface a{//defines the interface astring AUTHOR = "刘勋";//global constant void print ();//abstract method string GetInfo ();//Abstract method}
The two definitions are exactly the same, without any distinction.
Implementing Interfacesas with abstract classes, the use of interfaces must also be through subclasses, and subclasses implement interfaces through the Implements keyword. Implementation format: class subclass implements interface A, interface b{
}the use of interfaces must also have subclasses, subclasses must overwrite all the abstract methods, implements keyword implementation, a subclass can implement multiple interfaces. If the subclass is not an abstract class, it must overwrite all the abstract methods in the interface.
Interface a{//defines the interface apublic string AUTHOR = "刘勋";//global constant public void print ();//abstract method public String getInfo ();//abstract method}inter Face b{//defines interface bpublic void say ();//define abstract method}class X implements a,b{//X class implements both A and B two interfaces public void say () {System.out.println ("H Ello world!!! ");} Public String GetInfo () {return "HELLO";} public void print () {System.out.println ("" + AUTHOR);}}; public class Interfacedemo03{public static void Main (String args[]) {x x = new X ();//Instantiate Subclass Object X.say (); X.print ();}};
Implementation of the interfacein the above program, a subclass implements two interfaces at the same time, so that all the abstract methods in the two interfaces must be covered at the same time in the subclass.
inheriting an abstract class implementation interface: a subclass can inherit both abstract classes and implementation interfaces. The format is as follows:Class subclass extends abstract class implements interface A, interface B, ... {}
Interface a{//defines the interface apublic string AUTHOR = "刘勋";//global constant public void print ();//abstract method public String getInfo ();//abstract method}abstr The Act class b{//defines the abstract class bpublic abstract void Say ();//define abstract method}class X extends B implements a{//X class line inherits type B, then implement A interface public void say () {System.out.println ("Hello world!!!");} Public String GetInfo () {return "HELLO";} public void print () {System.out.println ("" + AUTHOR);}}; public class Interfacedemo04{public static void Main (String args[]) {x x = new X ();//Instantiate Subclass Object X.say (); X.print ();}};
In use, an abstract class can implement an interface, then for the subclass of the abstract class you must overwrite both the interface and all abstract methods defined in the abstract class.
Interface a{//defines the interface apublic string AUTHOR = "Li Xinghua";//global constant public void print ();//abstract method public String getInfo ();//abstract method}abst Ract Class B implements a{//defines the abstract class B, implements the interface Apublic abstract void say ();//define abstract method}class x extends b{//x class line inherit type B public void say () {System.out.println ("Hello world!!!");} Public String GetInfo () {return "HELLO";} public void print () {System.out.println ("" + AUTHOR);}}; public class Interfacedemo05{public static void Main (String args[]) {x x = new X ();//Instantiate Subclass Object X.say (); X.print ();}};
Inheritance of Interfaces
An interface cannot inherit an abstract class, but it can inherit multiple interfaces at the same time through the extends keyword, implementing multiple inheritance of the interface.format: Interface Sub-interface extends parent interface A, parent interface B, ... {}
Interface a{//defines the interface apublic String AUTHOR = "刘勋";//global constant public void PrintA ();//abstract method}interface b{public void Printb ();} Interface C extends a,b{public void Printc ();} Class X implements c{//X line inherits Class B public void PrintA () {System.out.println ("A, Hello World!!!");} public void Printb () {System.out.println ("B, Hello JAVA");} public void Printc () {System.out.println ("C, Hello LX");}}; public class Interfacedemo06{public static void Main (String args[]) {x x = new X ();//Instantiate Subclass Object X.printa (); X.printb (); x.pri NtC ();}};

Summary of the interface:1, just elaborated the basic concept of the interface, but the actual application has not elaborated. 2. The interface is a special class that contains only global constants and abstract methods. Abstract methods in an interface can be added without abstract and abstract methods in abstract classes must have an abstract keyword declaration. 3, a class can inherit only one parent class, but can implement multiple interfaces at the same time. 4, an interface can inherit multiple interfaces at the same time to achieve multiple inheritance of the interface. 5, interfaces and abstract classes, must rely on subclasses. 6, an abstract class can implement multiple interfaces, but an interface; cannot inherit an abstract class. the polymorphism of the objectPolymorphism : Polymorphism is an important concept in object-oriented, and there are two main types of object-oriented in Java:the overloads and overrides of the method. the polymorphism of the object. object polymorphism is mainly applied to abstract classes and interfaces. the polymorphism of the object is mainly divided into the following two types:transition up: The child object----the parent object. For the upward transformation, the program will automatically complete the format:Object Transformation: Parent Class object = Subclass instance;transition Down: The parent object--The child class object. For a downward transition, you must explicitly indicate the type of subclass that you want to transform, in the format:Object Downward transformation: Subclass Subclass Object = (subclass) Parent class instance
Class a{//defines classes apublic void Fun1 () {//define FUN1 () method System.out.println ("A-to-public void fun1 () {}");} public void Fun2 () {this.fun1 ();//Call Fun1 () method}};class B extends a{public void fun1 () {//This method covers System.out.println ("B-- > public void Fun1 () {} ");} public void Fun3 () {System.out.println ("B--and public void Fun3 () {}");}}; public class Poldemo01{public static void Main (String asrgs[]) {b b = new B ();//Instantiate sub-class object A = B;//Upward Transition relationship a.fun1 ();//This method Quilt category covered//a.fun3 ();        Error: After the subclass has been transformed, it is not possible to invoke the method or property of the subclass extension}};


for the above program, the parent class object is instantiated through its subclasses, then the overridden method is definitely called if the method being called is overridden by the quilt class. Note: After the transformation, the new method defined in the subclass cannot be found because the parent class object is being manipulated. Turning the parent class object into a subclass object becomes a downward transition, which requires a coercive approach.
Class a{//defines classes apublic void Fun1 () {//define FUN1 () method System.out.println ("A-to-public void fun1 () {}");} public void Fun2 () {this.fun1 ();//Call Fun1 () method}};class B extends a{public void fun1 () {//This method covers System.out.println ("B-- > public void Fun1 () {} ");} public void Fun3 () {System.out.println ("B--and public void Fun3 () {}");}}; public class Poldemo02{public static void Main (String asrgs[]) {A A = new B ();//Upward transformation Relationship b b = (b) A;//A downward transition relationship b.fun1 (); B . fun2 (); B.fun3 ();};

There are three methods in class B, so all can be called.However, you must be aware of one thing when you perform an object-down transition:
Class a{//defines classes apublic void Fun1 () {//define FUN1 () method System.out.println ("A-to-public void fun1 () {}");} public void Fun2 () {this.fun1 ();//Call Fun1 () method}};class B extends a{public void fun1 () {//This method covers System.out.println ("B-- > public void Fun1 () {} ");} public void Fun3 () {System.out.println ("B--and public void Fun3 () {}");}}; public class Poldemo03{public static void Main (String asrgs[]) {A A = new A ();//Instantiate a parent class object b b = (b) A;//a downward transition relationship b.fun1 ( ); b.fun2 (); B.fun3 ();}};

The above exception is the second largest occurrence of the exception, this exception occurs when the object transformation occurs frequently, if the two non-related objects have a conversion relationship, this exception will certainly occur.that is, if you want to create a downward transformation of an object, you must first create an upward transition relationship. A = new B (); Build relationships. application of Object polymorphism:Requirement: Design a method that can receive any subclass object of Class A and invoke the method. implementation one: Do not use the polymorphism of the object to complete. Use overloading to complete.
Class a{//defines classes apublic void Fun1 () {//define FUN1 () method System.out.println ("A-to-public void fun1 () {}");} public void Fun2 () {this.fun1 ();//Call Fun1 () method}};class B extends a{public void fun1 () {//This method covers System.out.println ("B-- > public void Fun1 () {} ");} public void Fun3 () {System.out.println ("B--and public void Fun3 () {}");}}; Class C extends A{public void fun1 () {//This method covers System.out.println ("C--and public void Fun1 () {}");} public void Fun5 () {System.out.println ("C--and public void Fun5 () {}");}; The public class poldemo04{public the static void Main (String asrgs[]) {Fun (new B ()));//passing B's instance fun (new C ());//Passing the instance of B}public Stati c void Fun (b b) {b.fun1 ();//Call Overwrite the Fun1 () method in the parent class}public static void Fun (c c) {c.fun1 ();//Call Overwrite the Fun1 () method in the parent class}};

If you follow the above idea, each additional subclass, then the fun method must be overloaded once, when the subclass very long will be extremely cumbersome.at this point, to solve this problem, you can use the polymorphism of the object to complete the operation.
Class a{//defines classes apublic void Fun1 () {//define FUN1 () method System.out.println ("A-to-public void fun1 () {}");} public void Fun2 () {this.fun1 ();//Call Fun1 () method}};class B extends a{public void fun1 () {//This method covers System.out.println ("B-- > public void Fun1 () {} ");} public void Fun3 () {System.out.println ("B--and public void Fun3 () {}");}}; Class C extends A{public void fun1 () {//This method covers System.out.println ("C--and public void Fun1 () {}");} public void Fun5 () {System.out.println ("C--and public void Fun5 () {}");}; The public class poldemo05{public the static void Main (String asrgs[]) {Fun (new B ()));//passing B's instance fun (new C ());//Passing the instance of B}public Stati c void Fun (a a) {a.fun1 ();//Call Overwrite the Fun1 () method in the parent class}};

In this way, the code can be done easily, regardless of the number of subclasses.
A summary of polymorphism:1. The concept of object polymorphismtransition up: auto-complete. downward transformation: coercion. You must have an upward transition relationship before a downward transition occurs. 2, the object polymorphism can solve the problem of the method receiving parameters.

Object-oriented advanced--the basic concept of interface and the polymorphism of objects

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.