Why not override instead of using abstract class?

前端 未结 6 1918
北恋
北恋 2021-01-13 19:11

This might be a simple question for many but has confused me. I am picking an example from Kathy Sierra that shows the utility of Abstract Classes but I am unable to underst

相关标签:
6条回答
  • 2021-01-13 19:37

    Why not have these two methods in any one of these car subtypes, say BMW and then other two - Volkswagen and Audi - can simply override these methods?

    Wouldn't that require Volkswagen and Audi to inherit from BMW? That wouldn't be correct, since those aren't BMWs.

    The abstract base class and its abstract methods form a kind of contract which inheriting objects must implement. There's no such thing as a concrete Car that isn't a more specific type. But all concrete types share commonalities. The abstract Car enforces those commonalities, allowing specific types to be treated as generic instances of Car when needed.

    For example, if you have code which needs a Car on which it will invoke the Drive() method, that code doesn't care what kind of Car it gets. It will Drive() any Car. So it accepts an abstract type so that anything which implements that type can be used:

    UseACar(Car car)
    {
        car.Drive();
    }
    
    // elsewhere
    
    BMW myCar = new BMW();
    UseACar(myCar);
    

    Without this enforcement of the contract, a statically-typed and compiled language would have no way of guaranteeing that .Drive() exists on the object. You'd need to create a UseAX() method for every possible kind of car, each accepting a different type. Using polymorphism like this allows you to create a single, more generic method which works on any instance of the abstract type.

    0 讨论(0)
  • 2021-01-13 19:38

    By making a method abstract, it means that people have to implement it. You require people to do so and it is impossible for people to forget to do so, as it will fail to compile if they do.

    The @override annotation exists for a very similar reason, by marking a method as @override you get an error if (for example) you typed the method name wrong and aren't actually overriding something.

    In many ways the abstract class is half way between an interface and a normal class - it defines what you need to do to use it in the same way an interface does, but it also handles some of the implementation for you.

    Classes can only extend one other class. They can implement any number of interfaces.

    For example you might have MotorVehicle inherited by Car, Motorbike and Train - but then you might have a Steerable interface implemented by Car, Motorbike and Pedalbike.

    To answer the question in the comments:

    If there is an Interface "I" having method m() which is implemented by class "A" and another class "B" wants to access the method m(), what is the need of interface here. Can we simply not implement that method in class A?

    You can - but if on the other hand class B wants to access the method m() in both a class A and class C (where A and C don't inherit from each other or a common class containing m()) then the way to do that is to specify a common interface I and class B uses the interface type, I, not the types A and C at all.

    Also remember that interfaces can be used between packages and libraries. For example Listener and Strategy patterns make heavy use of interfaces. When the Java developers wrote JButton (for example) the ActionLstener is specified as an Interface to provide maximum flexibility to people using JButtons in the future.

    0 讨论(0)
  • 2021-01-13 19:38

    abstraction is great when the original programmer/architect want's to allow customized behavior to some base class and ensure that the consuming programmer implements the required methods.

    0 讨论(0)
  • 2021-01-13 19:46

    This is called abstraction. Methods in abstract class are considered as protocol so that car makers should not violate.

    0 讨论(0)
  • 2021-01-13 19:48

    So that you can write code that deals with Cars without knowing what kind of car it is:

    public void PrintTopSpeed(Car car)
    {
        System.out.println("This car's top speed is " + car.topSpeed());
    }
    

    If the Car class didn't define topSpeed(), this code wouldn't compile. You'd have to have a different version of this print function for each of your BMW, Volkswagen, Audi, etc. derived classes. This is perhaps the most basic concept in object-oriented programming, so you really need to master it. Base classes allow objects to share common behavior, and allow code to be written to use that behavior without any knowledge of what specific type of object it's dealing with.

    0 讨论(0)
  • 2021-01-13 19:53

    Polymorphism is the answer. If you don't have methods power() and topSpeed() in the abstract class you can't do things like this :

    List<Car> cars;
    cars.add(new Porshe());
    cars.add(new Ford());
    
    for(Car car : cars){
        System.out.println(car.topSpeed());
    }
    

    You would have to handle lot of thing your self if you have only custom methods in your subclasses.

    0 讨论(0)
提交回复
热议问题