Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes clear my doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "clear my doubt" Watch "clear my doubt" New topic
Author

clear my doubt

VIGNESHWAR VISWANATHAN
Greenhorn

Joined: May 26, 2007
Posts: 17
new can be used with interface to return the object of other class or not....
please share your views... thank you
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
I don't understand your question.

Are you reffering to creating anonymous classes using interfaces or to upcasting created objects to interface they implement?
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
Hi
Please specify proper subject line and make sure you read your message atleast once before posting .
Thanks
VIGNESHWAR VISWANATHAN
Greenhorn

Joined: May 26, 2007
Posts: 17
interface Animal
{
void soundOff();
}
class Elephant implements Animal
{
public void soundOff()
{
System.out.println("Trumphet");
}
}
class Lion implements Animal
{
public void soundOff()
{
System.out.println("Roar");
}
}
class Alpha1
{
static Animal get(String choice)
{
if(choice.equalsIgnoreCase("meat eater"))
{
return new Lion();
}
else
{
return new Elephant();
}}}

which compiles?
A. new Animal().soundOff();
B.Elephant e=new Alpha1();
C.Lion 1=Alpha.get("meat eater");
D.new Alpha1().get("veggie").soundOff();

this is the code... here i have learnt that in option A. you cannot use new ininterface...Is it so?
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Yes it is, you can not create instance of interface. (Interface methods have no body, what would they do?)
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

VIGNESHWAR

There are no constructors for interfaces.So can't create an object.


Thanks

Anil Kumar
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Originally posted by VIGNESHWAR VISWANATHAN:
which compiles?
A. new Animal().soundOff();
B.Elephant e=new Alpha1();
C.Lion 1=Alpha.get("meat eater");
D.new Alpha1().get("veggie").soundOff();

Such questions can easily be answered by simply trying it out. Write a small Java program with the lines mentioned under options A to D, try to compile it and see what the compiler says.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clear my doubt