Java Object-oriented interface

Source: Internet
Author: User

    1. Interface (interface):

      The interface is the same as class, in other words all methods in the interface are abstract methods. The abstract keyword can be used when declaring a method in an interface, or it may not, typically omitting the abstract keyword

    2. Interfaces can be considered as special abstract classes (abstract classes can have specific methods or abstract methods, and interfaces can only have abstract methods, not specific methods)

    3. class can implement interfaces. The implementation uses the keyword implement representation, which represents a class that implements an interface

    4. A class implements an interface. Then the class must implement all the methods declared in the interface, and if the class is an abstract class, then there is no need to implement the methods in the interface

    5. Java is a single-inheritance, that is, a class can only have a single parent class, a class may implement multiple interfaces, multiple interfaces separated by commas

Package com.yuanzijain02;interface myinterface{public void output ();} Interface myinterface2{public void Output2 ();} Class MyClass implements MyInterface, myinterface2{public void output () {System.out.println ("Output");} @Overridepublic void Output2 () {//TODO auto-generated method stub System.out.println ("Output2");}} public class Test3 {public static void main (string[] args) {MyClass MyClass = new MyClass (); Myclass.output (); MYCLASS.OUTPU T2 ();}}

6. A class can inherit a parent class at the same time, while implementing multiple interfaces

Package com.yuanzijain02;interface myinterface{public void output ();} Interface myinterface2{public void Output2 ();} Class Myparent{public void Output3 () {System.out.println ("Output3");}} Class MyClass extends Myparent implements MyInterface, myinterface2{public void output () {System.out.println ("Output");} @Overridepublic void Output2 () {//TODO auto-generated method stub System.out.println ("Output2");}} public class Test3 {public static void main (string[] args) {MyClass MyClass = new MyClass (); Myclass.output (); MYCLASS.OUTPU T2 (); Myclass.output3 ();}}

7. Polymorphism: A so-called polymorphic refers to a reference to a parent type that can point to an object of a subtype, or an application of an interface type can point to an instance of a class that implements the interface, as in the case of a forced type conversion between classes that implement an interface and the coercion of a class between a parent class and a subclass.

Package Com.yuanzijain03;public class Test4 {public static void main (string[] args) {//up type conversion/*aa BB = new BB ();//reference to interface type Point to Bb.output (); *///down type convert/*aa AA = new BB (); bb bb = (BB) aa;bb.output (); */}}interface aa{public void output ();} Class BB implements aa{public void output () {System.out.println ("BB");}}


Java Object-oriented interface

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.