• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Puzzle

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Here i need to design a class , interface for the scenario
There are two things

1) Mammals which has function feed() .
2) Bird which has function fly().

We know Bat is a mammal as well as bird , bluewhale is a mammal ,pigeon is a bird.

Here are the rules :
Rule No 1 : All the mammals should feed their babies similiar in other words feed function is similiar for all the mammals.

Rule No 2: All the birds should fly similiar in other words fly function is similiar for all the birds.

Rule No 3 : Duplicate of code should not be there.

how can we implement classes and interfaces for the two generic type Mammal and Birds and for the three Specific type Bat , Bluewhale , Pigeon.

Can anyone of you tell me the answer for this
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajan,

How about this way?

(i) Have an interface which declares the feed() and fly() method
(ii) Have an abstract class implementing the interface
(iii) Define the methods feed() and fly() in the abstract class
(iv) Declare the Classes Mammals and Birds and make them extend the above said abstract class.

This way any descendant classes for Mammals and Birds would get to have the implementation defined in the base abstract class and you dont need to redefine them.
 
Rajan Venkat
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(i) Have an interface which declares the feed() and fly() method
(ii) Have an abstract class implementing the interface
(iii) Define the methods feed() and fly() in the abstract class
(iv) Declare the Classes Mammals and Birds and make them extend the above said abstract class.

This way any descendant classes for Mammals and Birds would get to have the implementation defined in the base abstract class and you dont need to redefine them.



Let us take Bluewhale which is a mammal so i extend the Mammals class but the bluewhale will have fly function because the mammals super abstract class is having both functions defined . The bluewhale will fly does it makes sense?
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajan Venkat:
Hi All,

Here i need to design a class , interface for the scenario
There are two things

1) Mammals which has function feed() .
2) Bird which has function fly().

We know Bat is a mammal as well as bird , bluewhale is a mammal ,pigeon is a bird.

Here are the rules :
Rule No 1 : All the mammals should feed their babies similiar in other words feed function is similiar for all the mammals.

Rule No 2: All the birds should fly similiar in other words fly function is similiar for all the birds.

Rule No 3 : Duplicate of code should not be there.

how can we implement classes and interfaces for the two generic type Mammal and Birds and for the three Specific type Bat , Bluewhale , Pigeon.

Can anyone of you tell me the answer for this



Good question.
Sounds like multiple inheritance. But that�s not allowed in Java.

Go for encapsulation. Something like:

1. Create an interface Behavior, with behave() method.
2. Create different-different behavior classes implementing Behavior interface and behave() method.
3. Now any class wants to have a behavior will "has a" Behavior (association relationship).



4. Now what about those who can do both feed and fly. Well, you can implement this something like this:



Hope it�s a good design.

(It's called strategy pattern, keeping behavior separate from objects...)
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have made a program as per the rules explained by you
check it out if it gives the output you expect..I am not exactly sure about the answer but may be it is correct according to the creteria explained by you.
Below is the java class shown.


abstract class Mammel implements Fun{

public void Feed(String bluewhale)
{
//add the statements for feeding .
}

public void Fly(String Bat) {
//add the function for flying.
//now here call the function Feed() for Bat as its a mammel which can feed as wel as fly.
Feed(Bat);
}
}
public abstract class Bird extends Mammel{
public abstract void Fly(String Pigeon);


public static void main(String[] args) {

Bird bd=null;
bd.Fly("Pigeon");
Mammel mm=null;
mm.Feed("bluewhale");
mm.Fly("Bat");//this method already has a method Feed()....so here Bat can Feed as well as Fly.
}
}


Here comes the interface which consist of two methods Feed and Fly.


public interface Fun {
public abstract void Feed(String bird);
public abstract void Fly(String mammel);
}


Please let me know if this program gives the expected output.
 
Rajan Venkat
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. Create an interface Behavior, with behave() method.
2. Create different-different behavior classes implementing Behavior interface and behave() method.
3. Now any class wants to have a behavior will "has a" Behavior (association relationship).



what about generic type "Mammal" and "Bird" , can you tell me how do you implement the generic type?
 
Rajan Venkat
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have made a program as per the rules explained by you
check it out if it gives the output you expect..I am not exactly sure about the answer but may be it is correct according to the creteria explained by you.
Below is the java class shown.


abstract class Mammel implements Fun{

public void Feed(String bluewhale)
{
//add the statements for feeding .
}

public void Fly(String Bat) {
//add the function for flying.
//now here call the function Feed() for Bat as its a mammel which can feed as wel as fly.
Feed(Bat);
}
}
public abstract class Bird extends Mammel{
public abstract void Fly(String Pigeon);


public static void main(String[] args) {

Bird bd=null;
bd.Fly("Pigeon");
Mammel mm=null;
mm.Feed("bluewhale");
mm.Fly("Bat");//this method already has a method Feed()....so here Bat can Feed as well as Fly.
}
}


Here comes the interface which consist of two methods Feed and Fly.


public interface Fun {
public abstract void Feed(String bird);
public abstract void Fly(String mammel);
}


Please let me know if this program gives the expected output



I am not looking for a program, all i want is a design that can be implementable. If i want to add any new classes in future then it should be done.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajan Venkat:


what about generic type "Mammal" and "Bird" , can you tell me how do you implement the generic type?



Where is the problem? You can have both the classes in place and other "actual animals" classes would extends one of the classes...
 
Rajan Venkat
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Where is the problem? You can have both the classes in place and other "actual animals" classes would extends one of the classes...



Let us consider Bat which is a bird as well as mammal, how can we extend both classes mammal and bird ?
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajan Venkat:


Let us consider Bat which is a bird as well as mammal, how can we extend both classes mammal and bird ?



I understood your concern.

How about this design:

1. Two interface BirdBehavior and MammalBehavior.
2. One class Animal to have common methods applies to both.
3. Now other "actual animal" classes will extends Animal and "has a" any or both behavior interface (association).

 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can give a idea about it ,that if you write a class inside the interface
The class will be implicitly static, and you instantiate it just like you would any other class:
As a matter of fact, the class inside the interface is implicitly public as well, so you can skip that modifier:
After that you can create another class which implements the above given interface.Example of this shown below.


public interface Foo {
>
> Bar getBar();
>
> public class Bar {
>
> private String value;
>
> public Bar(String value) {
> this.value = value;
> }
> }
> }
>
>
> public class Example implements Foo {
>
> public Bar getBar() {
> return new Foo.Bar("abc");
> }
> }
>

i hope if this type of implementation works in your program as well.But i am realy not sure about it.

reply
    Bookmark Topic Watch Topic
  • New Topic