• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference and purpose of Extends and Implements  RSS feed

 
Benjamin Bielschowsky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty new to java here, I know extends is for classes and implements is for interfaces. I know that you can only extend one class and implement multiple interfaces. And I think that interfaces have methods without bodies and Classes have methods with bodies?

Here is an example I found online. I don't get the purpose of class Animal and interface Pet. I see that class Dog extends and implements them, but then declares those methods all over again, couldn't Dog just declare those methods on its own without extending or implementing? I think of it like variables. I guess my problem here is that I see Dog could stand on its own without implementing and extending, could some one please clarify? Also, could you implement a Class?

01 abstract class Animal {
02 private String name;
03
04 public void eat();
05 public void sleep();
06 }
07
08 public interface Pet {
09 public void play();
10 }
11
12 public class Dog extends Animal implements Pet {
13 public void eat() {
14 // do stuff
15 }
16
17 public void sleep() {
18 // do stuff
19 }
20
21 public void play() {
22 // do stuff
23 }
24 }
25
26 public class Hippo extends Animal {
27 public void eat() {
28 // do stuff
29 }
30
31 public void sleep() {
32 // do stuff
33 }
34 }
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dog has to implement play() (The Interface)

Dog overrides eat() and sleep()

Hippo doesn't need to play() so doesn't implement the interface

WP
 
Benjamin Bielschowsky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the difference if I rewrote the code as follows?

public class Dog(){

public void eat(){
//Do stuff
}
public void sleep(){
//Do stuff
}
public void Play(){
//Do stuff
}
}

public class Hippo(){

public void eat(){
//Do stuff
}
public void sleep(){
//Do stuff
}
}
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wouldn't be able to use a Dog in place of a Hippo.

The purpose of an interface is to define a contract for behavior without requiring any particular hierarchy for the implementation. Interfaces allow you to write code that is more flexible and general in nature. Any code that uses a reference to an interface can be expected to work, no matter what kind of implementation class is actually used at runtime. Take the following example:


Since the Mechanic class is programmed to deal with the Automobile interface, it expects any object that gets passed to its method to adhere to the contract for behavior of an Automobile. If it were programmed to reference only SportsCar, then it's utility is limited to that particular class only. With interfaces, a Mechanic can deal with any implementation of Automobile that correctly implements the general behavior for start(), stop(), reverse(), park(), etc. It can even handle any new implementations that get created in the future!
 
Benjamin Bielschowsky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So implementing would change the type of the class, like type casting?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Bielschowsky wrote:So implementing would change the type of the class, like type casting?

No, implementing is the same as the class saying "Ok, I understand that an Automobile is supposed to behave a certain way, I promise that I will behave in whatever way all Automobiles are expected to behave." As long as the implementing class sticks to that promise, it can do whatever and be whatever else it wants to be. It's like when I became an American citizen, I took an oath to do certain things as an American. It didn't change me as a person, I'm still originally from the Philippines, I'm still Asian, I still speak my native language, but now since I took that oath of citizenship as an American, there are circumstances under which I am treated as, and expected to behave as, any other American citizen. My class is ME. The interface I implement is American. So to speak.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also... type-casting doesn't actually change the type of a class. Nor does it change the type of an object. It assigns an object of class X to a variable of type Y, provided that is allowed. No changes to anything occur. But you can't understand that yet, because you haven't yet understood inheritance of types. So carry on studying that.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to extend my example from before:


Notice that there's no casting being done. As long as an object says it obeys the contract for Automobile, bob, the Mechanic, will take it. Notice also that on Line 13, bigfoot is declared not as an Automobile but its actual implementation class, MonsterTruck. This means that if a MonsterTruck had a method called squash(), anything else that saw bigfoot as a MonsterTruck could ask it to execute that method. Since bob only sees things as Automobiles, he can only ask them to do whatever Automobiles can do.


On line 15, even if bob was dealing with bigfoot, which can actually squash an Automobile, bob can't call squash() because all he knows is that he's dealing with an Automobile. He is totally unaware that he actually has a MonsterTruck, if indeed that was what was passed to the testDrive() method. Line 15 would produce a compiler error. It is illegal code.
 
Benjamin Bielschowsky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow this helped a lot, just a little more verification needed though.

I am so use to seeing stuff declared like
int[] array = new int[5];

where the left matches the right. int[]

Could you give me the layout for how "new" works? Is it:

Class <name> = new <class> ?

Automobile myStang = new SportsCar();
Automobile aCamry = new FamilySedan();
MonsterTruck bigfoot = new MonsterTruck();

 
Benjamin Bielschowsky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add on, is there a "Drag-and-drop" plug-in for eclipse that allow developers to easily create GUI interfaces? And another question, does Java have placeholders for printing? I come from C# background and if you wanted to incorporate multiple variables in a print you could just write: Console.WriteLine("Hello my name is {0}, and I like to eat {1}", Billy, Pizza); Where {0} and {1} is replaced with Billy and Pizza respectfully. I am aware that you could use System.out.println("Hello my name is" + Bob +"and I like to eat " + Pizza); but I find the constant + and "" redundant.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin,

I don't know if you have been "officially" greeted but if not, Welcome to the Ranch!

The Java Tutorials is a great place to start learning about a lot of the basics that you've been asking about.

A few pointers about protocols we like to follow here:
1. When posting code snippets, please UseCodeTags (<-- click) to make the listings more readable.
2. Please UseOneThreadPerQuestion (<-- click) so that the topic doesn't wander from one subject to another.
3. Please try to SearchFirst (<-- click). Sometimes people will be in the mood to give long answers like I did earlier but more often they will appreciate it when the person they are helping can ShowSomeEffort (<-- click) first so that there's no need to go over the general knowledge type things.

Sorry if this kind of leaves you hanging but like I said, sometimes people are in the mood to give long answers and sometimes they're not. Go look at those tutorials I mentioned. They'll help you a lot.

See you around!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:So to extend my example from before:

Hi Junilu,

A reminder to you: please don't put very long lines inside code blocks; they screw up the formatting. I've split the really long ones in your code this time.

Thanks

Winston
 
Benjamin Bielschowsky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips, this is my first forum I have ever joined, I am very eager to get better and learn how to create cool programs. I will open up new threads to answer my future questions, thanks for the welcome and the tips.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!