Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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



There are 2 classes, 1 LandAnimal and another WaterAnimal. There is another class Animal which want..

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

There are 2 classes, 1 LandAnimal and another WaterAnimal. There is another class Animal which want..

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

There are 2 classes, 1 LandAnimal and another WaterAnimal. There is another class Animal which want..

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

There are 2 classes, 1 LandAnimal and another WaterAnimal. There is another class Animal which want..

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

There are 2 classes, 1 LandAnimal and another WaterAnimal. There is another class Animal which want..

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

There are 2 classes, 1 LandAnimal and another WaterAnimal. There is another class Animal which want..

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

Post New Answer

More Core Java Interview Questions

How do you identify if jvm is 32-bit or 64-bit from java program?

0 Answers  


what is wrapper class and its uses?

2 Answers  


What is garbage collector?

0 Answers  


What is the purpose of a default constructor?

0 Answers  


What is :: operator in java 8?

0 Answers  


Do you know how to reverse string in java?

0 Answers  


What are the new features in java 8?

0 Answers  


11. static class A { 12. void process() throws Exception { throw new Exception(); } 13. } 14. static class B extends A { 15. void process() { System.out.println(”B”); } 16. } 17. public static void main(String[] args) { 18. new B().process(); 19. } What is the result? 1 B 2 The code runs with no output. 3 Compilation fails because of an error in line 12. 4 Compilation fails because of an error in line 15.

6 Answers  


Which command from the jdk compiles a java program?

0 Answers  


What is the java idl system?

0 Answers  


What does system out println () do?

0 Answers  


Given: 11. public static void main(String[] args) { 12. Integer i = uew Integer(1) + new Integer(2); 13. switch(i) { 14. case 3: System.out.println(”three”); break; 15. default: System.out.println(”other”); break; 16. } 17. } ‘What is the result? 1 three 2 other 3 An exception is thrown at runtime. 4 Compilation fails because of an error on line 12.

9 Answers  


Categories