Java Question Of Interfaces

0 Developer John · February 26, 2015
I haven't reached the difference between implementing or extending, but how can a class get information from an interface? What are the rules for what a class can implement or extend? Would about an interface itself? 

-Thanks!8-)

Post a Reply

Replies

Oldest  Newest  Rating
+1 Branislav Lazic · February 26, 2015
Class can extend only one class. However, it can implement multiple interfaces. Why is that important? Why Java doesn't support multiple inheritance?
Consider next situation:

/images/forum/upload/2015-02-26/20e5c025fe4088af0f45ce46ebb2131e.jpg

EXAMPLE IF MULTIPLE INHERITANCE IS ALLOWED IN JAVA:

Let's say that we have 4 classes: Animal, Cat, Pet and Munchkin.
Animal class has one method called: "eat". Cat and Pet classes inherit that method. One class can possibly override that method. Munchkin extends both Pet and Cat classes and inherits "eat" method. If "eat" method was overriden in Cat or Pet question is: Which "eat" method will Munchkin class inherit?
With behavior of "eat" method within Cat class or with behavior of "eat" method within Pet class. This situation is called: "Diamond of death".

REAL EXAMPLE (MULTIPLE INHERITANCE NOT ALLOWED):

Animal, Cat and Pet are interfaces rather then classes. Even tho Cat and Pet inherit "eat" method it's assured that "eat" method will not be overriden in non of those two classes and assured that "eat" method will be overriden in Munchkin class. Therefore, concrete Munchkin class will be "sure" to inherit one same method without of any behavior since methods in interfaces are abstract.
0 Developer John · February 26, 2015
I know that interfaces can only extend other interfaces, but would about implementation? 
0 Nicholas Eason · February 26, 2015
Classes implement interfaces. Classes extend classes, be it abstract or not. To be honest, that's about all I know about the difference between the two either ;).
  • 1

Java / Android Development

107,167 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator