The object-oriented interface of Java learning notes

Source: Internet
Author: User

Object-Oriented Interface summary

Interface Understanding:

An interface is a collection of functions, and the definition of an interface uses a. java file, which also produces a. class file after compilation. The definition of a class uses class, whereas the definition of an interface uses interface;

Define the format:
1  Public Interface Interface Name {2     // constant Definition Format: public static final constant name (typically uppercase) 3     Member variable, constant 4     // abstract method definition: Public abstract  return value type  method name (parameter list)  the methods in the interface are all abstract methods 5     Member method, abstract method 6}7// Note: constants and modifiers for abstract methods are invariant, and write-and-write modifiers are the same , you can omit the modifier

Interface Definition Demo:

1  Public Interface myinterface{2    // Defining Constants 3    Public Static Final int time=133; 4   // Defining abstract methods 5    Public Abstract void Show ();         6 }
Usage rules for interfaces:

1. Interfaces cannot create objects

2. Interfaces use can define implementation classes, implement interfaces, override abstract methods in interfaces, create implementation class objects using

To implement the definition of a class:

Interface implementation of the relationship between class and interface is the implementation of the relationship, that is, class implementation interface, using the keyword implements

Format:

Modifier  class   class name  implements  {   overrides all methods in the interface;}
after the class implements the interface, the class will inherit all the abstract methods in the interface, and this class will need to rewrite the abstract method before the object can be created, or the subclass is an abstract class;
Features of the interface:

supports a class in 1.java to implement multiple interfaces at the same time

There is no security risk, the methods in the interface are abstract methods, there is no method body, the implementation class needs to rewrite the abstract methods in all interfaces;

Demo

 Public  class  Aandbimpl  implements  a,b{// rewriting methods in A and B;}

2.java supports a class to inherit the parent class, you can implement multiple interfaces (a person can have a father, a number of father-in-God)

Subclasses to override abstract methods in the parent class and interface

Demo

public class Student extends  person  implements  eat,play{//overrides all abstract methods in the parent class and interface}

3. The relationship between interfaces and interfaces is an inherited relationship, and an interface can inherit multiple interfaces at the same time

C interfaces inherit classes A and B interfaces, which is equivalent to all abstract methods in C that have the class ABC three interface;

 Public Interface C  extends interface A, interface b{}

Summarize:

1. The relationship between classes and classes is inherited;

 Public class  extends  b{}

2. The relationship between classes and interfaces is the implementation relationship:

 Public class Aimpl  implements b{}

3. An inheritance relationship between an interface and an interface:

 Public Interface extends b,c{}

The object-oriented interface of Java learning notes

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.