简体   繁体   中英

Can a subclass force a call to a parent class method to be a compiler error?

I'm trying to make my own Exception which requires a String to be given at construction so that string can be passed along to the user at catch time. Currently, in my code I'm throwing an IllegalArgumentException for the zero argument constructor for my class. What I'd really like is to be able to do is make it a compiler error to call the zero argument constructor for my subclass, as opposed to calling the zero argument constructor for Exception for example.

Is that possible?

Yes, don't provide a default constructor, then you can't call it. (Assuming you have another constructor)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM