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

Source: Internet
Author: User

12-16 Application of object-oriented interfaces and abstract classes
1. abstract class and interface instantiation

In java, object polymorphism can be used to instantiate abstract classes and interfaces. In this way, when abstract classes and interfaces are used, override methods in sub-classes can be used.

Abstract classes and interface classes cannot be directly instantiated because they contain various abstract methods. abstract methods are not implemented and cannot be called. Through polymorphism, sub-classes undergo upward transformation, and all the methods called are overwritten.

 

// This Program is an operation to continue instantiation of abstract classes and interfaces abstract class A // define abstract classes {public abstract void printA (); // define the abstract method s} interface B // define the interface {public abstract void printB (); // define the abstract method} class C extends A implements B {public void printA () // override the method {System. out. println (this is the method of abstract class A);} public void printB () // override method in the 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 B B = new C ();. printA (); // call Method B of the abstract class. printB (); // Method for calling the interface }}

 

To use abstract classes and interfaces, you can only perform the preceding operations.

 

2. Application of abstract classes-definition templates
// This Program is an operation on the abstract class -- Definition template abstract class Person {private String name; private int age; public Person (String name, int age) {this. name = name; this. age = age;} public String getName () {return this. name;} public int getAge () {return this. age;} public void say () // speaking is a specific function {System. out. println (this. getInfo ();} public abstract String getInfo (); // determines the content and subclass} class Student extends Person {private String school; public 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 age, int salary) {super (name, age); this. 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, 22, dsad); Worker wor = new Worker (dd, 33,100); Person per1 = s; Person per2 = wor; per1.say (); per2.say ();}}

In this case, we need to draw a different picture of the template design in our daily life. Its core is to provide the framework in the design. We need to add things to the framework for different application objects.

3. Actual interface reference-develop standards
// This program is the interface to develop a standard operation interface USB {public abstract void start (); public abstract void stop ();} class Computer {public static void plugin (USB) // The computer can use the interface {usb. start (); System. out. println (USB on the computer); usb. stop () ;}} class Flash implements USB {public void start () {System. out. println (USB flash drive);} public void stop () {System. out. println (USB flash drive stopped);} class Printer implements USB {public void start () {System. out. println (printer startup);} public void stop () {System. out. println (Printer stopped);} 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 explanation:

The interface defines a standard that can be called by both USB flash drives and printer.

Overwrite the USB standard content in each object.

Extension: Is the Key an interface? Everyone belongs to an object. People can open the door with the key.

1. Factory Design Model
// This program is the operation interface Fruit {public abstract void eat ();} class Apple implements Fruit {public void eat () {System. out. println (Apple eating);} class Orange implements Fruit {public void eat () {System. out. println (Orange) ;}} public class Test06 {public static void main (String [] args) {Fruit a = new Apple (); Fruit B = new Orange (); a. eat (); B. eat ();}}

However, there is a problem with the program. The main method is mostly a client which does not generate apple, but only indicates apple. In this case, the subclass to be operated is directly specified in the main method. If you want to change the subclass, you must modify the client.

Closely coupled with a specific subclass.

// This program is the 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 (Apple eating);} class Orange implements Fruit {public void eat () {System. out. println (orange) ;}} 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 order of the apple equals method is very particular.

 

2. Proxy Design Mode

 

Proxy Internet Access Server in life

// This program is the 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 Network; // Proxy object public Proxy (network Network) {this. network = network;} public void check () {System. out. println (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 the Proxy. net. browse ();}}

3. adapter design

Specific idea: declare many methods in the interface, but the actual use is only a part.

 

// The operation interface Window {public abstract void open (); public abstract void close (); public abstract void expand ();} abstract class WindowAdapter implements Window {public void open () {}; public void close () {}; public 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 concept is widely used in the java GUI.

 

4. Internal class Extension

 

I have explained the concept of internal classes. Actually, an abstract class can contain interfaces.

// This Program 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 printing method) ;}} public class Test06 {public static void main (String [] args) {Exp. X aa = new Exp (). new X ();. B a = aa;. printB ();}}

 

Instead, define an abstract class in an interface.

From the perspective of actual development, this design is uncommon and the code structure is messy.

 

 






 

 

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.