Kaydell Leavitt wrote:
I read that it isn't good to use nextInt() anyway because it doesn't validate. I believe that you can get stuck on nextInt() when it's in a loop and the next character is not a digit and it never will be.
Kaydell Leavitt wrote:
Would you say that you should always use nextLine() or next() and always input String objects and then do validation in your own Java code?
Maneesh Godbole wrote:I don't use IntelliJ but sounds like you have an IDE refresh issue. Maybe you can try refreshing the file/workspace?
Jeff Verdegan wrote:. Is there some reasonable common/default behavior that could be provided for at least some implementations? Then use abstract class. Are we defining a pure abstract type? Then use interface. And in many cases we use both.
Campbell Ritchie wrote:Thank you for quoting the book.
I don’t know whether our FAQ would help you. I did not think that passage from the book is very clear; it might be clearer in the context of the whole paragraph, however.
I don’t know whether this sort of example helps, but let’s try:-
Q. When to use an abstract class?:
In case where you want to use implementation inheritancethen it is
usually provided by an abstract base class. Abstract classes are excellent candidates inside of application
frameworks. Abstract classes let you define some default behavior and force subclasses to provide any specific
behavior. Care should be taken not to overuse implementation inheritance as discussed in Q10in Java section.
Q. When to use an interface?:
For polymorphic interface inheritance, where the client wants to only deal with a
type and does not care about the actual implementation use interfaces. If you need to change your design
frequently, you should prefer using interface to abstract. COCoding to an interfacereduces coupling and
interface inheritance can achieve code reusewith the help of object composition. For example:The Spring
framework’s dependency injection promotes code to an interface principle. Another justification for using interfaces
is that they solve the ‘diamond problem’ of traditional multiple inheritance as shown in the figure. Java does not
support multiple inheritance. Java only supports multiple interface inheritance. Interface will solve all the
ambiguities caused by this ‘diamond problem
Campbell Ritchie wrote:I am not sure you have understood it. You can provide getXXX methods for those instance fields which are used by other classes, and setXXX methods for fields which are set from other classes. Obviously you don’t always need both setXXX and getXXX methods for a particular field.
Paul Witten wrote:
What they mean is "if you are forced to redesign your class often then that indicates that a polymorphic interface(s) would be preferable."