开发者

super class constructors and class hierarchy question

开发者 https://www.devze.com 2023-02-19 19:19 出处:网络
So I\'m working on a small project that involved creating a camera class with a constructor, getters/setters, etc. After doing so, it says to create four subclasses, and it hint开发者_开发问答s at mak

So I'm working on a small project that involved creating a camera class with a constructor, getters/setters, etc. After doing so, it says to create four subclasses, and it hint开发者_开发问答s at making some of them abstract. I plan to make a DigitalCamera class that is abstract, and a concrete DSLR class that extends the DigitalCamera class. How could I set up the constructor in the DSLR class, to use the constructor from the main Camera class, and then set fields from the abstract DigitalCamera class? I've tried using the generate constructor option in eclipse, but it doesn't see the constructor from the main class, so I assume it can't be done that way.

Any help would be very much appreciated, thanks!


abstract class DigitalCamera {
    private final Param param;
    protected DigitialCamera(Param param) {
        this.param = param;
    }
}
final class DSLR extends DigitalCamera {
    public DigitialCamera(Param param) {
        super(param); // <-- Call super's constructor.
    }
}

Note, constructors "are not members" and are therefore not inherited. Available constructors of a derived class need not resemble those of the base class.

0

精彩评论

暂无评论...
验证码 换一张
取 消