Java Basic系列之(七):Interfaces and Inheritance接口与继承

原文链接

Interfaces

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

  • 接口的作用:规范行为。
  • Java中的Type关键字:interface和class
  • To use an interface, you write a class that implements the interface.
  • public interface,如果不声明public,accessible only to classes defined in the same package。
  • The interface body can contain abstract methods, default methods, and static methods.
  • abstract method没有大括号,只有声明语句。这些方法都是public的,因此不用写public。接口中的常量默认是public, static, and final。
default method
  • 如果你想在接口中新增method,可以有几个选择:(1)You could create a XXPlus interface that extends XX,(2)Alternatively, you can define your new methods as default methods。Note that you must provide an implementation for default methods。你必须实现你的default方法。(3)You could also define new static methods to existing interfaces.
  • Default方法可以让你在接口中增加新功能。
  • 当你继承一个包含default方法的接口时:若不重写,就默认继承了该方法,可重新定义该方法为抽象方法,可重写该方法(override)。
static method

静态方法与class绑定,而不是object。

A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.

default方法的出现极大的方便了lambda的使用。

Default methods enable you to add new functionality to existing interfaces and ensure binary compatibility with code written for older versions of those interfaces. In particular, default methods enable you to add methods that accept lambda expressions as parameters to existing interfaces.

Summary of Interfaces

  • An interface declaration can contain method signatures, default methods, static methods and constant definitions. The only methods that have implementations are default and static methods.接口可以包括方法签名,默认方法,静态方法和常量。只有默认方法和静态方法才能有方法体。
  • A class that implements an interface must implement all the methods declared in the interface.实现接口的类必须实现接口的所有抽象方法。
  • An interface name can be used anywhere a type can be used.接口可用于任何type能被用到的地方。

Inheritance

  • subclass和superclass
  • 没有父类的类默认继承自Object类。

    In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

  • 子类可继承所有父类的成员,除了构造函数。

    A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

What You Can Do in a Subclass

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:

  • The inherited fields can be used directly, just like any other fields.
  • You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
  • You can declare new fields in the subclass that are not in the superclass.
  • The inherited methods can be used directly as they are.
  • You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
  • You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
  • You can declare new methods in the subclass that are not in the superclass.
  • You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

Private Members in a Superclass

  • A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
  • A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.

区别

  • One significant difference between classes and interfaces is that classes can have fields whereas interfaces cannot.

静态方法和实例方法

Instance Methods

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass’s method.

Static Methods

If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

区别

The distinction between hiding a static method and overriding an instance method has important implications:

  • The version of the overridden instance method that gets invoked is the one in the subclass.
  • The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.
类的方法和接口方法有冲突时
  • Instance methods are preferred over interface default methods.实例方法优先于接口方法。
  • Methods that are already overridden by other candidates are ignored. This circumstance can arise when supertypes share a common ancestor.最近继承原则。
  • 显示声明你要调用哪个接口的方法。super关键字。OperateCar.super.startEngine(key)
  • Inherited instance methods from classes can override abstract interface methods. 从类继承的实例方法可以重写接口中的抽象方法。
    • 通过super关键字可以调用父类的方法。如果子类的构造函数不显式调用父类的构造函数,编译器会自动调用父类的无参构造函constructor chaining。如果父类没有无参构造函数,编译器会报错compile-time error
clone()

Object’s implementation of the clone() method creates an object of the same class as the original object and initializes the new object’s member variables to have the same values as the original object’s corresponding member variables.

final
  • You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses.
  • A class that is declared final cannot be subclassed.
Abstract Methods and Classes
  • An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
  • An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon)。
  • If a class includes abstract methods, then the class itself must be declared abstract,
  • When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值