请用通俗易懂的语言或文章链接进行解释。
当前回答
extends用于扩展类。
implements用于实现接口
接口和常规类之间的区别在于,在接口中不能实现任何声明的方法。只有“实现”接口的类才能实现方法。C++等价的接口将是一个抽象类(不完全相同,但几乎相同)。
此外,java不支持类的多重继承。这通过使用多个接口来解决。
public interface ExampleInterface {
public void doAction();
public String doThis(int number);
}
public class sub implements ExampleInterface {
public void doAction() {
//specify what must happen
}
public String doThis(int number) {
//specfiy what must happen
}
}
现在扩展类
public class SuperClass {
public int getNb() {
//specify what must happen
return 1;
}
public int getNb2() {
//specify what must happen
return 2;
}
}
public class SubClass extends SuperClass {
//you can override the implementation
@Override
public int getNb2() {
return 3;
}
}
在这种情况下
Subclass s = new SubClass();
s.getNb(); //returns 1
s.getNb2(); //returns 3
SuperClass sup = new SuperClass();
sup.getNb(); //returns 1
sup.getNb2(); //returns 2
此外,请注意,实现接口不需要@Override标记,因为原始接口方法中没有任何内容需要重写
我建议您对面向对象编程中的动态绑定、多态性和一般继承进行更多研究
其他回答
这两个关键字与继承直接关联,这是OOP的核心概念。当我们将某个类继承到另一个类时,我们可以使用extends,但当我们要将某些接口继承到我们的类时,不能使用extends时,我们应该使用implements,并且我们可以使用扩展关键字从另一个接口继承接口。
扩展:用于将父类的属性获取到子类中,并且可能包含已定义的方法,这些方法可以在子类中重写。
Implements:这用于通过在子类中定义接口来实现接口(仅具有函数签名而不具有函数定义的父类)。
有一个特殊的条件:“如果我希望一个新接口成为现有接口的子接口怎么办?”。在上述条件下,子接口扩展父接口。
通常实现用于实现接口,扩展用于扩展基类行为或抽象类。
extends:派生类可以扩展基类。您可以重新定义已建立关系的行为。派生类“是”基类类型
implements:您正在实现合同。实现接口的类“具有”功能。
在java8版本中,接口可以在接口中具有默认方法,这在接口本身中提供了实现。
请参阅此问题以了解何时使用它们:
接口与抽象类(通用OO)
理解事物的榜样。
public class ExtendsAndImplementsDemo{
public static void main(String args[]){
Dog dog = new Dog("Tiger",16);
Cat cat = new Cat("July",20);
System.out.println("Dog:"+dog);
System.out.println("Cat:"+cat);
dog.remember();
dog.protectOwner();
Learn dl = dog;
dl.learn();
cat.remember();
cat.protectOwner();
Climb c = cat;
c.climb();
Man man = new Man("Ravindra",40);
System.out.println(man);
Climb cm = man;
cm.climb();
Think t = man;
t.think();
Learn l = man;
l.learn();
Apply a = man;
a.apply();
}
}
abstract class Animal{
String name;
int lifeExpentency;
public Animal(String name,int lifeExpentency ){
this.name = name;
this.lifeExpentency=lifeExpentency;
}
public void remember(){
System.out.println("Define your own remember");
}
public void protectOwner(){
System.out.println("Define your own protectOwner");
}
public String toString(){
return this.getClass().getSimpleName()+":"+name+":"+lifeExpentency;
}
}
class Dog extends Animal implements Learn{
public Dog(String name,int age){
super(name,age);
}
public void remember(){
System.out.println(this.getClass().getSimpleName()+" can remember for 5 minutes");
}
public void protectOwner(){
System.out.println(this.getClass().getSimpleName()+ " will protect owner");
}
public void learn(){
System.out.println(this.getClass().getSimpleName()+ " can learn:");
}
}
class Cat extends Animal implements Climb {
public Cat(String name,int age){
super(name,age);
}
public void remember(){
System.out.println(this.getClass().getSimpleName() + " can remember for 16 hours");
}
public void protectOwner(){
System.out.println(this.getClass().getSimpleName()+ " won't protect owner");
}
public void climb(){
System.out.println(this.getClass().getSimpleName()+ " can climb");
}
}
interface Climb{
public void climb();
}
interface Think {
public void think();
}
interface Learn {
public void learn();
}
interface Apply{
public void apply();
}
class Man implements Think,Learn,Apply,Climb{
String name;
int age;
public Man(String name,int age){
this.name = name;
this.age = age;
}
public void think(){
System.out.println("I can think:"+this.getClass().getSimpleName());
}
public void learn(){
System.out.println("I can learn:"+this.getClass().getSimpleName());
}
public void apply(){
System.out.println("I can apply:"+this.getClass().getSimpleName());
}
public void climb(){
System.out.println("I can climb:"+this.getClass().getSimpleName());
}
public String toString(){
return "Man :"+name+":Age:"+age;
}
}
输出:
Dog:Dog:Tiger:16
Cat:Cat:July:20
Dog can remember for 5 minutes
Dog will protect owner
Dog can learn:
Cat can remember for 16 hours
Cat won't protect owner
Cat can climb
Man :Ravindra:Age:40
I can climb:Man
I can think:Man
I can learn:Man
I can apply:Man
需要理解的要点:
狗和猫是动物,它们通过分享名字、生命来延长记忆和保护主人猫能爬,但狗不能。狗会思考,但猫不会。通过实施这些功能,这些特定功能被添加到猫和狗中。人不是动物,但他能思考、学习、应用、攀爬
通过这些示例,您可以理解
不相关的类可以通过接口具有功能,但相关的类通过基类的扩展重写行为。
最简单的说法是,extends用于从类继承,implements用于在类中应用接口
延伸:
public class Bicycle {
//properties and methods
}
public class MountainBike extends Bicycle {
//new properties and methods
}
机具:
public interface Relatable {
//stuff you want to put
}
public class RectanglePlus implements Relatable {
//your class code
}
如果您仍然感到困惑,请阅读以下内容:https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.htmlhttps://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html
我注意到你的个人资料中有一些C++问题。如果您理解C++的多重继承(指从多个其他类继承特性的类)的概念,Java不允许这样做,但它确实有关键字接口,这有点像C++中的纯虚拟类。正如很多人所提到的,你扩展了一个类(而且你只能从一个类扩展),你实现了一个接口——但是你的类可以实现任意多的接口。
也就是说,这些关键字及其使用规则描述了Java中多重继承的可能性(只能有一个超级类,但可以实现多个接口)。
推荐文章
- .toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?
- Hibernate中不同的保存方法之间有什么区别?
- Java 8流和数组操作
- Java Regex捕获组
- Openssl不被视为内部或外部命令
- 如何添加自定义方法到Spring Data JPA
- 如何在Ubuntu中设置Java环境路径
- 无法执行dex:在Eclipse中超过GC开销限制
- 有人能解释一下JPA和Hibernate中的mappedBy吗?
- 是什么导致JNI调用变慢?
- Java中的&和&&有什么区别?
- 使用Java的Collections.singletonList()?
- Maven使用多个src目录编译
- 导入时无法解析符号
- 用谓词限制流