Interfaces and Inheritance 接口与继承

 

一、接口

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.

 

接口定义

类似:
public interface DoIt { void doSomething(int i, double x); int doSomethingElse(String s); boolean didItWork(int i, double x, String s); }

接口实现

 

public class RectanglePlus implements DoIt{ public int width = 0; public int height = 0; public Point origin;
....
}

接口更改
如果要在接口中增加某个方法的办法为:
1、继承原来的类创建一个新的接口,在新的类中定义新的方法.
If you want to add additional methods to an interface, you have several options. You could create a DoItPlus interface that extends DoIt
2、you can define your new methods as default methods.
3、You could also define new static methods to existing interfaces.

 

接口的其他要求:

1、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.

2、A class that implements an interface must implement all the methods declared in the interface.

3、An interface name can be used anywhere a type can be used.

二、继承

A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

 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.

三、接口与继承的区别

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

2、In addition, you can instantiate a class to create an object, which you cannot do with interfaces.

3、Inherited instance methods from classes can override abstract interface methods

四、多态

 Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

虽然各子类继承自同一父类的同一个功能,但是各子类可以定义各自独一无二的行为或者功能。

Note the overridden printDescription method. In addition to the information provided before, additional data about the suspension is included to the output.

多态在子类中重新编写父类中的某个函数,除了使用前面父类中功能为,额外增加自己独有的功能。

五、在子类中如何调用父类方法和变量

Using the Keyword super Accessing Superclass Members;

super.superclass method or filed;

super(); super(parameter list); 调用父类的构造函数。

六、公共类或者方法

The  Object class, in the java.lang package, sits at the top of the class hierarchy tree.

Every class you use or write inherits the instance methods of Object. You need not use any of these methods, but, if you choose to do so, you may need to override them with code that is specific to your class.

有很多方法已经写好,可以供我们调用,超过50个。下面是一些常见的方法:

protected Object clone() throws CloneNotSupportedException       Creates and returns a copy of this object.

  • public boolean equals(Object obj)       Indicates whether some other object is "equal to" this one.
  • protected void finalize() throws Throwable       Called by the garbage collector on an object when garbage       collection determines that there are no more references to the object
  • public final Class getClass()       Returns the runtime class of an object.
  • public int hashCode()       Returns a hash code value for the object.
  • public String toString()       Returns a string representation of the object.

七、final

如果我们写的类或方法不想让覆盖或者重写,那么可以加 final 关键字。

You can declare some or all of a class's methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final.

八、抽象类

父类只声明方法,不可以实例化。子类必须要进行方法实例化,除非使用abstract进行声明。

 

Summary of Inheritance

Except for the Object class, a class has exactly one direct superclass. A class inherits fields and methods from all its superclasses, whether direct or indirect. A subclass can override methods that it inherits, or it can hide fields or methods that it inherits. (Note that hiding fields is generally bad programming practice.)

The table in Overriding and Hiding Methods  section shows the effect of declaring a method with the same signature as a method in the superclass.

The Object class is the top of the class hierarchy. All classes are descendants from this class and inherit methods from it. Useful methods inherited from Object include toString(), equals(), clone(), and getClass().

You can prevent a class from being subclassed by using the final keyword in the class's declaration. Similarly, you can prevent a method from being overridden by subclasses by declaring it as a final method.

An abstract class can only be subclassed; it cannot be instantiated. An abstract class can contain abstract methods—methods that are declared but not implemented. Subclasses then provide the implementations for the abstract methods

转载于:https://www.cnblogs.com/clebean/p/7592273.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值