Java Object-oriented fourth chapter

Source: Internet
Author: User

First, basic knowledge of interfaces

1. Why interfaces are required

To solve the problem that single inheritance cannot solve in practical application

2. What is an interface

A) Basic concepts?

Interface is a special kind of abstract class

An interface is a specification and a standard that can constrain the behavior of a class, which is a collection of method features, but the method has no method body

b) Basic grammar

Access modifier Interface Interface Name {

Defines the method (without the method body), and the method's access adornment defaults thinks public

Defining properties (default is public static constants)

Data Type Constant name = value;

}

access modifier can only be public and the default

c) precautions?

1. Interface name, interface Word must be the same as file name

2. The method in the interface has no method body

3. The attribute name in the interface must be capitalized and assigned a value

4. Interface cannot be instantiated, no construction method

5. Interfaces support multiple inheritance, an interface can inherit multiple interfaces, but cannot inherit classes

6. The method in the interface must be implemented specifically by all classes that implement the interface

 Public Abstract classHandest {PrivateString brand;//Brand        PrivateString type;//Model                 PublicString Getbrand () {returnbrand; }                 PublicHandest (String brand,string type) {//a method for constructing a parameter             This. Brand =brand;  This. type=type; }                 PublicString GetType () {returntype; }                         Public Abstract voidprint ();  Public Abstract voidfunction ();} Parent class
Parent Class
 Public Interface Network {    void surf ();        // Internet Access }
// Interface 2  Public Interface playwiring {    void play ();        // playback function }
// Interface 3  Public Interface takepictures {    void takepictures ();        // Photo }
// Interface 4  Public Interface Videocall {        void visualtelephone ();        // Video Phone }
 Public classAptitudehandset extends Handest implements Playwiring,network,takepictures,videocall { PublicAptitudehandset (string brand, String type) {Super (brand, type); } @Override Public voidVisualtelephone () {System. out. println ("start a video call ..."); } @Override Public voidtakepictures () {System. out. println ("To be clicked on ...  Photo Success"); } @Override Public voidSurf () {System. out. println ("mobile network started ..."); } @Override Public voidPlay () {System. out. println ("start playing video "Little Times" ..."); } @Override Public voidprint () {System. out. println ("This is a model for"+super.gettype () +"of the"+super.getbrand () +"Mobile:"); } @Override Public voidfunction () {System. out. println ("send a message with pictures and text ..."); }} Subclass
sub-class
 Public Static void main (String [] args) {                aptitudehandset a=new aptitudehandset ("I9100" ,"HTC");        A.print ();        A.surf ();        A.play ();        A.takepictures ();        A.function ();        A.visualtelephone ();    } Test class
Test Class

3. How a class implements an interface

Class name implements interface name {

The method in the interface must be implemented, specifically

}

4, one class how to inherit another class, and simultaneously implement an interface "Let 1 classes implement multiple inheritance in disguise"

Class subclass name extends parent class name implements Interface 1, ... Interface n{

}

5, the advantages of the interface?

The interface embodies the principle that the agreement and the realization phase separate

It makes up the deficiency of Java class single inheritance, and facilitates the extension and maintenance of code.

6. What are the similarities and differences between abstract classes and interfaces? "Common pen questions"

Same point

    1. 1. Neither of them can be instantiated
    2. 2. both contain abstract methods that must be overridden or implemented by non-abstract subclasses
    3. 3. Both can be used to achieve polymorphic

Different points:

1, abstract class can only inherit single, and interface can support multiple inheritance

2, abstract class in addition to abstract methods, but also can have a non-abstract method; The method in an interface is only an abstract method

3, the abstract class can have a construction method, the interface does not

4, the variables in the abstract class can not be assigned value, the variables in the interface must be assigned

5, the abstract class can define a static method, the interface can not

 Public Abstract classPet {PrivateString name; Private intHealth ; Private intLove ;  PublicPet (String name,intHealthintLove ) {             This. name=name;  This. health=Health ;  This. Love =Love ; }         PublicString GetName () {returnname; }                 Public intGethealth () {returnHealth ; }                 Public intGetlove () {returnLove ; }                 Public Abstract voidprint ();} Parent class
Parent Class
// Interface 1  Public Interface eatable {    void eat ();        // Dinner }
// Interface 2  Public Interface flyingcatchable {    void catchingfly ();        // pick up the Frisbee }
 Public classDog1 extends Pet implements eatable,flyingcatchable{ PublicDog1 (String name,intHealthintLove )        {Super (name, health, Love); //TODO auto-generated Constructor stub} @Override Public voidprint () {//TODO auto-generated Method StubSystem. out. println ("My name"+super.getname () +", the health value is:"+super.gethealth () +", with the intimacy of the master as:"+super.getlove () +". "); } @Override Public voidCatchingfly () {//TODO auto-generated Method StubSystem. out. println ("I'm playing frisbee with my master! "); } @Override Public voideat () {//TODO auto-generated Method StubSystem. out. println ("I'm having dinner! "); }} Subclass
sub-class
 Public class Pettext {    publicstaticvoid  main (String [] args) {        Dog1 d=  New Dog1 (" good ", ------);        D.print ();        D.eat ();        D.catchingfly ();            }

Second, interface-oriented programming

1, the interface represents a capability

2, the interface represents a convention

3. What are the benefits of interface-oriented programming?

A) separation of the implementation interface and implementation

b) can reduce the coupling between the code

c) Increased scalability and maintainability of the program

Java Object-oriented fourth chapter

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.