![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 7
Rep Power: 0
![]() |
Interface design?
As a simple practice in the interface mechanism for a new java programmer, I am implementing interfaces for the following classes including requirements:
1. Bats and sparrows can fly (a possible method in the interface might be maxTimeInFlight). 2. Rats, bats, and lions are carnivorous (a possible method might be huntingRadius). I have defined four classes and two interfaces. Does anyone see anything fundamentaly wrong with what I have so far? Are there any other java concepts\code I can include to enhance what I have? interface Fly {
String maxTimeInFlight();
}
interface Carnivorous {
String huntingRadius();
}
class Bats implements Fly, Carnivorous {
public String maxTimeInFlight() {
return "maxTimeInFlight is ...";
}
public String huntingRadius() {
return "huntingRadius is ...";
}
}
class Sparrows implements Fly {
public String maxTimeInFlight() {
return "maxTimeInFlight is ...";
}
}
class Rats implements Carnivorous {
public String huntingRadius () {
return "huntingRadius is ...";
}
}
class Lions implements Carnivorous {
public String huntingRadius () {
return "huntingRadius is ...";
}
}
class Interface {
public static void main(String args[]) {
Bats bat = new Bats();
Lions lion = new Lions();
System.out.println(bat.maxTimeInFlight());
System.out.println(bat.huntingRadius());
System.out.println(lion.huntingRadius());
}
}Thank you. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
It does what you want it to, all Bats will be able to be passed to methods which take Carnivorous or Fly arguments, and other such polymorphic uses. The naming seems a bit off, first of all, the class should be "Bat" not "Bats". If you did more coding im sure you would notice this..
Bats b = new Bats(); Looks odd doesnt it. Also you could probably do with a better name for the "Fly" interface, but those are all superficial problems. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 7
Rep Power: 0
![]() |
Fantastic!! Thanks for the tips.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|