regards,<br /> rajan<br /> SCJP 1.4
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]
(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.
regards,<br /> rajan<br /> SCJP 1.4
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
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).
regards,<br /> rajan<br /> SCJP 1.4
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
regards,<br /> rajan<br /> SCJP 1.4
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...
regards,<br /> rajan<br /> SCJP 1.4
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 ?
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |