简体   繁体   中英

How is abstract class different from concrete class?

I understand WHY we need Abstract Class in Java - to create sub-classes. But the same can be achieved by concrete class. eg Class Child extends Parent. Here Parent can very well be abstract & concrete. 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. 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. 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. 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.

A better, specific example:

形状层次 UML 图

In the example above the Shape class should be abstract because it is not useful on its own.

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

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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