Java 我应该实现抽象类中存在的所有方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21537982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I implement all the methods present in an abstract class?
提问by violet kiwi
Below is the code snippet:
下面是代码片段:
public abstract class MyAbstractClass {
public abstract void a();
public abstract void b();
}
public class Foo extends MyAbstractClass {
public void a() {
System.out.println("hello");
}
public void b(){
System.out.println("bye");
}
}
public class Bar extends MyAbstractClass {
public void a() {
System.out.println("hello");
}
public void delta() {
System.out.println("gamma");
}
}
There are couple of questions that I have:
我有几个问题:
Q-1 :- Should I implement ALL the methods in abstract
class?
Q-1 :- 我应该在abstract
课堂上实现所有方法吗?
Q-2 :- Can the implementing class have its own methods?
Q-2 :- 实现类可以有自己的方法吗?
回答by tskuzzy
You not only should, but have toimplement all abstract methods (if the subclass is non-abstract). Otherwise an object of that subclass wouldn't know what to do if that method was called!
您不仅应该,而且必须实现所有抽象方法(如果子类是非抽象的)。否则,如果调用该方法,该子类的对象将不知道该怎么做!
The only way to prevent this is if the subclass is also declared abstract
, so that it cannot be instantiated in the first place.
防止这种情况的唯一方法是如果子类也被声明abstract
,那么它首先不能被实例化。
回答by Girish
Ya definately implementing class can define its own method as well and if are not implementing all the methods of your abstract class in the derived class then mark this derived class also as Abstract
Ya肯定实现类也可以定义自己的方法,如果没有在派生类中实现抽象类的所有方法,则将该派生类也标记为抽象
but at the end of chain you have to make one concrete class which implements all the method that was not implement in abstract sub-parent
但是在链的末尾,你必须创建一个具体的类,它实现了所有没有在抽象子父级中实现的方法
回答by M7Hymans
Yes, the implementing class need only implement the methods labeled as abstract in the abstract class.
是的,实现类只需要实现抽象类中标记为抽象的方法。
回答by Christian
Q-1:- Should I implement all methods in abstract
class?
Q-1:- 我应该在abstract
课堂上实现所有方法吗?
- Yes, you mustimplement all
abstract
methods.
- 是的,您必须实现所有
abstract
方法。
Q-2 :- Can the implementing class have its own methods?
Q-2 :- 实现类可以有自己的方法吗?
- Yes, you can declare own (more specfic) methods.
- 是的,您可以声明自己的(更具体的)方法。
回答by Vyaas
Yes, When you are implementing an Interface you will haveto implement all its methods. That is the purpose of defining an Interface. And yes, you can have your own methods in the class where you implement an Interface.
是的,当你实现一个接口时,你必须实现它的所有方法。这就是定义接口的目的。是的,您可以在实现接口的类中拥有自己的方法。
回答by ?smail Taha AYKA?
You don't have to implement all methods of an abstract class. But you must implement all abstract methods of it.
您不必实现抽象类的所有方法。但是你必须实现它的所有抽象方法。
In fact extending an abstract class has no difference then extending a normal class. It's not like implementing interfaces. Since you're extending you are creating a subclass thus you can add as many methods and attributes as you need.
事实上,扩展抽象类与扩展普通类没有区别。这不像实现接口。由于您正在扩展,因此您正在创建一个子类,因此您可以根据需要添加任意数量的方法和属性。
回答by user1339772
public interface I{
public void m();
}
public abstract class A1 implements I{
//No need to implement m() here - since this is abstract
}
public class B1 extends A1{
public void m(){
//This is required, since A1 did not implement m().
}
}
public abstract class A11 extends A1{
//Again No need to implement m() here - since this is abstract
public abstract void newA11Method()
}
public class B11 extends A11{
//This class needs to implement m() and newA11Method()
}
回答by mdewitt
When you extend an Interface
or an Abstract
class you are creating a contract of sorts with that superclass
. In the contract you are saying:
当您扩展一个Interface
或一个Abstract
类时,您正在创建一个与该类的契约superclass
。在合同中你说:
"I will implement all unimplemented methods in my superclass"
“我将在我的超类中实现所有未实现的方法”
If you do not, implement all the unimplemented methods, then you are breaking your contract. A way to not break your contract is make your subclass Abstract
as well as a way of saying
如果你不这样做,实现所有未实现的方法,那么你就违反了合同。不破坏合同的一种方法是创建您的子类Abstract
以及一种说法
"I have not implemented all the classes in my contract, I am going to have my subclasses implement them".
“我还没有实现合同中的所有类,我将让我的子类实现它们”。
For your class bar
right now, you must implement b()
or make bar
an Abstract
class or you are not fulfilling your contract with MyAbstractClass
为了您的类bar
现在,你必须执行b()
或作出bar
一个Abstract
类或者你没有履行与您的合同MyAbstractClass
The basic idea is:
基本思想是:
Interface:None of my methods are implemented. A subclass must implement all my methods in order to implement me. (Note: I believe default interfaces have been added to Java 8 which may change this a bit)
接口:我的方法都没有实现。子类必须实现我的所有方法才能实现我。(注意:我相信默认接口已添加到 Java 8 中,这可能会有所改变)
Example:
例子:
public interface myInterface
{
//My subclasses must implement this to fulfill their contract with me
public void methodA();
//My subclasses must implement this to fulfill their contract with me
public void methodB();
}
Abstract:I may implement some of my methods, but I will also leave methods as abstract so that my subclasses must implement because they can implement those classes to suit their needs better than I can.
摘要:我可能会实现我的一些方法,但我也会将方法保留为抽象,以便我的子类必须实现,因为它们可以比我更好地实现这些类来满足他们的需求。
Example:
例子:
public abstract class myAbstractClass
{
//My subclasses must implement this to fulfill their contract with me
public abstract void methodC();
public void helloWorld()
{
System.out.println("Hello World");
}
}
Abstract
classes can also extend interfaces so they can implement some of their methods. But they can also leave some of the methods unimplemented so the subclass can implement them. If you leave an interface method unimplemented, there is not need to declare it abstract, it is already in the contract.
Abstract
类还可以扩展接口,这样它们就可以实现它们的一些方法。但是它们也可以保留一些未实现的方法,以便子类可以实现它们。如果未实现接口方法,则无需将其声明为抽象的,它已经在合同中。
Example:
例子:
public abstract class myAbstractClass2 implement myInterface
{
@Override
public void methodA()
{
// this fulfills part of the contract with myInterface.
// my subclasses will not need to implement this unless they want to override
// my implementation.
}
//My subclasses must implement this to fulfill their contract with me
public abstract void methodD();
}
So in essence, an abstract class doesn't have as strict a contract with it's superclass because it can delegate its methods to its subclasses.
所以本质上,抽象类与它的超类没有那么严格的契约,因为它可以将它的方法委托给它的子类。
Regular Class: (I use regular to mean non-interface, and non-abstract). I must implement allunimplemented methods from all of my superclasses. These classes have a binding contract.
常规类:(我使用常规来表示非接口和非抽象)。我必须从我的所有超类中实现所有未实现的方法。这些类有一个有约束力的契约。
Example:
例子:
public class mySubClass extends myAbstractClass2
{
@Override
public void methodB()
{
//must be implemented to fulfill contract with myInterface
}
@Override
public void methodD()
{
//must be implemented to fulfill contract with myAbstractClass2
}
public void myMethod()
{
//This is a method specifically for mySubClass.
}
}
回答by VISHWANATH N P
Yes, when you extends abstract you should give implementation for all abstract methodswhich are presents in abstract class. Othrewise you should make it implementation class as abtract class.
回答by Achintha Isuru
You must implement all the abstract methods you have in the abstract class. But you can directly use the concrete methods that are implemented.
您必须实现抽象类中的所有抽象方法。但是你可以直接使用实现的具体方法。
For more information please refer to the : https://www.javatpoint.com/abstract-class-in-java