aspose file tools*
The moose likes Beginning Java and the fly likes Puzzle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Puzzle" Watch "Puzzle" New topic
Author

Puzzle

Rajan Venkat
Ranch Hand

Joined: Dec 17, 2006
Posts: 35
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


regards,<br /> rajan<br /> SCJP 1.4
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Rajan Venkat
Ranch Hand

Joined: Dec 17, 2006
Posts: 35
(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?
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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...)
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
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

Joined: Dec 17, 2006
Posts: 35
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

Joined: Dec 17, 2006
Posts: 35
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Dec 17, 2006
Posts: 35
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: May 08, 2007
Posts: 621
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Puzzle