Java - Abstract class to contain variables?

前端 未结 3 1548
Happy的楠姐
Happy的楠姐 2021-01-03 21:13

Is it good practice to let abstract classes define instance variables?

public abstract class ExternalScript extends Script {

    String source;

    public          


        
3条回答
  •  囚心锁ツ
    2021-01-03 21:25

    Of course. The whole idea of abstract classes is that they can contain some behaviour or data which you require all sub-classes to contain. Think of the simple example of WheeledVehicle - it should have a numWheels member variable. You want all sub classes to have this variable. Remember that abstract classes are a very useful feature when developing APIs, as they can ensure that people who extend your API won't break it.

提交回复
热议问题