What is the main advantage of making a class abstr

2019-01-28 10:41发布

问题:

Why do we declare a class as abstract? I know it cannot be instantiated, but why give a special keyword for it. Even a 'normal' class will work just as well and can be easily subclassed. so what is the main advantage of making a class abstract?

回答1:

In abstract class you can implement some method and can make some abstract all your client will have to implement it also. you can provide some common functionality , also you can have some inherited fields and some of the skeleton method here



回答2:

I think you misunderstand the point of abstract classes: they provide a partial implementation of some functionality, but not a complete implementation.

You suggested that abstract classes were redundant because you can define incomplete methods using public void methodname(){} -- which is certainly ok. However, let's say your clients inherit from a class defined in such a way, how do they know which methods to override? What happens if they forget to override a method? Now their derived class has an incomplete definition -- you don't want that.

The abstract keyword forces clients to provide implementations for certain methods, otherwise the code won't even compile. In other words, it provides a compile-time guarantee that classes you use or create are fully implemented.



回答3:

Declaring the class abstract prevents any code from instantiating the class.

This enforces the design guideline to make non-leaf classes abstract.

It allows you to add abstract methods to your superclass (and implementations to the subclasses) later, without affecting any existing clients.

The abstract keyword works even if the non-leaf class does not currently have any abstract methods.



回答4:

An abstract class can have abstract methods and "concrete" methods.

The "concrete" methods can use the abstract methods, and can be sure that they are (correct) impelmented at runtime. Because every (not abstract) subclass has to implement them. (And ther will be no instance of the abstract class itselfe).

So it is all about savety! - It makes sure that the programmer who want to subclass an abstract class must implement the abstract method(s).

If you do this only with a normal class then the class, corresponding to the abstract class, would have the (abstract) methods with an empty Implementation, and only a notic to the programmer that he has to override this method.

Of course you can use the concept of abstract classes for other thinks, like create not instanciable classes, but that is not the main point.



回答5:

Just a real life example. I have a GUI abstract class that is the parent for all my GUI components. Lets call this AbstractSuperClass. Each of my components that extend AbstractSuperClass need their own implementation of the save function. So the nice thing about making my super class abstract is that I can have an array of type AbstractSuperClass that can hold all of my GUI components. I can then loop over that array and call the save function knowing that each GUI component has its own save method. Since the class is abstract, it forces my subclasses to provide a implementation of the save function.

This is especially useful because when we open up our APIto other programmers, they dont get the source. They just extend the AbstractSuperClass and must provide a save implementation.



回答6:

It's useful if you want to have a group of classes that inherit the same logical functions. But in the same time, the class only implements the basic logic, and doesn't contain any real functionality.

You should see it as a skeleton class.

For example, I once made a class with these specifications :

  1. Controls the process of executing a command on another thread, and relays events of that process.

  2. The class itself didn't have any functionality by itself (didn't implement the actual work() function)

So the result is an abstract class, that can be inherited, that already has a built in thread control, which all you need to do is just implement the work() method.



回答7:

If your class has some default behavior and you want some other behavior to be implemented by the extending classes then you use abstract classes. They cannot be initialized, you can think of abstract classes as a template for the extending classes.

Abstract classes can also call the abstract methods which in result calls extending object's method. Anyways there are lot's of discussions about when to use abstract classes, when to prefer it over an interface. Make a google search, it is an epic discussion :) interfaces vs abstract classes.



回答8:

You would declare a class as abstract if it makes little to no sense to create an instance of it (you would create instances of subclasses).

public abstract class Shape {
    public double calculateArea();
}

public class Square : Shape {
    private double side;

    double calculateArea() {
        return side*side;
    }
}

public class Circle: Shape {
    private double radius;

    double calculateArea() {
        return 3.1415 * radius * radius;
    }
}

public class MainClass() {
    public static void Main() {
        Shape myShape = new Square();
        system.out.print(myShape.calculateArea());
        myShape = new Circle();
    }
}

It makes no sense to create an instance of Shape because it doesn't mean anything concrete, its an abstract concept. However, you can have variable of type Shape which allows you to program around the common base-type (though it could be argued that an interface might be better in this situation).



回答9:

Generally, if there is inheritance, as in a super domain class, with common methods and common implementations in subclasses then look into going with an abstract class, which is not that often, but I do use it.

If you just go with Abstract classes just because there is inheritance, you can run into problems if the code changes a lot. This is detailed very well in the example here: Interfaces vs Abstract Classes in Java, for the different types of domain objects of motors. One of them required a dual powered motor, instead of a specific single type, like asolar powered or battery powered motor. This required multiple subclass implementation methods from both motor types to be used in a single subclass and that is where abstract classes can get messy.

To sum it all up, as a rule you want to define behaviors (what the objects will do) with interfaces and not in Abstract classes. In my thinking, the main advantage for Abstract classes is when the focus from the use case is on an implementation hierarchy and code reuse from subclasses.