Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 4th, 2005, 12:57 AM   #1
AusTex
Newbie
 
Join Date: Feb 2005
Posts: 7
Rep Power: 0 AusTex is on a distinguished road
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.
AusTex is offline   Reply With Quote
Old Jul 4th, 2005, 2:30 AM   #2
VigilanteP@comcast.net
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 VigilanteP@comcast.net is on a distinguished road
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.
VigilanteP@comcast.net is offline   Reply With Quote
Old Jul 4th, 2005, 2:46 AM   #3
AusTex
Newbie
 
Join Date: Feb 2005
Posts: 7
Rep Power: 0 AusTex is on a distinguished road
Fantastic!! Thanks for the tips.
AusTex is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC