Can an interface inherit from another interface? Can an abstract class implement an interface?

Source: Internet
Author: User

All can; one interface can inherit another interface? Interface inheritance Interface The essence is that an abstract class inherits another abstract class (an instance of which neither method is written). When a class inherits an abstract class, it must override the abstract method in the parent abstract class, and if it does not rewrite the instance of the parent abstract method, then the class is also an abstract class (this abstract subclass can be removed from the abstract fate until an instance of the abstract method is overridden). In fact, we think that a class to implement an interface is not an entity subclass an abstract so abstract method instantiation (overriding the abstract parent class all abstract methods), of course, abstract classes and interfaces are still different. I wrote about the difference between abstract class and interface, which is interesting to look at. speaking of which, in fact, we can ask ourselves a question, whether the abstract class can inherit the interface? We know the answer, OK? Is it only the hung Ming can set fire, not let our programmers "a little idea". If an abstract class can inherit an interface, we can either give the interface a realistic example, or leave it to the subclass (the subclass of the abstract class) to rewrite it. Two . Can an abstract class implement an interface?    This is the question we can think about, in order to implement the interface, then we have to write in this implementation class (this abstract class) inside the interface method of the instance. Can an instance of a method exist in an abstract class? Everyone should have seen the concept of abstract class, the abstract class can exist instances.  So we can explain that an abstract class can implement an interface.   Of course, we can also not in this abstract class in the realistic example, pass him to this abstract class subclass to write, this is no problem.     Below I introduce the abstract class and interface, hope to be able to understand the above paragraph understanding point, of course, if there is said wrong place hope that the big God pointing twos, younger brother is just a rookie. A class that declares the existence of a method and does not implement it is called an abstract class, which is used to create a class that embodies some basic behavior, declares a method for that class, but does not implement the class in that class (but it is not allowed to write). An instance of the abstract class cannot be created. However, you can create a variable whose type is an abstract class that points to an instance of a specific subclass. Cannot have abstract constructors or abstract static methods. The subclasses of the abstract class provide implementations for all abstract methods in their parent class, otherwise they are also abstract classes. Instead, implement the method in the subclass. Other classes that know their behavior can implement these methods in the class. an interface (interface) is a variant of an abstract class. In an interface, all methods are abstract. Multiple inheritance can be obtained by implementing such an interface. All the methods in the interface are abstract, without a program body. An interface can only define static final member variables. The implementation of an interface is similar to a subclass, except that the implementation class cannot inherit the behavior from the interface definition. When a class implements a special interface, it defines the method (which is given by the program body) to all such interfaces. It can then invoke the interface's method on any object that implements the interface's class. Because of an abstract class, it allows you to use the interface name as the type of the reference variable. The usual dynamic binder will take effect. A reference can be converted to an interface type or converted from an interface type, and the instanceof operator can be used to determine whether the class of an object implements an interface. Here's an example to deepen your understanding of memory.
 Public Abstract classBasephoneImplementsphoto{/*** Photo is an interface, it has an abstract method of taking pictures * Our phone has its own call and the abstract behavior of the message, we call and send the message as the whole function of the phone thing * We designed abstract classes to describe a thing, the interface is to put things between each The behaviors we have are put together so that we can invoke them. * We may summarize: * Abstract class is an abstraction of a thing, that is, an abstraction of a class, and an interface is an abstraction of a behavior*/     Public Abstract voidCall ();//Call     Public Abstract voidMessage ();//Send Message@Override Public voidphoto () {//This rewrite can not write, I just test will not error    }}
 Public Interface The variables in the Net    {// interface are implicitly specified as public static final variable    String net_type = "4G";     void net (String net_type);}
 Public Interface Photo {    void photo ();   photo Extraction }
 Public classPhone1extendsBasephoneImplementsNet {@Override Public voidCall () {System.out.println ("What's the matter, we'll contact Yo"); } @Override Public voidmessage () {SYSTEM.OUT.PRINTLN ("Send me a short message, I will return."); } @Override Public voidphoto () {System.out.println ("Beautiful 美图秀秀"); } @Override Public voidnet (String net_type) {System.out.println (net_type); }}

Test Class I will not write, you can play casually, if you want to call the interface constants, we can write:

Net net= new Phone1 ();
String a=net.net_type;//constants in the calling interface (a);

Can an interface inherit from another interface? Can an abstract class implement an 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: 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.