What is the main advantage of making a class abstr

2019-01-28 11:11发布

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?

9条回答
成全新的幸福
2楼-- · 2019-01-28 11:19

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-28 11:20

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.

查看更多
该账号已被封号
4楼-- · 2019-01-28 11:26

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.

查看更多
来,给爷笑一个
5楼-- · 2019-01-28 11:27

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

查看更多
在下西门庆
6楼-- · 2019-01-28 11:28

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.

查看更多
Deceive 欺骗
7楼-- · 2019-01-28 11:29

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

查看更多
登录 后发表回答