Java class inheritance and interface inheritance

Source: Internet
Author: User

When I read design patterns-the basis for reusable object-oriented software, I am confused about the difference between class inheritance and interface inheritance. There is no explanation for searching. Here is my understanding!

1. Two important paragraphs in Design Patterns

--------------

An object class defines how the object is implemented, as well as its internal states and operations. However, the object type is only related to its interface, which is the set of requests that the object can respond.

--------------

Class inheritance defines the implementation of another object based on the implementation of one object. In short, it is a sharing mechanism of code and representation. However, interface inheritance describes when an object can be used to replace another object.

-------------

2. Understanding of two paragraphs

The above two paragraphs solve my understanding of the difference between class inheritance and interface inheritance.

We all know that the interface reference can be the Instance Object of the implementation class, and the base class reference can also be the Instance Object of the subclass. However, the fundamental difference is that the interface implementation class must implement all interfaces of the interface, this is why "interface inheritance" describes when an object can be used to replace another object. "if" the implementation class does not implement all functions of the interface, therefore, it cannot be referenced as an interface.

In class inheritance, the content of the base class does not appear in the subclass or is controllable with any permissions. It is also possible for the subclass to have/override functions of the parent class. The difference is that the modifier of the Java Rule interface function must be public/abstract, the modifier of the base class function can be public/protected/private/default.

3. Example

1) assume there is an interface base:

package base;public interface Base {public void show();public void cout();public void print();}

2) subinterface Baset inherits base:

Package base; public interface Baset extends base {public void println (); // Add a function}

3) Implementation class of the interface base:

package impl;import base.Base;public class BaseImpl implements Base {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub}@Overridepublic void show() {// TODO Auto-generated method stub}@Overridepublic void cout() {// TODO Auto-generated method stub}@Overridepublic void print() {// TODO Auto-generated method stub}}

4) Implementation class of interface Baset:

package impl;import base.Base;import base.BaseT;public class BaseTImpl implements BaseT{/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubBase base1 = new BaseImpl();base1.print();BaseT base2 = new BaseTImpl();Base base3 = new BaseImpl_T();}@Overridepublic void show() {// TODO Auto-generated method stub}@Overridepublic void cout() {// TODO Auto-generated method stub}@Overridepublic void print() {// TODO Auto-generated method stubSystem.out.println("baseTImp");}@Overridepublic void println() {// TODO Auto-generated method stub}}

As you can see, the interface can be inherited, and the Implementation class of the interface must implement all the methods of the interface it implements. With this, you can strictly control the functions that implement the class: if it does not implement a method of the interface, it is regarded as illegal and cannot be used as an interface reference.

5) base class basec:

package base;public class BaseC {public void show(){}public void cout(){}public void print(){}}

6) subclass basect:

package impl;import base.BaseC;public class BaseCT1 extends BaseC{@Overridepublic void print() {// TODO Auto-generated method stubSystem.out.println("BaseCT1");}public void println(){}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubBaseC baseC = new BaseCT1();}}

Ordinary class inheritance does not have this restriction.

4. Summary

If the application is released using the interface that others implement our interface, and suddenly we want to add a new function for this interface, this will affect the implementation of all inherited interfaces. If the class is inherited, this problem will not occur.

Related Article

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.