How is abstract class different from concrete clas

2019-01-30 12:08发布

I understand WHY we need Abstract Class in Java - to create sub-classes. But the same can be achieved by concrete class. e.g. Class Child extends Parent. Here Parent can very well be abstract & concrete. So why do we have ABSTRACT??

6条回答
甜甜的少女心
2楼-- · 2019-01-30 12:38

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

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-30 12:42

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.

查看更多
SAY GOODBYE
4楼-- · 2019-01-30 12:43

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:

Shape hierarchy UML Diagram

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

查看更多
等我变得足够好
5楼-- · 2019-01-30 12:54

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.

查看更多
时光不老,我们不散
6楼-- · 2019-01-30 12:56

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.

查看更多
Juvenile、少年°
7楼-- · 2019-01-30 12:58

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.

查看更多
登录 后发表回答