12-16 object-oriented interfaces and application of abstract classes

Source: Internet
Author: User

1. Abstract class and interface instantiation

In Java , it is possible to instantiate an abstract class and an interface through the polymorphism of the object, so that when you use the abstract class and interface, you can use the Overwrite method of the subclass.

Abstract classes and interface classes cannot be instantiated directly because they contain various abstract methods, but are not implemented and cannot be called. By polymorphism, the subclass is transformed upward, and all of the methods that are called are overridden methods.

This procedure is to continue instantiating an abstract class and an interface, abstract class a//, defining abstraction classes {public abstract void PrintA ();//define abstract methods S}interface b//define interface {public abstract void Printb ();//define an abstraction method}class C extends A implements B{public void PrintA ()//overwrite method {System.out.println in abstract class (" This is the method of abstract Class A ");} public void Printb ()//overwrite method in interface {SYSTEM.OUT.PRINTLN ("This is the method of Interface B");}} public class Test06 {public static void main (string[] args) {A A = new C ();//Instantiate the subclass object and pass it up B = new C (); A.printa ();/Invoke abstract class Method B.printb ();//method that invokes the interface}}

If you want to use abstract classes and interfaces, you can only follow these steps.

2. Application of abstract class--Define Template
This program is an abstract class-definition template operation abstract class Person{private String name;p rivate int;p ublic person (String name, int. name = name; this.age = age;} Public String GetName () {return this.name;} public int getage () {return this.age;} public void Say ()//Speaking is a specific feature of {System.out.println (This.getinfo ());}  Public abstract string GetInfo ();//content sub-class decision}class Student extends Person{private string school;p ublic Student (string name , int age, String school) {Super (name, age); this.school = School;}  Public String GetInfo ()//overwrite {return "name:" + this.getname () + ", Age:" + this.getage () + ", school:" +this.school; }}class worker extends person{private int salary; public worker (String name, int. int, int salary) {Super (name, age); thi S.salary = salary;}  Public String GetInfo ()//overwrite {return "name:" + this.getname () + ", Age:" + this.getage () + ", Salary:" +this.salary; }}public class Test06 {public static void main (string[] args) {Student s = new Student ("SS", "N", "Dsad"); Worker wor = new Worker ("DD", 33,100); Person Per1= S; Person per2 = Wor;per1.say ();p Er2.say ();}}

At this time to extrapolate the life of the template design, the essence of the core: given the design of the framework, the need for different applications to the framework of the object to add things.

3. Actual reference to the interface--setting standards
This procedure is an interface to develop standard operation interface Usb{public abstract void start ();p ublic abstract void Stop ();} class Computer{public static void plugin (USB USB)//PC can use interface {Usb.start (); SYSTEM.OUT.PRINTLN ("Computer plugged in USB"); Usb.stop ();}} Class Flash implements Usb{public void Start () {System.out.println ("USB stick Boot");} public void Stop () {System.out.println ("USB stick Stop");}} Class Printer implements Usb{public void Start () {System.out.println ("printer Start");} public void Stop () {System.out.println ("printer Stop");}} public class Test06 {public static void main (string[] args) {flash f = new Flash (); Printer p = new Printer (); Computer com = new computer (); Com.plugin (f); Com.plugin (P);}}

Program Interpretation:

Interface Interface defines a standard that can be used by either the U - disk or the printer to invoke the standard.

In each object, overwrite the USB standard specific content.

Extension: Is the key a kind of interface, everyone belongs to an object, the person has the key to be able to open the door.

1. Factory design mode
This procedure is a factory design mode operation interface Fruit {public abstract void Eat ();} Class Apple implements Fruit {public void Eat () {System.out.println ("Eat Apple");}} Class Orange implements Fruit {public void Eat () {System.out.println ("eating Oranges");}} public class Test06 {public static void main (string[] args) {Fruit a = new Apple (); Fruit B = new Orange (); A.eat (); B.eat ();}}

But the program has a problem, themain method is more a client, not responsible for the production of apples, just specify Apple. The subclass that you want to manipulate is specified directly in the main method, and if you want to replace the subclass, you must modify the client.

Tightly coupled with a particular subclass.

This procedure is a factory design mode operation interface Fruit {public abstract void Eat ();} Class Factory{public static Fruit getfruit (String name) {Fruit f = null;if ("Apple". Equals (name)) {f = new Apple ();} if ("Orange". Equals (name)) {f = new Orange ();} return f;}} Class Apple implements Fruit {public void Eat () {System.out.println ("Eat Apple");}} Class Orange implements Fruit {public void Eat () {System.out.println ("eating Oranges");}} public class Test06 {public static void main (string[] args) {new Factory (). Getfruit (Args[0]). Eat ();}}

Among them, in the factory, in order to determine whether the Apple class equals method of the order is very fastidious.

2. Proxy design mode

Proxy Internet server in the life

This procedure is a factory design mode operation interface Network {public abstract void browse ();} Class Real implements network{public void browse () {System.out.println ("Internet browsing Information");}} Class  Proxy implements Network{private network network;//proxy object public proxy (network network) {this.network = network;} public void Check () {System.out.println ("user-valid");} public void browse () {This.check (); This.network.browse ();}} public class Test06 {public static void main (string[] args) {Network net = new Proxy (new Real ());//Specify Proxy Net.browse ();}}

3.Adapter Design

Specific ideas: More methods are declared in the interface, but the actual use is only part of it.

This program is an operation of the adapter design mode interface  Window {public abstract Void open ();p ublic abstract void Close ();p ublic abstract void expand ();} Abstract class Windowadapter implements Window{public Void Open () {};p ublic void Close () {};p ublic void expand () {};} Class Win extends windowadapter{public void open () {System.out.println ("open");}} public class Test06 {public static void main (string[] args) {Window  x = new Win (); X.open ();}}

This design idea is used very much in Java's graphical interface.

4. Extension of the Inner class

The concept of an inner class is explained before, and it is actually possible to include an interface in an abstract class

This procedure is an internal class extension operation abstract class A{public abstract void PrintA (); interface b{public abstract void Printb ();}} Class Exp extends A{public void PrintA () {System.out.println ("A printing Method"); Class X implements B{public void Printb () {System.out.println ("B Print Method");}}} public class Test06 {public static void main (string[] args) {exp.x aa= new Exp (). New X (); A.B a = AA;A.PRINTB ();}}

Conversely, an abstract class is defined in an access port.

From a practical point of view, this design is not common and the code structure is confusing.


I wish you all a healthy and happy. Tomorrow will not update the need for feedback consolidation.








12-16 object-oriented interfaces and application of abstract classes

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.