课程: Java 8+ Essential Training: Objects and APIs

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Use abstract classes and methods

Use abstract classes and methods

- [Instructor] When designing an object-oriented contract based programming architecture, you can frequently choose between using interfaces and abstract classes. You mark an abstract class with the abstract keyword in the class declaration. When it's abstract, it can't be instantiated directly. You have to use one of its subclasses. So, in this project I'll go to clothing item dot java. And I'm going to turn clothing item into an abstract class. I'll just add the keyword abstract right here. I need to keep the class keyword as well. Now, that class can't be instantiated directly. And if I go to the main class, I see an error condition now, saying that clothing item is abstract, cannot be instantiated. I'm going to just comment out that bit of code. I'm not going to need it anymore. But notice, I can still instantiate the subclass, shirt, using the shirt classes constructor and its type. And I can still create an object that's typed as the superclass as long as I use one of the…

内容