And you couldn't get it to compile any more than I could.Monica Shiralkar wrote:. . . I just created interface with that name Foo . . .
Stephan van Hulst wrote:I don't see how you drew that conclusion.
Why would classes that implement the same interface not have anything in common except the default method?
have more in common than you think
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your Code
Mike Simmons wrote:And I'm not sure it's a very useful statement either way.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your Code
Monica Shiralkar wrote:For example in case of an animal type and sub types cat and dog doing bark, the subtypes are related. So it cannot be interfaces and has to be abstract classes.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your Code
Not at all; it is quite possible to implement LifeForm or Animal or Carnivore interfaces.Monica Shiralkar wrote:. . . in case of an animal type and sub types cat and dog . . . it cannot be interfaces . . .
Monica Shiralkar wrote:Point is when to use abstract classes and when to instead use Java 8 Interfaces?
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your Code
Mike Simmons wrote:... you need to ask, is this really a method that should be part of the public API for your class? Or is it something that should remain a hidden implementation detail. If the latter, consider other ways of code sharing, like using a protected method in an abstract or base class, or making it a static method or a public method of some other class. Default methods are good for some things, but don't overuse them in places where they don't make sense.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your Code
Junilu Lacar wrote:
Monica Shiralkar wrote:Point is when to use abstract classes and when to instead use Java 8 Interfaces?
My rule of thumb is to prefer interfaces over abstract classes. I can't remember the last time I've created an abstract class, honestly. If all you're interested in is the contract for behavior, which is most of the time anyway, then use interfaces. If you think there's something to gain by having a more concrete superclass-subclass relationship, then use abstract classes as your foundation. I think a class hierarchy imposes more restrictions on your design because you have to think about inheritance and the semantics of equals() and a bunch of other considerations that Joshua Bloch explains quite thoroughly in his book "Effective Java Programming".
Monica Shiralkar wrote:...But if one should use Interfaces all the time then abstract classes also do exist for some reason.
The Java Tutorials wrote:Abstract Classes Compared to Interfaces
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
Which should you use, abstract classes or interfaces?
Consider using abstract classes if any of these statements apply to your situation:
You want to share code among several closely related classes. You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior. You want to take advantage of multiple inheritance of type.
Mike Simmons wrote:So, if you've got some code in a private method, that you want to share between classes, don't put it into an interface default method just so you can share it. That makes it a public method - and initially it was a private method.
Mike Simmons wrote: Some things might have been a good idea originally, but now there are better ways, so no longer a good idea. Some things might have never been a good idea, but got into the language anyway.
beichen cao wrote:abstract class can have normal methods and abstract methods, interfaces hava abstract method and static method
Campbell Ritchie wrote:When did you last look at the JLS? Can you have private default methods now?
salvin francis wrote:
Monica Shiralkar wrote:...But if one should use Interfaces all the time then abstract classes also do exist for some reason.
I think the standard java tutorial provides a very good explaination: https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
The Java Tutorials wrote:Abstract Classes Compared to Interfaces
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
Which should you use, abstract classes or interfaces?
Consider using abstract classes if any of these statements apply to your situation:
You want to share code among several closely related classes. You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior. You want to take advantage of multiple inheritance of type.
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
|