简体   繁体   English

抽象类与具体类有何不同?

[英]How is abstract class different from concrete class?

I understand WHY we need Abstract Class in Java - to create sub-classes.我理解为什么我们需要 Java 中的抽象类 - 创建子类。 But the same can be achieved by concrete class.但同样可以通过具体类来实现。 eg Class Child extends Parent.例如 Class Child 扩展了 Parent。 Here Parent can very well be abstract & concrete.在这里 Parent 很可能是抽象的和具体的。 So why do we have ABSTRACT??那么为什么我们有抽象?

Abstract classes cannot be instantiated directly.抽象类不能直接实例化。 Declaring a class as abstract means that you do not want it to be instantiated and that the class can only be inherited.将类声明为抽象类意味着您不希望它被实例化并且该类只能被继承。 You are imposing a rule in your code.您正在代码中强加规则。

If you extend your Parent/Child relationship example further to include a Person class then it would make good sense for Person to be abstract.如果你进一步扩展你的父/子关系示例以包含一个 Person 类,那么 Person 是抽象的会很有意义。 Parent is a concrete idea and so is child.父母是一个具体的想法,孩子也是。 Person is an abstract concept in reality as well as in code.人在现实和代码中都是一个抽象的概念。

One benefit is that you explicitly define and protect the idea of the abstract class.一个好处是您明确定义和保护抽象类的想法。 When you declare a class as an abstract there's no way that you or anyone else using your code uses it incorrectly by instantiating it.当您将类声明为抽象类时,您或使用您的代码的任何其他人都无法通过实例化它来错误地使用它。 This reasoning is similar to why we specify functions and fields as public, private or protected.这个推理类似于我们为什么将函数和字段指定为 public、private 或 protected。 If you declare a function or member as private you are in effect protecting it from improper access from client code.如果您将函数或成员声明为私有,您实际上是在保护它免受来自客户端代码的不当访问。 Privates are meant to be used within the class and that's it. Privates 是为了在课堂上使用,就是这样。 Abstract classes are meant to be inherited and that's that.抽象类是用来继承的,仅此而已。

Now, do you have to use abstract classes and define functions and fields as private instead of public?现在,您是否必须使用抽象类并将函数和字段定义为私有而不是公共? No, you don't .不,你没有 But these concepts are provided to help keep code clean and well-organized.但是提供这些概念是为了帮助保持代码整洁和组织良好。 The abstract class is implemented in all object-oriented languages to my knowledge.据我所知,抽象类以所有面向对象的语言实现。 If you look around you will see that C++, C#, VB.NET etc. all use this concept.如果你环顾四周,你会发现 C++、C#、VB.NET 等都在使用这个概念。

A better, specific example:一个更好的,具体的例子:

形状层次 UML 图

In the example above the Shape class should be abstract because it is not useful on its own.在上面的例子中,Shape 类应该是抽象的,因为它本身没有用。

Abstract class means it is abstract not complete.抽象类意味着它是抽象的不完整的。 It needs another class to complete it and/or its functionalities.它需要另一个类来完成它和/或其功能。 You need to extend the abstract class.您需要扩展抽象类。 It will be useful with Certain class eg.它对某些类很有用,例如。 Fruit all fruits have the same property like color.水果 所有水果都具有相同的属性,如颜色。 But you can have different properties for different fruits like is it pulpy such as orange or not eg Banana etc.但是对于不同的水果,你可以有不同的特性,比如它是多肉的,比如橙子还是不是,比如香蕉等。

I know this is an old question but it looks like the poster still had some questions about the benefit of using an abstract class.我知道这是一个老问题,但看起来海报仍然有一些关于使用抽象类的好处的问题。

If you're the only one who will ever use your code then there really is no benefit.如果您是唯一会使用您的代码的人,那么真的没有任何好处。 However, if you're writing code for others to use there is a benefit.但是,如果您正在编写供他人使用的代码,则有一个好处。 Let's say for example you've written a caching framework but want to allow clients to create their own caching implementation classes.例如,假设您编写了一个缓存框架,但希望允许客户端创建自己的缓存实现类。 You also want to keep track of some metrics, like how many caches are open, hypothetically.您还希望跟踪一些指标,例如假设打开了多少缓存。 Your abstract class might look something like this:您的抽象类可能如下所示:

public abstract class AbstractCache {
    public final void open() {
        ... // Do something here to log your metrics
        openImpl();
    }

    protected abstract void openImpl() { }
}

On its own the AbstractCache class is useless and you don't want clients to try to instantiate one and use it as a cache, which they would be able to do if the class was concrete. AbstractCache 类本身是无用的,您不希望客户端尝试实例化一个并将其用作缓存,如果该类是具体的,他们将能够这样做。 You also want to make sure they can't bypass your metric logging, which they would be able to do if you just provided them a Cache interface.您还希望确保他们无法绕过您的指标日志记录,如果您只是为他们提供了一个 Cache 接口,他们就可以做到这一点。

The point of abstraction is not to create sub-classes.抽象的重点不是创建子类。 It's more about creating Seams in your code.更多的是在你的代码中创建接缝。 You want code to be test-able and decoupled which lead to the ultimate goal of maintainability.您希望代码可测试和解耦,从而实现可维护性的最终目标。 For similar reasons, abstraction also buys us the ability to replace a bit of code without rippling side effects.出于类似的原因,抽象也让我们能够在不产生副作用的情况下替换一些代码。

An abstract class is meant to be used as the base class from which other classes are derived.抽象类旨在用作派生其他类的基类。 The derived class is expected to provide implementations for the methods that are not implemented in the base class.派生类应为基类中未实现的方法提供实现。 A derived class that implements all the missing functionality is called a concrete class实现所有缺失功能的派生类称为具体类

According to my understanding按照我的理解

Abstract Class is a class which just describes the behavior but doesn't implement it.抽象类是一个只描述行为但不实现它的类。 Consider this Java example for Abstract Class:考虑抽象类的这个 Java 示例:

public interface DoSomething(){
public void turnOnTheLight(); 
}

Concrete Classes are those, which are to be implemented.具体类是那些要实现的类。 For Example:例如:

    public abstract class A(){
        public void doIt(); 
    } 
    public class B extends A(){
        public void doIt(){ 
        //concrete method
        System.out.println(“I am a Concrete Class Test”); 
    }
 }

In other words, A concrete class in java is any such class which has implementation of all of its inherited members either from interface or abstract class.换句话说,java 中的具体类是任何这样的类,它具有从接口或抽象类继承的所有成员的实现。

For those who seek only differences in pure technical approach, the clearest difference between concrete parent classes and abstract parent classes is the obligation for children to include/implement specific methods.对于那些只寻求纯技术方法差异的人来说,具体父类和抽象父类之间最明显的区别是子类有义务包含/实现特定方法。

A concrete parent class cannot force/oblige its children to include/implement a method.具体的父类不能强制/强制其子类包含/实现一个方法。 An abstract parent class oblige its children to do that by declaring abstract methods.抽象父类通过声明抽象方法强制其子类这样做。

Apart from the above, it comes to design and functional requirements to dictate the use of abstract class.除此之外,设计和功能要求决定了抽象类的使用。 Such examples can be found on javax.servlet.http.HttpServlet class这样的例子可以在javax.servlet.http.HttpServlet类中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM