There are 2 classes, 1 LandAnimal and another WaterAnimal.
There is another class Animal which wants to have the
properties of both LandAnimal and WaterAnimal. How will you
design this situation?
Answers were Sorted based on User's Feedback
Answer / bhaskr reddy
interface LandAnimals {
class LandAnimal {
// define the Land Animal prperties
}
}
interface WaterAnimals{
class WaterAnimal{
// define the Water Animal prperties
}
}
Now define the "Animal" class by implementing the two
interfaces(WaterAnimals and LandAnimals ).
Note1 : Java supports defining the class inside an
interface.
Note 2 : In the previous answer somebody has defined two
seperate classes for WaterAnimal and Land animal
and his LandAnimal extends WaterAnimal then LandAnmal
becomes the Water animal this is poor object orientation.
Is This Answer Correct ? | 18 Yes | 1 No |
Answer / abdul
class WaterAnimal
{
// code
}
class LandAnimal extends WaterAnimal
{
//This class has WaterAnimal properties
}
class Animal extends LandAnimal
{
//This class has both LandAnimal and WaterAnimal properties
}
Is This Answer Correct ? | 10 Yes | 9 No |
Answer / sahaj
[note:landanimal and wateranimal both behave as base class
and the other one behave as derieved class]
#include<iostream.h>
#include<conio.h>
class landanimal
{
void print();
{
cout<<"this is base class"<<endl;
}};
class wateranimal
{
void display();
{
cout<<"this is another base class"<<endl:
}};
class animal:public landanimal,public wateranimal
{
void disp();
{
cout<<"this is derieved class"<<endl;
}};
void main()
{
animal a;
a.print();
a.display();
a.disp();
getch();
}
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / kash
@ Bhaskr Reddy
The only problem I see with your solution is when I want to create the object Animal which has to implement properties from both WaterAnimal and LandAnumal: How will I create an instance of of class Animal which implements both the interfaces at the same time?
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / suganya from tamilnadu
class Animal
{
// code
}
class LandAnimal extends Animal
{
//This class has WaterAnimal properties
}
class WaterAnimal extends Animal
{
//This class has both LandAnimal and WaterAnimal properties
}
Is This Answer Correct ? | 1 Yes | 4 No |
Answer / vikneswarank
class LandAnimal
{
//some code here
}
class WaterAnimal extends LandAnimal
{
//soma code here
}
class Animal extends landAnimal
{
// here we can access properties both class
}
Is This Answer Correct ? | 2 Yes | 19 No |
List types of storage classes in java?
Why would you desing a J2EE application so user data is entered by way of a JSP page and managed by an underlying Java Beans class?
What are the common uses of "this" keyword in java ?
What if constructor is protected in java?
What is a boolean used for?
Can constructor be synchronized?
What is boolean keyword in java?
Can java arraylist hold different types?
Why chararray() is preferred over string to store the password?
What is garbage collection? What is the process that is responsible for doing that in java?
What are constants and how to create constants in java?
Does A Class Inherit The Constructors Of Its Superclass?