Please explain in an easy to understand language or a link to some article.
请用简单易懂的语言或文章链接进行解释。
14 个解决方案
#1
571
extends
is for extending a class.
扩展是用来扩展类的。
implements
is for implementing an interface
实现是用来实现接口的
The difference between an interface and a regular class is that in an interface you can not implement any of the declared methods. Only the class that "implements" the interface can implement the methods. The C++ equivalent of an interface would be an abstract class (not EXACTLY the same but pretty much).
接口和常规类之间的区别在于,在接口中,您不能实现任何已声明的方法。只有“实现”接口的类才能实现这些方法。与c++等价的接口是一个抽象类(不是完全相同,但差不多)。
Also java doesn't support multiple inheritance for classes. This is solved by using multiple interfaces.
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
}
}
now extending a class
现在扩展一个类
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;
}
}
in this case
在这种情况下
Subclass s = new SubClass();
s.getNb(); //returns 1
s.getNb2(); //returns 3
SuperClass sup = new SuperClass();
sup.getNb(); //returns 1
sup.getNb2(); //returns 2
I suggest you do some more research on dynamic binding, polymorphism and in general inheritance in Object-oriented programming
我建议您对面向对象编程中的动态绑定、多态性和一般继承进行更多的研究
#2
72
I notice you have some C++ questions in your profile. If you understand the concept of multiple-inheritance from C++ (referring to classes that inherit characteristics from more than one other class), Java does not allow this, but it does have keyword interface
, which is sort of like a pure virtual class in C++. As mentioned by lots of people, you extend
a class (and you can only extend from one), and you implement
an interface -- but your class can implement as many interfaces as you like.
我注意到你的个人资料中有一些c++问题。如果您理解c++的多继承概念(指从多个类继承特征的类),Java不允许这样做,但是它有关键字接口,这有点像c++中的纯虚拟类。正如许多人提到的,您扩展了一个类(并且只能从一个类扩展),并且实现了一个接口——但是您的类可以实现任意多的接口。
Ie, these keywords and the rules governing their use delineate the possibilities for multiple-inheritance in Java (you can only have one super class, but you can implement multiple interfaces).
例如,这些关键字和使用它们的规则描述了在Java中多继承的可能性(您只能有一个超类,但是可以实现多个接口)。
#3
33
extends
is for when you're inheriting from a base class (i.e. extending its functionality).
扩展是当您从基类继承时(即扩展它的功能)。
implements
is for when you're implementing an interface.
实现用于实现接口时。
Here is a good place to start: Interfaces and Inheritance.
这里有一个很好的起点:接口和继承。
#4
29
A class
can only "implement" an interface
. A class only "extends" a class
. Likewise, an interface
can extend another interface
.
类只能“实现”接口。类只“扩展”类。同样,一个接口可以扩展另一个接口。
A class
can only extend one other class
. A class
can implement several interface
s.
一个类只能扩展另一个类。一个类可以实现几个接口。
If instead you are more interested in knowing when to use abstract class
es and interface
s, refer to this thread: Interface vs Abstract Class (general OO)
如果您对何时使用抽象类和接口更感兴趣,请参考这个线程:Interface vs抽象类(通用OO)
#5
26
Generally implements used for implementing an interface and extends used for extension of base class behaviour or abstract class.
通常实现用于实现接口和扩展基类行为或抽象类的扩展。
extends: A derived class can extend a base class. You may redefine the behaviour of an established relation. Derived class "is a" base class type
扩展:派生类可以扩展基类。你可以重新定义已建立关系的行为。派生类是“基类类型”
implements: You are implementing a contract. The class implementing the interface "has a" capability.
工具:你正在执行一个契约。实现接口的类“有”能力。
With java 8 release, interface can have default methods in interface, which provides implementation in interface itself.
使用java 8发行版,接口可以在接口中有默认方法,接口本身提供实现。
Refer to this question for when to use each of them:
请参考以下问题:
Interface vs Abstract Class (general OO)
接口vs抽象类(通用OO)
Example to understand things.
例子来理解事物。
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;
}
}
output:
输出:
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
Important points to understand:
理解要点:
-
Dog and Cat are animals and they extended
remember
() andprotectOwner
() by sharingname,lifeExpentency
fromAnimal
- 狗和猫是一种动物,它们通过共同的名字,从动物身上获得生命力,从而扩展了记忆和保护意识
-
Cat can climb() but Dog does not. Dog can think() but Cat does not. These specific capabilities are added to
Cat
andDog
by implementing that capability. - 猫会爬,狗不会爬。狗会思考,猫不会。通过实现该功能,将这些特定的功能添加到Cat和Dog中。
- Man is not an animal but he can
Think,Learn,Apply,Climb
- 人不是动物,但他能思考、学习、应用、攀登
By going through these examples, you can understand that
通过这些例子,你可以理解这一点
Unrelated classes can have capabilities through interface but related classes override behaviour through extension of base classes.
不相关的类可以通过接口具有功能,但是相关的类通过基类的扩展覆盖行为。
#6
18
An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X". Again, as an example, anything that "ACTS LIKE" a light, should have a turn_on() method and a turn_off() method. The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X,Y,Z, etc.
接口是一个对象可以做的动作的描述。举个例子,当你打开电灯开关时,灯就会亮起来,你不关心它是怎么亮的。在面向对象编程中,接口是一个对象为了成为“X”而必须具有的所有函数的描述。同样,作为一个示例,任何“像”灯的操作都应该有一个turn_on()方法和一个turn_off()方法。接口的目的是让计算机执行这些属性,并知道类型为T的对象(无论接口是什么)必须具有称为X、Y、Z等的函数。
An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class). For example, say we have a car class and a scooter class and a truck class. Each of these three classes should have a start_engine() action. How the "engine is started" for each vehicle is left to each particular class, but the fact that they must have a start_engine action is the domain of the interface.
接口是一种编程结构/语法,允许计算机在对象(类)上强制执行某些属性。比如说,我们有一个汽车班,一个摩托车班和一个卡车班。这三个类中的每一个都应该有start_engine()操作。每个车辆的“引擎是如何启动的”取决于每个特定的类,但是必须有start_engine操作的事实是接口的领域。
#7
11
-
A extends B:
一个扩展B:
A and B are both classes or both interfaces
A和B都是类或都是接口
-
A implements B
实现了B
A is a class and B is an interface
A是类,B是接口
-
The remaining case where A is an interface and B is a class is not legal in Java.
在剩下的例子中,A是接口,B是类,在Java中是不合法的。
#8
11
Extends : This is used to get attributes of a parent class into base class and may contain already defined methods that can be overridden in the child class.
extend:用于将父类的属性获取到基类中,并可能包含已定义的方法,这些方法可以在子类中重写。
Implements : This is used to implement an interface (parent class with functions signatures only but not their definitions) by defining it in the child class.
实现:通过在子类中定义接口来实现接口(父类只具有函数签名,而不具有它们的定义)。
There is one special condition: "What if I want a new Interface to be the child of an existing interface?". In the above condition, the child interface extends the parent interface.
有一个特殊的条件:“如果我想要一个新接口成为现有接口的子接口,该怎么办?”在上述条件下,子接口扩展父接口。
#9
9
Implements is used for Interfaces and extends is used to extend a class.
实现用于接口,扩展用于扩展类。
To make it more clearer in easier terms,an interface is like it sound - an interface - a model, that you need to apply,follow, along with your ideas to it.
为了使它更清晰更简单,接口就像它听起来——一个接口——一个模型,你需要应用它,遵循它,以及你对它的想法。
Extend is used for classes,here,you are extending something that already exists by adding more functionality to it.
扩展用于类,在这里,通过添加更多的功能来扩展已经存在的东西。
A few more notes:
一些说明:
an interface can extend another interface.
接口可以扩展另一个接口。
And when you need to choose between implementing an interface or extending a class for a particular scenario, go for implementing an interface. Because a class can implement multiple interfaces but extend only one class.
当您需要为特定场景选择实现接口还是扩展类时,请使用实现接口。因为一个类可以实现多个接口,但只能扩展一个类。
#10
6
When a subclass extends a class, it allows the subclass to inherit (reuse) and override code defined in the supertype. When a class implements an interface, it allows an object created from the class to be used in any context that expects a value of the interface.
当子类扩展一个类时,它允许子类继承(重用)和重写超类型中定义的代码。当一个类实现一个接口时,它允许在任何期望接口值的上下文中使用从该类创建的对象。
The real catch here is that while we are implementing anything it simply means we are using those methods as it is. There is no scope for change in their values and return types.
这里真正的问题是,当我们实现任何东西时,它仅仅意味着我们正在使用那些方法。它们的值和返回类型没有更改的余地。
But when we are extending anything then it becomes an extension of your class. You can change it, use it, reuse use it and it does not necessarily need to return the same values as it does in superclass.
但是当我们扩展任何东西时它就变成了类的扩展。您可以更改它,使用它,重用它,它并不一定需要返回与在超类中相同的值。
#11
5
Both keywords are used when creating your own new class in the Java language.
在使用Java语言创建自己的新类时,将使用这两个关键字。
Difference: implements
means you are using the elements of a Java Interface in your class. extends
means that you are creating a subclass of the base class you are extending. You can only extend one class in your child class, but you can implement as many interfaces as you would like.
差异:实现意味着在类中使用Java接口的元素。扩展意味着您正在创建正在扩展的基类的子类。您只能在子类中扩展一个类,但是您可以实现任意多的接口。
Refer to oracle documentation page on interface for more details.
有关更多细节,请参阅接口上的oracle文档页面。
This can help to clarify what an interface is, and the conventions around using them.
这有助于澄清接口是什么,以及使用它们的约定。
#12
4
In the most simple terms extends is used to inherit from a class and implements is used to apply an interface in your class
在最简单的术语中,extend用于从类继承,实现用于在类中应用接口
extends:
扩展:
public class Bicycle {
//properties and methods
}
public class MountainBike extends Bicycle {
//new properties and methods
}
implements:
实现:
public interface Relatable {
//stuff you want to put
}
public class RectanglePlus implements Relatable {
//your class code
}
if you still have confusion read this: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html https://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html
如果您仍然有疑惑,请阅读:https://docs.oracle.com/javase/tutorial/java/iandi/subclass.html https://docs.oracle.com/javase/tutorial/iandi/usinginterface.html
#13
3
We use SubClass extends SuperClass only when the subclass wants to use some functionality (methods or instance variables) that is already declared in the SuperClass, or if I want to slightly modify the functionality of the SuperClass (Method overriding). But say, for example I have an Animal class(SuperClass) and a Dog class (SubClass) and there are few methods that I have defined in the Animal class eg. doEat(); , doSleep(); ... and many more.
我们只在子类想要使用超类中已经声明的功能(方法或实例变量)或者想稍微修改超类(方法重写)的功能时才使用子类继承超类。但是,比方说,我有一个动物类(超类)和一个狗类(子类),而且我在动物类eg中定义的方法很少。doEat();doSleep();…和许多更多。
Now, my Dog class can simply extend the Animal class, if i want my dog to use any of the methods declared in the Animal class I can invoke those methods by simply creating a Dog object. So this way I can guarantee that I have a dog that can eat and sleep and do whatever else I want the dog to do.
现在,我的Dog类可以简单地扩展Animal类,如果我想让我的Dog使用在Animal类中声明的任何方法,我可以通过创建Dog对象来调用这些方法。这样我就可以保证我有一只狗,它可以吃,可以睡,可以做我想让它做的任何事情。
Now, imagine, one day some Cat lover comes into our workspace and she tries to extend the Animal class(cats also eat and sleep). She makes a Cat object and starts invoking the methods.
现在,想象一下,有一天,一些爱猫的人来到我们的工作区域,她试图延长动物的种类(猫也吃和睡觉)。她创建一个Cat对象并开始调用这些方法。
But, say, someone tries to make an object of the Animal class. You can tell how a cat sleeps, you can tell how a dog eats, you can tell how an elephant drinks. But it does not make any sense in making an object of the Animal class. Because it is a template and we do not want any general way of eating.
但是,比方说,有人试图制作动物类的对象。你可以知道猫是怎么睡的,你可以知道狗是怎么吃的,你可以知道大象是怎么喝的。但是把动物类作为对象是没有意义的。因为它是一个模板,我们不想要任何一般的饮食方式。
So instead, I will prefer to make an abstract class that no one can instantiate but can be used as a template for other classes.
因此,我宁愿创建一个抽象类,没有人可以实例化它,但可以作为其他类的模板。
So to conclude, Interface is nothing but an abstract class(a pure abstract class) which contains no method implementations but only the definitions(the templates). So whoever implements the interface just knows that they have the templates of doEat(); and doSleep(); but they have to define their own doEat(); and doSleep(); methods according to their need.
综上所述,接口只是一个抽象类(一个纯粹的抽象类),它不包含方法实现,只包含定义(模板)。无论谁实现了这个接口,他都知道他们有doEat()的模板;和doSleep();但是他们必须定义自己的doEat();和doSleep();方法根据需要。
You extend only when you want to reuse some part of the SuperClass(but keep in mind, you can always override the methods of your SuperClass according to your need) and you implement when you want the templates and you want to define them on your own(according to your need).
只有当您希望重用超类的某些部分时才进行扩展(但是请记住,您总是可以根据需要重写超类的方法),并在需要模板并希望自己定义它们时(根据您的需要)实现它们。
I will share with you a piece of code: You try it with different sets of inputs and look at the results.
我将与您分享一段代码:您尝试使用不同的输入集并查看结果。
class AnimalClass {
public void doEat() {
System.out.println("Animal Eating...");
}
public void sleep() {
System.out.println("Animal Sleeping...");
}
}
public class Dog extends AnimalClass implements AnimalInterface, Herbi{
public static void main(String[] args) {
AnimalInterface a = new Dog();
Dog obj = new Dog();
obj.doEat();
a.eating();
obj.eating();
obj.herbiEating();
}
public void doEat() {
System.out.println("Dog eating...");
}
@Override
public void eating() {
System.out.println("Eating through an interface...");
// TODO Auto-generated method stub
}
@Override
public void herbiEating() {
System.out.println("Herbi eating through an interface...");
// TODO Auto-generated method stub
}
}
Defined Interfaces :
接口定义:
public interface AnimalInterface {
public void eating();
}
interface Herbi {
public void herbiEating();
}
#14
2
As shown in the figure given below, a class extends another class, an interface extends another interface but a class implements an interface.
如图所示,一个类扩展了另一个类,一个接口扩展了另一个接口,而一个类实现了一个接口。
For more details
为更多的细节
#1
571
extends
is for extending a class.
扩展是用来扩展类的。
implements
is for implementing an interface
实现是用来实现接口的
The difference between an interface and a regular class is that in an interface you can not implement any of the declared methods. Only the class that "implements" the interface can implement the methods. The C++ equivalent of an interface would be an abstract class (not EXACTLY the same but pretty much).
接口和常规类之间的区别在于,在接口中,您不能实现任何已声明的方法。只有“实现”接口的类才能实现这些方法。与c++等价的接口是一个抽象类(不是完全相同,但差不多)。
Also java doesn't support multiple inheritance for classes. This is solved by using multiple interfaces.
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
}
}
now extending a class
现在扩展一个类
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;
}
}
in this case
在这种情况下
Subclass s = new SubClass();
s.getNb(); //returns 1
s.getNb2(); //returns 3
SuperClass sup = new SuperClass();
sup.getNb(); //returns 1
sup.getNb2(); //returns 2
I suggest you do some more research on dynamic binding, polymorphism and in general inheritance in Object-oriented programming
我建议您对面向对象编程中的动态绑定、多态性和一般继承进行更多的研究
#2
72
I notice you have some C++ questions in your profile. If you understand the concept of multiple-inheritance from C++ (referring to classes that inherit characteristics from more than one other class), Java does not allow this, but it does have keyword interface
, which is sort of like a pure virtual class in C++. As mentioned by lots of people, you extend
a class (and you can only extend from one), and you implement
an interface -- but your class can implement as many interfaces as you like.
我注意到你的个人资料中有一些c++问题。如果您理解c++的多继承概念(指从多个类继承特征的类),Java不允许这样做,但是它有关键字接口,这有点像c++中的纯虚拟类。正如许多人提到的,您扩展了一个类(并且只能从一个类扩展),并且实现了一个接口——但是您的类可以实现任意多的接口。
Ie, these keywords and the rules governing their use delineate the possibilities for multiple-inheritance in Java (you can only have one super class, but you can implement multiple interfaces).
例如,这些关键字和使用它们的规则描述了在Java中多继承的可能性(您只能有一个超类,但是可以实现多个接口)。
#3
33
extends
is for when you're inheriting from a base class (i.e. extending its functionality).
扩展是当您从基类继承时(即扩展它的功能)。
implements
is for when you're implementing an interface.
实现用于实现接口时。
Here is a good place to start: Interfaces and Inheritance.
这里有一个很好的起点:接口和继承。
#4
29
A class
can only "implement" an interface
. A class only "extends" a class
. Likewise, an interface
can extend another interface
.
类只能“实现”接口。类只“扩展”类。同样,一个接口可以扩展另一个接口。
A class
can only extend one other class
. A class
can implement several interface
s.
一个类只能扩展另一个类。一个类可以实现几个接口。
If instead you are more interested in knowing when to use abstract class
es and interface
s, refer to this thread: Interface vs Abstract Class (general OO)
如果您对何时使用抽象类和接口更感兴趣,请参考这个线程:Interface vs抽象类(通用OO)
#5
26
Generally implements used for implementing an interface and extends used for extension of base class behaviour or abstract class.
通常实现用于实现接口和扩展基类行为或抽象类的扩展。
extends: A derived class can extend a base class. You may redefine the behaviour of an established relation. Derived class "is a" base class type
扩展:派生类可以扩展基类。你可以重新定义已建立关系的行为。派生类是“基类类型”
implements: You are implementing a contract. The class implementing the interface "has a" capability.
工具:你正在执行一个契约。实现接口的类“有”能力。
With java 8 release, interface can have default methods in interface, which provides implementation in interface itself.
使用java 8发行版,接口可以在接口中有默认方法,接口本身提供实现。
Refer to this question for when to use each of them:
请参考以下问题:
Interface vs Abstract Class (general OO)
接口vs抽象类(通用OO)
Example to understand things.
例子来理解事物。
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;
}
}
output:
输出:
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
Important points to understand:
理解要点:
-
Dog and Cat are animals and they extended
remember
() andprotectOwner
() by sharingname,lifeExpentency
fromAnimal
- 狗和猫是一种动物,它们通过共同的名字,从动物身上获得生命力,从而扩展了记忆和保护意识
-
Cat can climb() but Dog does not. Dog can think() but Cat does not. These specific capabilities are added to
Cat
andDog
by implementing that capability. - 猫会爬,狗不会爬。狗会思考,猫不会。通过实现该功能,将这些特定的功能添加到Cat和Dog中。
- Man is not an animal but he can
Think,Learn,Apply,Climb
- 人不是动物,但他能思考、学习、应用、攀登
By going through these examples, you can understand that
通过这些例子,你可以理解这一点
Unrelated classes can have capabilities through interface but related classes override behaviour through extension of base classes.
不相关的类可以通过接口具有功能,但是相关的类通过基类的扩展覆盖行为。
#6
18
An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X". Again, as an example, anything that "ACTS LIKE" a light, should have a turn_on() method and a turn_off() method. The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X,Y,Z, etc.
接口是一个对象可以做的动作的描述。举个例子,当你打开电灯开关时,灯就会亮起来,你不关心它是怎么亮的。在面向对象编程中,接口是一个对象为了成为“X”而必须具有的所有函数的描述。同样,作为一个示例,任何“像”灯的操作都应该有一个turn_on()方法和一个turn_off()方法。接口的目的是让计算机执行这些属性,并知道类型为T的对象(无论接口是什么)必须具有称为X、Y、Z等的函数。
An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class). For example, say we have a car class and a scooter class and a truck class. Each of these three classes should have a start_engine() action. How the "engine is started" for each vehicle is left to each particular class, but the fact that they must have a start_engine action is the domain of the interface.
接口是一种编程结构/语法,允许计算机在对象(类)上强制执行某些属性。比如说,我们有一个汽车班,一个摩托车班和一个卡车班。这三个类中的每一个都应该有start_engine()操作。每个车辆的“引擎是如何启动的”取决于每个特定的类,但是必须有start_engine操作的事实是接口的领域。
#7
11
-
A extends B:
一个扩展B:
A and B are both classes or both interfaces
A和B都是类或都是接口
-
A implements B
实现了B
A is a class and B is an interface
A是类,B是接口
-
The remaining case where A is an interface and B is a class is not legal in Java.
在剩下的例子中,A是接口,B是类,在Java中是不合法的。
#8
11
Extends : This is used to get attributes of a parent class into base class and may contain already defined methods that can be overridden in the child class.
extend:用于将父类的属性获取到基类中,并可能包含已定义的方法,这些方法可以在子类中重写。
Implements : This is used to implement an interface (parent class with functions signatures only but not their definitions) by defining it in the child class.
实现:通过在子类中定义接口来实现接口(父类只具有函数签名,而不具有它们的定义)。
There is one special condition: "What if I want a new Interface to be the child of an existing interface?". In the above condition, the child interface extends the parent interface.
有一个特殊的条件:“如果我想要一个新接口成为现有接口的子接口,该怎么办?”在上述条件下,子接口扩展父接口。
#9
9
Implements is used for Interfaces and extends is used to extend a class.
实现用于接口,扩展用于扩展类。
To make it more clearer in easier terms,an interface is like it sound - an interface - a model, that you need to apply,follow, along with your ideas to it.
为了使它更清晰更简单,接口就像它听起来——一个接口——一个模型,你需要应用它,遵循它,以及你对它的想法。
Extend is used for classes,here,you are extending something that already exists by adding more functionality to it.
扩展用于类,在这里,通过添加更多的功能来扩展已经存在的东西。
A few more notes:
一些说明:
an interface can extend another interface.
接口可以扩展另一个接口。
And when you need to choose between implementing an interface or extending a class for a particular scenario, go for implementing an interface. Because a class can implement multiple interfaces but extend only one class.
当您需要为特定场景选择实现接口还是扩展类时,请使用实现接口。因为一个类可以实现多个接口,但只能扩展一个类。
#10
6
When a subclass extends a class, it allows the subclass to inherit (reuse) and override code defined in the supertype. When a class implements an interface, it allows an object created from the class to be used in any context that expects a value of the interface.
当子类扩展一个类时,它允许子类继承(重用)和重写超类型中定义的代码。当一个类实现一个接口时,它允许在任何期望接口值的上下文中使用从该类创建的对象。
The real catch here is that while we are implementing anything it simply means we are using those methods as it is. There is no scope for change in their values and return types.
这里真正的问题是,当我们实现任何东西时,它仅仅意味着我们正在使用那些方法。它们的值和返回类型没有更改的余地。
But when we are extending anything then it becomes an extension of your class. You can change it, use it, reuse use it and it does not necessarily need to return the same values as it does in superclass.
但是当我们扩展任何东西时它就变成了类的扩展。您可以更改它,使用它,重用它,它并不一定需要返回与在超类中相同的值。
#11
5
Both keywords are used when creating your own new class in the Java language.
在使用Java语言创建自己的新类时,将使用这两个关键字。
Difference: implements
means you are using the elements of a Java Interface in your class. extends
means that you are creating a subclass of the base class you are extending. You can only extend one class in your child class, but you can implement as many interfaces as you would like.
差异:实现意味着在类中使用Java接口的元素。扩展意味着您正在创建正在扩展的基类的子类。您只能在子类中扩展一个类,但是您可以实现任意多的接口。
Refer to oracle documentation page on interface for more details.
有关更多细节,请参阅接口上的oracle文档页面。
This can help to clarify what an interface is, and the conventions around using them.
这有助于澄清接口是什么,以及使用它们的约定。
#12
4
In the most simple terms extends is used to inherit from a class and implements is used to apply an interface in your class
在最简单的术语中,extend用于从类继承,实现用于在类中应用接口
extends:
扩展:
public class Bicycle {
//properties and methods
}
public class MountainBike extends Bicycle {
//new properties and methods
}
implements:
实现:
public interface Relatable {
//stuff you want to put
}
public class RectanglePlus implements Relatable {
//your class code
}
if you still have confusion read this: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html https://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html
如果您仍然有疑惑,请阅读:https://docs.oracle.com/javase/tutorial/java/iandi/subclass.html https://docs.oracle.com/javase/tutorial/iandi/usinginterface.html
#13
3
We use SubClass extends SuperClass only when the subclass wants to use some functionality (methods or instance variables) that is already declared in the SuperClass, or if I want to slightly modify the functionality of the SuperClass (Method overriding). But say, for example I have an Animal class(SuperClass) and a Dog class (SubClass) and there are few methods that I have defined in the Animal class eg. doEat(); , doSleep(); ... and many more.
我们只在子类想要使用超类中已经声明的功能(方法或实例变量)或者想稍微修改超类(方法重写)的功能时才使用子类继承超类。但是,比方说,我有一个动物类(超类)和一个狗类(子类),而且我在动物类eg中定义的方法很少。doEat();doSleep();…和许多更多。
Now, my Dog class can simply extend the Animal class, if i want my dog to use any of the methods declared in the Animal class I can invoke those methods by simply creating a Dog object. So this way I can guarantee that I have a dog that can eat and sleep and do whatever else I want the dog to do.
现在,我的Dog类可以简单地扩展Animal类,如果我想让我的Dog使用在Animal类中声明的任何方法,我可以通过创建Dog对象来调用这些方法。这样我就可以保证我有一只狗,它可以吃,可以睡,可以做我想让它做的任何事情。
Now, imagine, one day some Cat lover comes into our workspace and she tries to extend the Animal class(cats also eat and sleep). She makes a Cat object and starts invoking the methods.
现在,想象一下,有一天,一些爱猫的人来到我们的工作区域,她试图延长动物的种类(猫也吃和睡觉)。她创建一个Cat对象并开始调用这些方法。
But, say, someone tries to make an object of the Animal class. You can tell how a cat sleeps, you can tell how a dog eats, you can tell how an elephant drinks. But it does not make any sense in making an object of the Animal class. Because it is a template and we do not want any general way of eating.
但是,比方说,有人试图制作动物类的对象。你可以知道猫是怎么睡的,你可以知道狗是怎么吃的,你可以知道大象是怎么喝的。但是把动物类作为对象是没有意义的。因为它是一个模板,我们不想要任何一般的饮食方式。
So instead, I will prefer to make an abstract class that no one can instantiate but can be used as a template for other classes.
因此,我宁愿创建一个抽象类,没有人可以实例化它,但可以作为其他类的模板。
So to conclude, Interface is nothing but an abstract class(a pure abstract class) which contains no method implementations but only the definitions(the templates). So whoever implements the interface just knows that they have the templates of doEat(); and doSleep(); but they have to define their own doEat(); and doSleep(); methods according to their need.
综上所述,接口只是一个抽象类(一个纯粹的抽象类),它不包含方法实现,只包含定义(模板)。无论谁实现了这个接口,他都知道他们有doEat()的模板;和doSleep();但是他们必须定义自己的doEat();和doSleep();方法根据需要。
You extend only when you want to reuse some part of the SuperClass(but keep in mind, you can always override the methods of your SuperClass according to your need) and you implement when you want the templates and you want to define them on your own(according to your need).
只有当您希望重用超类的某些部分时才进行扩展(但是请记住,您总是可以根据需要重写超类的方法),并在需要模板并希望自己定义它们时(根据您的需要)实现它们。
I will share with you a piece of code: You try it with different sets of inputs and look at the results.
我将与您分享一段代码:您尝试使用不同的输入集并查看结果。
class AnimalClass {
public void doEat() {
System.out.println("Animal Eating...");
}
public void sleep() {
System.out.println("Animal Sleeping...");
}
}
public class Dog extends AnimalClass implements AnimalInterface, Herbi{
public static void main(String[] args) {
AnimalInterface a = new Dog();
Dog obj = new Dog();
obj.doEat();
a.eating();
obj.eating();
obj.herbiEating();
}
public void doEat() {
System.out.println("Dog eating...");
}
@Override
public void eating() {
System.out.println("Eating through an interface...");
// TODO Auto-generated method stub
}
@Override
public void herbiEating() {
System.out.println("Herbi eating through an interface...");
// TODO Auto-generated method stub
}
}
Defined Interfaces :
接口定义:
public interface AnimalInterface {
public void eating();
}
interface Herbi {
public void herbiEating();
}
#14
2
As shown in the figure given below, a class extends another class, an interface extends another interface but a class implements an interface.
如图所示,一个类扩展了另一个类,一个接口扩展了另一个接口,而一个类实现了一个接口。
For more details
为更多的细节